Monday, June 8, 2020

VBA ParamArray keyword usage

Option Explicit
''Application of ParamArray keyword
Function SumArrayData(ParamArray numbers() As Variant) As Double
    ''dynamic array is passed as argument
    Dim dSum As Double
    Dim i As Long
    
    'LBound of a ParamArray is always 0.
    'Each element of the ParamArray is added here:
    For i = LBound(numbers) To UBound(numbers)
        dSum = dSum + numbers(i)
    Next i
    SumArrayData = dSum
    
End Function

Sub AddArrayElementsTest()
    Debug.Print SumArrayData(22, 25, 30, 40)    ''array size = 4
    Debug.Print SumArrayData(2.32, 2.55, 30.25, 40.55, 5.25, 10.5, 7.52, 2.25) ''array size = 8
End Sub

NOTE: The array must be declared as an array of type Variant, and it must be the last argument in the procedure definition.

Details at Microsoft site

No comments:

Post a Comment

Hot Topics