Is it possible to pass the fields of an array to a function with variant parameter array?

Viewed 113

I am in the following situation: I have a function, that takes a ParamArray of type variant and generates a string from the keywords given in its ParamArray in a special manner by execution mergeToString.

Function function1(ParamArray var() As Variant) As String
    For i = LBound(var) To UBound(var)
       function1 = mergeToString(function1, CStr(var(i))
    Next i
End Function

In another subroutine, I have an array of strings obtained from the Split Function in VBA and want to use it as an input for function1

Sub displayFCTN1()
    Dim arr() As String
    arr() = Split("foo|bar", "|")
    'and here I ran out of ideas...
    Debug.Print function1(**???**)
End Sub

The two lines

function1(**???**)
function1("foo","bar")

should be equivalent the first somehow using arr().

In Matlab this is relatively easy - I know, VBA is not Matlab, still this might help as an extended description of my problem: you could most likely do it by using the colon operator in Matlab

function1(arr(:))

since then the fields of the array arr() count as "free" parameters.

Is there something comparable to this in VBA? I tried ReDim already, that somehow didn't do the job (as far as I tried).

Thank you for your help!

3 Answers

You need to test, whether the first item of array is array:

Sub FFF()
    MsgBox Func1("foo", "bar")
    MsgBox Func1(Split("foo|bar", "|"))
End Sub

Function Func1$(ParamArray var() As Variant)
    Dim s$, x%, args
    args = IIf(IsArray(var(0)), var(0), var)
    '//Do something
    For x = 0 To UBound(args)
        s = s & args(x) & "|"
    Next
    Func1 = Left$(s, Len(s) - 1)
End Function

A workaround as mentioned in the comments above

Sub displayFCTN1()
        Dim arr() As String
        arr() = Split("foo|bar", "|")
        Myhelper arr
    End Sub

    Sub Myhelper(arr)
    Select Case UBound(arr)
    Case 0: Debug.Print function1(arr(0))
    Case 1: Debug.Print function1(arr(0), arr(1))
    Case 2: Debug.Print function1(arr(0), arr(1), arr(2))
    Case 3: Debug.Print function1(arr(0), arr(1), arr(2), arr(3))
    Case 4: Debug.Print function1(arr(0), arr(1), arr(2), arr(3), arr(4))
    'etc up to 29.
    Case Else
    End Select
    End Sub

This does require a change to function1 code, but should still work with orginal.

Sub Test()

    Debug.Print function1("foo", "bar")

    Dim arr() As String
    arr = Split("foo|bar", "|")
    Debug.Print function1(arr)

End Sub

Function function1(ParamArray var() As Variant) As String
    Dim i As Long

    If UBound(var) = 0 Then
        For i = LBound(var(0)) To UBound(var(0))
            'function1 = Join(var(0), "|")
            function1 = mergeToString(function1, CStr(var(0)(i)))
        Next i
    Else
        'Original code.
        For i = LBound(var) To UBound(var)
           'function1 = Join(var, "|")
           function1 = mergeToString(function1, CStr(var(i)))
        Next i
    End If
End Function
Related