Understanding Parameter Arrays

Microsoft VBA Tips

Understanding Parameter Arrays

   

A parameter array can be used to pass an array of arguments to a procedure. You don't have to know the number of elements in the array when you define the procedure.

You use the ParamArray keyword to denote a parameter array. The array must be declared as an array of type Variant, and it must be the last argument in the procedure definition.

The following example shows how you might define a procedure with a parameter array.

Sub AnyNumberArgs(strName As String, ParamArray intScores() As Variant)
    Dim intI As Integer

    Debug.Print strName; "    Scores"
    ' Use UBound function to determine upper limit of array.
    For intI = 0 To UBound(intScores())
        Debug.Print "          "; intScores(intI)
    Next intI
End Sub

The following examples show how you can call this procedure.

AnyNumberArgs "Jamie", 10, 26, 32, 15, 22, 24, 16

AnyNumberArgs "Kelly", "High", "Low", "Average", "High"