Is there a VB or VBA math function which will calculate the factors of
a number?
i.e. for 24 the factors could be 1,2,3,4,8,6,12
I need to find the lowest common denominator for a macro that I am
writing.
TIA
Garry
Jason Lepack - 22 Jan 2007 21:52 GMT
Excel already has the function GCD(num1, num2, num3, ...) which finds
the greatest common divisor of multiple numbers.
That should serve your purpose.
Cheers,
Jason Lepack
> Is there a VB or VBA math function which will calculate the factors of
> a number?
[quoted text clipped - 6 lines]
>
> Garry
Bernie Deitrick - 22 Jan 2007 21:52 GMT
Garry,
Try the code below.
HTH,
Bernie
MS Excel MVP
Sub test()
Dim myFactors As Variant
Dim myNum As Long
Dim i As Integer
myNum = InputBox("What Number?")
myFactors = FactorFunction(myNum)
For i = LBound(myFactors) To UBound(myFactors)
MsgBox myFactors(i)
Next i
End Sub
Function FactorFunction(inVal As Long) As Variant
Dim myValue As Long
Dim myFA() As Long
Dim myCount As Integer
Dim i As Long
myCount = 1
ReDim Preserve myFA(1 To myCount)
For i = 2 To inVal / 2
If inVal Mod i = 0 Then
ReDim Preserve myFA(1 To myCount)
myFA(myCount) = i
myCount = myCount + 1
End If
Next i
FactorFunction = myFA
End Function
> Is there a VB or VBA math function which will calculate the factors of
> a number?
[quoted text clipped - 6 lines]
>
> Garry
gw.boswell@gmail.com - 22 Jan 2007 22:13 GMT
Bernie,
Thanks for the code. I will try it. Other experts also pointed me to
the GCD function which may/maynot work for my application but I thank
one and all for the help.
Garry
> Garry,
>
[quoted text clipped - 49 lines]
> >
> > Garry
gw.boswell@gmail.com - 22 Jan 2007 22:13 GMT
Bernie,
Thanks for the code. I will try it. Other experts also pointed me to
the GCD function which may/maynot work for my application but I thank
one and all for the help.
Garry
> Garry,
>
[quoted text clipped - 49 lines]
> >
> > Garry
okrob - 22 Jan 2007 21:57 GMT
Lowest common denominator is usually 1 for any factors... Use Greatest
Common Divisor:
=GCD(1,2,3,4,8,6,12) etc...
Rob
> Is there a VB or VBA math function which will calculate the factors of
> a number?
[quoted text clipped - 6 lines]
>
> Garry