2

In excel-vba, how do I pass a paramarray into a function which expects the same paramarray? I've been looking around but can't seem to find any answers to this specific problem

My code looks like this:

Sub test()
    GetRefData "SPX Index", "PX_LAST", "PX_VOLUME", "PE_RATIO", "PX_TO_CASH_FLOW"
End Sub

Public Function GetRefData(security As String, ParamArray fields() As Variant) As String
    Debug.Print fields(0) // returns "PX_LAST"
    SendReq security, fields
End Function

Sub SendReq(security, ParamArray fields() As Variant)
    Debug.Print fields(0) // returns type mismatch
End Sub
5
  • I'm not sure what your problem is. If I remove the brackets around the call to GetRefData in Sub test, then I can put a breakpoint in SendReq and the fields parameter has 4 items with the expected string values. Commented Jan 10, 2017 at 6:52
  • Hi DeanOC, thank you for your reply. Are you saying that the code works when you run it? For example, if I use Debug.Print fields(0) when I am in SendReq, VBA returns a 'type mismatch' error Commented Jan 10, 2017 at 6:58
  • 1
    See "Passing a ParamArray variable to another procedure" here: tushar-mehta.com/publish_train/xl_vba_cases/… Commented Jan 10, 2017 at 7:15
  • Thanks Tim Williams! Commented Jan 10, 2017 at 8:01
  • Another Q&A with more approaches: stackoverflow.com/questions/20783170/pass-array-to-paramarray Commented Sep 11, 2024 at 15:09

1 Answer 1

1

After looking up the article Tim posted, the solution was that paramarrays have to be accessed one level deeper when passed as a parameter, ie:

Sub SendReq(security, ParamArray fields() As Variant)
    Debug.Print fields(0)(0)
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Yep! The second fields(0) (0) parameter did the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.