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

#### Emily

##### Active Member
To: TsTom

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.

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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Replies
17
Views
526
Replies
4
Views
444
Replies
0
Views
133
Replies
0
Views
87
Replies
13
Views
217

### Forum statistics

1,143,709
Messages
5,720,390
Members
422,282
Latest member
psunith ### 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.

### Which adblocker are you using?    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

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