How do I display a ratio in Excel in the format A:B?
Try this formula:
=SUBSTITUTE(TEXT(A1/B1,"?/?"),"/",":")
Result:
A B C
33 11 3:1
25 5 5:1
6 4 3:2
Explanation:
- TEXT(A1/B1,"?/?") turns A/B into an improper fraction
- SUBSTITUTE(...) replaces the "/" in the fraction with a colon
This doesn't require any special toolkits or macros. The only downside might be that the result is considered text--not a number--so you can easily use it for further calculations.
Note: as @Robin Day suggested, increase the number of question marks (?) as desired to reduce rounding (thanks Robin!).
You are looking for the greatest common divisor (GCD).
You can calculate it recursively in VBA, like this:
Function GCD(numerator As Integer, denominator As Integer)
If denominator = 0 Then
GCD = numerator
Else
GCD = GCD(denominator, numerator Mod denominator)
End If
End Function
And use it in your sheet like this:
ColumnA ColumnB ColumnC
1 33 11 =A1/GCD(A1; B1) & ":" & B1/GCD(A1; B1)
2 25 5 =A2/GCD(A2; B2) & ":" & B2/GCD(A2; B2)
It is recommendable to store the result of the function call in a hidden column and use this result to avoid calling the function twice per row:
ColumnA ColumnB ColumnC ColumnD
1 33 11 =GCD(A1; B1) =A1/C1 & ":" & B1/C1
2 25 5 =GCD(A2; B2) =A2/C2 & ":" & B2/C2
The second formula on that page uses the GCD function of the Analysis ToolPak, you can add it from Tools > Add-Ins.
=A1/GCD(A1,B1)&":"&B1/GCD(A1,B1)
This is a more mathematical formula rather than a text manipulation based on.