....though I've had a play now and come up with a couple of things - this
function will take a string along the lines of 2/6 and reduce it to 1/3...
Function reduce(t)
If IsNull(t) Then
reduce = ""
Exit Function
End If
'num is the numerator
'den is the denominator
'pos is the position of the /
Dim num As Long, den As Long, pos As Integer
pos = InStr(t, "/")
If pos = 0 Then
reduce = t
Exit Function
End If
num = CLng(Left(t, pos - 1))
den = CLng(Mid(t, pos + 1))
Dim i As Long
'maxfactor is the maximum value that it's going to be possible to divide
'both top and bottom by... the maximum of the numerator and the integer
'part of half of the denominator
Dim maxfactor As Long
maxfactor = num
If Int(den / 2) < maxfactor Then
maxfactor = Int(den / 2)
End If
For i = maxfactor To 2 Step -1
If num Mod i = 0 And den Mod i = 0 Then
num = num / i
den = den / i
Exit For
End If
Next
If num Mod den = 0 Then
reduce = num / den
Else
reduce = CStr(num) & "/" & CStr(den)
End If
End Function
And then this function takes a decimal and converts it to it's simplest
fraction....
Function dectofrac(d As Double) As String
'Multiply d by increasing values... when the result is an integer
'(or close to one enough to just be a rounding error)
'then that multiplier is going to be the denominator of the fraction.
'MaxDen is the largest denominator that can be coped with
'Its value would be determined by the actual data in use
Dim MaxDen
MaxDen = 10000000
Dim i As Long
For i = 1 To MaxDen
If IsInt(d * i) Then
Exit For
End If
Next
If i = MaxDen + 1 Then
dectofrac = "Not found"
Else
If i = 1 Then
dectofrac = CStr(Round(d * i, 0))
Else
dectofrac = (CStr(Round(d * i, 0)) & "/" & CStr(i))
End If
End If
End Function
....which uses this...
Function IsInt(d As Double) As Boolean
'determines whether d is an integer value... or very close to an integer
value to
'allow for rounding errors
IsInt = Abs(d - Round(d, 0)) < 0.00000000001
End Function
Those need some error handling added, and I'm sure I'm missing some
exceptions (e.g. division by zero type stuff) but they seem to work for me.