Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default VBA or VB math function

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 120
Default VBA or VB math function


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

wrote:
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default VBA or VB math function

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



wrote in message
ps.com...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default VBA or VB math function

Lowest common denominator is usually 1 for any factors... Use Greatest
Common Divisor:
=GCD(1,2,3,4,8,6,12) etc...


Rob

wrote:
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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default VBA or VB math function

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

Bernie Deitrick wrote:
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



wrote in message
ps.com...
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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default VBA or VB math function

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

Bernie Deitrick wrote:
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



wrote in message
ps.com...
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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math Function? Doug Excel Discussion (Misc queries) 5 August 24th 09 05:15 PM
+ or - as text, not math function MLM Excel Worksheet Functions 3 November 6th 08 07:22 AM
What is the math behind array function yshridhar Excel Discussion (Misc queries) 2 December 7th 07 10:44 AM
Math Function Curtis Excel Discussion (Misc queries) 2 July 28th 06 04:24 AM
Need help with math Function wytedragn Excel Worksheet Functions 2 June 29th 06 10:51 PM


All times are GMT +1. The time now is 05:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"