I have a module that I have made a custom function in. It calculates the Standard Deviation for row level values.
I use 6 variables in the Sub Procedure on the form.
If I use 1, 2, 3 or 4 variables it returns a value that looks like a "Scientific" data type "ie: 5E-07"
If I use 5 or 6 variables it works perfectly except for one thing----if all the variables are the same which should give me an answer of 0.00000 I get an error in a line of the code in the macro. This part is giving me problems:
If n > 0 Then 'only applies if points available
RStDevP = Sqr((n * dblSumOfSq - dblSum * dblSum) / n / n)
Else
RStDevP = Null
End If
Can anyone look at it and help me with what is wrong?
Thanks in advance!
Function RStDevP(ParamArray FieldValues()) As Variant
Dim dblSum As Double, dblSumOfSq As Double
Dim n As Long
Dim varArg As Variant
For Each varArg In FieldValues
If IsNumeric(varArg) Then
dblSum = dblSum + varArg
dblSumOfSq = dblSumOfSq + varArg * varArg
n = n + 1
End If
Next
If n > 0 Then 'only applies if points available
RStDevP = Sqr((n * dblSumOfSq - dblSum * dblSum) / n / n)
Else
RStDevP = Null
End If
End Function
I use 6 variables in the Sub Procedure on the form.
If I use 1, 2, 3 or 4 variables it returns a value that looks like a "Scientific" data type "ie: 5E-07"
If I use 5 or 6 variables it works perfectly except for one thing----if all the variables are the same which should give me an answer of 0.00000 I get an error in a line of the code in the macro. This part is giving me problems:
If n > 0 Then 'only applies if points available
RStDevP = Sqr((n * dblSumOfSq - dblSum * dblSum) / n / n)
Else
RStDevP = Null
End If
Can anyone look at it and help me with what is wrong?
Thanks in advance!
Function RStDevP(ParamArray FieldValues()) As Variant
Dim dblSum As Double, dblSumOfSq As Double
Dim n As Long
Dim varArg As Variant
For Each varArg In FieldValues
If IsNumeric(varArg) Then
dblSum = dblSum + varArg
dblSumOfSq = dblSumOfSq + varArg * varArg
n = n + 1
End If
Next
If n > 0 Then 'only applies if points available
RStDevP = Sqr((n * dblSumOfSq - dblSum * dblSum) / n / n)
Else
RStDevP = Null
End If
End Function