问题
I have a function that takes in a ParamArray that I'm trying to pass an unknown number of parameters into. I'm looping through rows and passing in numbers based on if the cells are empty or not, but it seems like I have to pass in each number as its own argument. I tried putting the numbers into an array and passing that, but it just ended up being an array of an array in the function and not working properly. Is there a way to do this? Thank you.
Ex:
Dim myarray() as double
function test(ParamArray arg() as variant) as single
'function does stuff with arg(s)
end function
for each cell in [somerange]
if cell <> "" then
'save cell value into an myarray?
endif
next
'want to pass those saved values into function
call test(myarray)
Edit: I kind of found a workaround. I realized I can pass a range into the function so I'm just going to create a temporary range and pass that in.
回答1:
From Cpearson Passing And Returning Arrays With Functions, this is how you pass an array into a function and loop that array:
Sub DoSomethingWithPassedArray(ByRef Arr() As Long)
Dim N As Long
For N = LBound(Arr) To UBound(Arr)
'...do something
Next N
End Sub
Further on, it's not clear what you want to do...
回答2:
Here it is (it is related with my question in Calling vba macro from python with unknown number of arguments)
Use this:
Sub pass_this()
Call flexible("a")
End Sub
Sub pass_alsothis()
Call flexible("a", 2)
End Sub
Sub flexible(ParamArray args() As Variant)
Dim i As Long
MsgBox ("I have received " & _
Str(UBound(args) + 1) & _
" parameters.")
For i = 0 To UBound(args)
MsgBox (TypeName(args(i)))
Next i
End Sub
Cheers.
来源:https://stackoverflow.com/questions/28704950/passing-an-unknown-number-of-arguments-into-paramarray-in-vba