Dear John, How Do I... Pass Parameter Arrays?

If you declare the last parameter in the parameter list of a procedure as a Variant array with the ParamArray keyword, you can pass a flexible number of arguments. The following code demonstrates this by converting any number of arguments to a vertically formatted list of strings:

Option Explicit

Private Function MakeVerticalList(ParamArray vntN()) As String
    Dim strA As String, i As Integer
    For i = LBound(vntN) To UBound(vntN)
        strA = strA + vbCrLf + CStr(vntN(i))
    Next i
    MakeVerticalList = strA
End Function

Private Sub Form_Click()
    Dim intA As Integer
    Dim sngB As Single
    Dim strC As String
    intA = 123
    sngB = 3.1416
    strC = "This is a test."
    Print MakeVerticalList(intA, sngB, strC)
    Print
    Print MakeVerticalList("This", "time", "we'll", "pass five", _
        "string arguments.")
End Sub

Notice that I called MakeVerticalList twice, the first time with a variety of types of arguments—three in all—and the second time with five string arguments.

Figure 4-1 shows the results displayed when you click on the form.

Figure 4-1. Results of passing first three and then five arguments to the MakeVerticalList function.