1

I want to build function in VBA that build an array and return it to array that's in another sub so I can use this function in multiple places. For example:

Sub getArrayData ()
 Dim myItemsArr() As String ' dynamic array
 myItemsArr = functionToFillArray
End sub

And let's assume this is the function

Function functionToFillArray(arrayIWantToFill as Variant) 
 arrayToFill = ("A","B","C")
 arrayIWantToFill  = ???
 functionToFillArray = ?? ' what should i do here to return the array i want to fill
End function

At other language suck as Java its more clear to me how to do it, but in VBA I just don't understand how its work yet. I try to find some information online, but without success

1
  • It's not really clear what you want your function to do. Your two bocks of code are not in agreement. Your sub passes no argument to the function, but the function expects one. Commented Dec 16, 2020 at 7:41

1 Answer 1

1

Like this:

Function getArrayData() As Variant

    Dim myItemsArr As Variant

    ' Fill array.
    functionToFillArray myItemsArr

    getArrayData = myItemsArr

End Function


Sub functionToFillArray(arrayIWantToFill As Variant) 

    arrayIWantToFill = Array("A","B","C")
 
End Sub
Sign up to request clarification or add additional context in comments.

Comments

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.