Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I have a Simply Fraction UDF which works fine whenever the numerator is less than the denominator, but fails other wise. For example for n = 50, d = 75 it outputs 2/3 but for n = 5 , d = 4 it is failing. Any reason?
Code:
Private Function reduceFraction(n As Integer, d As Integer)
Dim numL As Variant
Dim numR As Variant
Dim i As Integer
'check for both negative
If n < 0 And d < 0 Then
n = -1 * n: d = -1 * d
End If
'take out the whole number
whole = Fix(n / d)
'numL = IIf(whole > 0, Trim(whole) & " ", "")
'set n to the remainder
n = n Mod d
MsgBox Abs(n)
For i = Abs(n) To 2 Step -1
If n / i = Fix(n / i) And d / i = Fix(d / i) Then
numL = IIf(whole > 0, Trim(whole) & " ", "")
'Debug.Print numL
numR = Trim(n / i) & "/" & Trim(d / i)
'Debug.Print numR
reduceFraction = numL & numR
Exit For
End If
Next i
End Function