To: TsTom
Below code your written in link http://216.92.17.166/board/viewtopic.php?topic=21461&forum=2&start=10
Although there is add-in to find Greatest Common Factor. But for learning purpose I hope you can debug this code, I can't dig it out.
For example Add-in GCD have 6 for GCD(6, 6, 12, 18) but 3 is the answer for the code GCF.
Another example GCD(10, 20, 40 ,50) = 10 but GCF gives 5.
Thanks in advance
Emily
Public Function gcf(ParamArray Values()) As Long
Dim LVF() As Long 'LowValueFactors
Dim OVF() As Long 'Other values' factors
Dim e As Integer, LowValue As Long, ve As Long, fc As Long
Dim HVal() As Long, c As Range
Application.Volatile
LowValue = Application.WorksheetFunction.Min(Values)
ReDim LVF(0): LVF(0) = 1
ReDim HVal(0): HVal(0) = 1
ReDim OVF(0)
'cleans up the argument. Forces only whole numbers. Ignores text.
'For example: 3 values passed 18, 24, 30
'will except =gcf(18,24,30) or =gcf(C1,D1,E1) or =gcf(C1:E1)
For e = 0 To UBound(Values)
If TypeName(Values(e)) = "Range" Then
For Each c In Values(e)
If IsNumeric(Values(e)) Then
ReDim Preserve HVal(UBound(HVal) + 1)
HVal(UBound(HVal)) = Int(c.Value)
End If
Next
Else
If IsNumeric(Values(e)) Then
ReDim Preserve HVal(UBound(HVal) + 1)
HVal(UBound(HVal)) = Int(Values(e))
End If
End If
Next
'find all the factors of the lowest value passed to the function
'and store these values in an array seeing that the smallest
'number will always contain the GCF as one of it's factors.
For e = 2 To LowValue / 2
If LowValue Mod e = 0 Then
ReDim Preserve LVF(UBound(LVF) + 1) As Long
LVF(UBound(LVF)) = e
End If
Next
'find the factors of all other values passed to the function
'and add them to the OVF array of factors
For ve = 1 To UBound(HVal)
For e = 2 To HVal(ve) / 2
If HVal(ve) Mod e = 0 And e <= LowValue Then
ReDim Preserve OVF(UBound(OVF) + 1)
OVF(UBound(OVF)) = e
End If
Next
Next
'count the number of factors and decide if they are
'common to all of the values. If they are not, then
'1 will be the only possible common factor.
For e = UBound(LVF) To 0 Step -1
fc = 0
For ve = 0 To UBound(OVF)
If OVF(ve) = LVF(e) Then
fc = fc + 1
End If
Next
If fc >= UBound(HVal) Then
gcf = LVF(e)
Exit Function
End If
Next
gcf = 1
End Function
Below code your written in link http://216.92.17.166/board/viewtopic.php?topic=21461&forum=2&start=10
Although there is add-in to find Greatest Common Factor. But for learning purpose I hope you can debug this code, I can't dig it out.
For example Add-in GCD have 6 for GCD(6, 6, 12, 18) but 3 is the answer for the code GCF.
Another example GCD(10, 20, 40 ,50) = 10 but GCF gives 5.
Thanks in advance
Emily
Public Function gcf(ParamArray Values()) As Long
Dim LVF() As Long 'LowValueFactors
Dim OVF() As Long 'Other values' factors
Dim e As Integer, LowValue As Long, ve As Long, fc As Long
Dim HVal() As Long, c As Range
Application.Volatile
LowValue = Application.WorksheetFunction.Min(Values)
ReDim LVF(0): LVF(0) = 1
ReDim HVal(0): HVal(0) = 1
ReDim OVF(0)
'cleans up the argument. Forces only whole numbers. Ignores text.
'For example: 3 values passed 18, 24, 30
'will except =gcf(18,24,30) or =gcf(C1,D1,E1) or =gcf(C1:E1)
For e = 0 To UBound(Values)
If TypeName(Values(e)) = "Range" Then
For Each c In Values(e)
If IsNumeric(Values(e)) Then
ReDim Preserve HVal(UBound(HVal) + 1)
HVal(UBound(HVal)) = Int(c.Value)
End If
Next
Else
If IsNumeric(Values(e)) Then
ReDim Preserve HVal(UBound(HVal) + 1)
HVal(UBound(HVal)) = Int(Values(e))
End If
End If
Next
'find all the factors of the lowest value passed to the function
'and store these values in an array seeing that the smallest
'number will always contain the GCF as one of it's factors.
For e = 2 To LowValue / 2
If LowValue Mod e = 0 Then
ReDim Preserve LVF(UBound(LVF) + 1) As Long
LVF(UBound(LVF)) = e
End If
Next
'find the factors of all other values passed to the function
'and add them to the OVF array of factors
For ve = 1 To UBound(HVal)
For e = 2 To HVal(ve) / 2
If HVal(ve) Mod e = 0 And e <= LowValue Then
ReDim Preserve OVF(UBound(OVF) + 1)
OVF(UBound(OVF)) = e
End If
Next
Next
'count the number of factors and decide if they are
'common to all of the values. If they are not, then
'1 will be the only possible common factor.
For e = UBound(LVF) To 0 Step -1
fc = 0
For ve = 0 To UBound(OVF)
If OVF(ve) = LVF(e) Then
fc = fc + 1
End If
Next
If fc >= UBound(HVal) Then
gcf = LVF(e)
Exit Function
End If
Next
gcf = 1
End Function