Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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!

like image 938
danielphili Avatar asked Dec 21 '25 00:12

danielphili


1 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
like image 174
JohnyL Avatar answered Dec 22 '25 16:12

JohnyL