我想将数组中的值传递给具有ParamArray的函数,就像我分别从数组中传递每个值一样。
简化示例以进一步解释我的问题:
Sub DoSomething()
Dim MyArray(3) As Variant
MyArray(0) = "A"
MyArray(1) = "Whatever"
MyArray(2) = 2
MyArray(3) = "xyz"
'Now I want this...
SomeNativeFunction MyArray
'...to act the same way like this:
SomeNativeFunction MyArray(0), MyArray(1), MyArray(2), MyArray(3)
'... because at this point I don't know how many elements MyArray actually might contain during runtime, so my only chance by now is something like this:
Select UBound(MyArray)
Case 1
SomeNativeFunction MyArray(0)
Case 2
SomeNativeFunction MyArray(0), MyArray(1)
Case 3
SomeNativeFunction MyArray(0), MyArray(1), MyArray(2)
Case .....
Case 99999
SomeNativeFunction MyArray(0), MyArray(1), MyArray(2), MyArray(...)
End Select
End Sub
Function SomeNativeFunction (ParamArray args() As Variant)
'can't touch this...
End Function
在上面的示例中,我首先调用SomeNativeFunction
时调用MyArray
的方式将移至args
中的位置0,因此args(0) = MyArray
。但是相反,我需要args(0) = MyArray(0)
,args(1) = MyArray(1)
等,就像我在第二个函数调用中所做的一样。问题是,MyArray可能具有不同数量的参数,因此硬编码实际上并不能在所有情况下都起作用,或者至少会变得非常混乱。
我想用这种方式调用的子功能是由Excel或其他来源提供的,因此无法更改它们。