Ts Tom, would you mind to debug the code you wrote

Emily

Active Member
Joined
Aug 28, 2002
Messages
304
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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top