Hi Experts,

I have two long integer fields: YouthNbr and AdultNbr. I need to calculate the ration of Youth To Adults.

e.g. Youth = 8, Adults = 2, Ratio = 4:1 displayed this way.

I want to do this in an Event using VBA, not using a Query.

How do I do it? Is there a Function?

Thanks,

Bob C.

I have two long integer fields: YouthNbr and AdultNbr. I need to calculate the ration of Youth To Adults.

e.g. Youth = 8, Adults = 2, Ratio = 4:1 displayed this way.

I want to do this in an Event using VBA, not using a Query.

How do I do it? Is there a Function?

Thanks,

Bob C.

```
Public Function getRatio(ByVal v1 As Integer, ByVal v2 As Integer) As String
Dim total As Integer
Dim divider As Integer
If v1 > v2 Then
divider = v2
Else
divider = v1
End If
total = v1 + v2
getRatio = Int(v1 / divider) & ":" & Int(v2 / divider)
End Function
```

```
Public Function getRatio(ByVal v1 As Integer, ByVal v2 As Integer) As String
Dim divider As Integer
If v1 > v2 Then
divider = v2
Else
divider = v1
End If
If divider = 0 Then
getRatio = ""
Else
getRatio = Int(v1 / divider) & ":" & Int(v2 / divider)
End If
End Function
```

```
Dim Youth As Integer, Adulss As Integer, Ratio As String
Youth = 8: adults = 2
Ratio = Trim(CStr(Youth / adults)) & ":1"
Debug.Print Ratio
```

pls try

```
Function fRatio(ByVal n1 As Long, ByVal n2 As Long) As String
If n1 = 0 Or n2 = 0 Then
getRatio = ""
Else
nGCD = fGCD(n1, n2)
fRatio = n1 / nGCD & ":" & n2 / nGCD
End If
End Function
Function fGCD(ByVal n1 As Long, ByVal n2 As Long) As Long
Do While n2 <> 0
i = n2
n2 = n1 Mod n2
n1 = i
Loop
fGCD = n1
End Function
```

@Ryan Chong does not work with 8:3 gives 2:1 wrong

Regards

```
Public Function getRatio(ByVal v1 As Integer, ByVal v2 As Integer) As String
Dim divider As Integer
If v1 > v2 Then
If v1 Mod v2 > 0 Then
getRatio = v1 & ":" & v2
Exit Function
End If
divider = v2
Else
If v2 Mod v1 > 0 Then
getRatio = v1 & ":" & v2
Exit Function
End If
divider = v1
End If
If divider = 0 Then
getRatio = "Invalid Ratio"
Else
getRatio = Int(v1 / divider) & ":" & Int(v2 / divider)
End If
End Function
```

```
Public Function getRatio(ByVal v1 As Integer, ByVal v2 As Integer) As String
Dim divider As Integer
If v1 = 0 Or v2 = 0 Then
getRatio = "Invalid Ratio"
Exit Function
End If
If v1 > v2 Then
If v1 Mod v2 > 0 Then
getRatio = v1 & ":" & v2
Exit Function
End If
divider = v2
Else
If v2 Mod v1 > 0 Then
getRatio = v1 & ":" & v2
Exit Function
End If
divider = v1
End If
getRatio = Int(v1 / divider) & ":" & Int(v2 / divider)
End Function
```

```
Ratio = IIf(YouthNbr * AdultNbr = 0, "0:0", IIf(YouthNbr > AdultNbr, YouthNbr\AdultNbr & ":1", "1:" & AdultNbr\YouthNbr))
```

But what if values are 4 and 3? Should the ratio be 4:3 or 1.33:1?/gustav

noted for that, apparently my method didn't apply GCD.

i have done another version to handle that case but don't think I wil post it here, it just look similar to what you have posted.

```
Ratio = IIf(YouthNbr * AdultNbr = 0, "0:0", IIf(YouthNbr > AdultNbr, (YouthNbr * 10 \ AdultNbr) / 10 & ":1", "1:" & (AdultNbr * 10 \ YouthNbr) / 10))
```

That will round down to one decimal maximum./gustav

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial