I used VBA to write a homemade function to calculate the length of a vector (Euclidean distance), and currently it works for any range selected for function parameter.
Function VectorLength(Numbers1 As Range) As Double
Dim i As Range
Dim val As Double
For Each i In Numbers1
val = val + i * i
Next
VectorLength = Math.Sqr(val)
End Function
Here, I want to generalize it to work with separate cells (non-connected cells, e.g. A3:A5, A7, A9) or double type values (e.g. 3.02, 1E-5, A3:A5).
But, for example, when I try to change to separate cells where I need to set the number of input parameters as variant, I used ParamArray. However, the code below comes to an error when running.
Function VectorLength2(ParamArray Numbers2() As Variant) As Double
Dim j As Long
Dim k As Variant
For j = LBound(Numbers2) To UBound(Numbers2)
For Each k In Numbers2(j)
val = val + k * k
Next k
Next j
VectorLength = Math.Sqr(val)
End Function
Well, my main aim is to imitate excel pre-defined SUM or AVERAGE function, with the input parameters being any of the double type values, separate cells or range.
Many thanks!