L
LB
I'm trying to create a function to calculate the period of a sum of
sinusoids. This is nothing more than the least common multiple of the
periods of all contributions. I note that worksheet function LCM does
not appear to be available in VBA. But that wouldn't work anyway since
it can't handle real arguments, so I create my own called LCM_real.
Problem with LCM_real is that it only accepts 2 arguments, but that can
be solved through using it in a looping function. I would like this
function to operate like the worksheet one, in that you can place in the
argument up to 29 sine wave periods separated by commas, or you can
enter a range (no commas). I have the following code for comma
delimited argument:
Function PeriodOfSinuSum(ParamArray T() As Variant) As Double
Dim i As Integer, tmp As Double
tmp = LCM_Real(T(0), T(1))
For i = 2 To UBound(T())
tmp = LCM_Real(tmp, T(i))
Next i
PeriodOfSinuSum = tmp
End Function
I note that this function doesn't work if you try entering a range, so I
come up with another one for that purpose:
Function PeriodOfSinuSumRange(T As Range) As Double
Dim i As Integer, tmp As Double
tmp = LCM_Real(T(1), T(2))
For i = 3 To UBound(T())
tmp = LCM_Real(tmp, T(i))
Next i
PeriodOfSinuSumRange = tmp
End Function
This one works with a range, but doesn't work if you input each period
separated by a comma. Can anyone recommend a single function that will
accept both types of input?
sinusoids. This is nothing more than the least common multiple of the
periods of all contributions. I note that worksheet function LCM does
not appear to be available in VBA. But that wouldn't work anyway since
it can't handle real arguments, so I create my own called LCM_real.
Problem with LCM_real is that it only accepts 2 arguments, but that can
be solved through using it in a looping function. I would like this
function to operate like the worksheet one, in that you can place in the
argument up to 29 sine wave periods separated by commas, or you can
enter a range (no commas). I have the following code for comma
delimited argument:
Function PeriodOfSinuSum(ParamArray T() As Variant) As Double
Dim i As Integer, tmp As Double
tmp = LCM_Real(T(0), T(1))
For i = 2 To UBound(T())
tmp = LCM_Real(tmp, T(i))
Next i
PeriodOfSinuSum = tmp
End Function
I note that this function doesn't work if you try entering a range, so I
come up with another one for that purpose:
Function PeriodOfSinuSumRange(T As Range) As Double
Dim i As Integer, tmp As Double
tmp = LCM_Real(T(1), T(2))
For i = 3 To UBound(T())
tmp = LCM_Real(tmp, T(i))
Next i
PeriodOfSinuSumRange = tmp
End Function
This one works with a range, but doesn't work if you input each period
separated by a comma. Can anyone recommend a single function that will
accept both types of input?