1

I have been combing the internet for an answer to this and can't find anything. For the life of me, I can't return a string array from a function. I've tried as a variant as well, but all I get is syntax errors.

Private Function set_device_list(ByVal deviceListSize As Integer) As Variant()



    Dim loopIndex As Integer
    loopIndex = 0

    Dim firstRow As Integer
    Dim lastRow As Integer
    Dim firstColumn As Integer
    Dim lastColumn As Integer

    firstRow = 2
    lastRow = 2
    firstColumn = 4
    lastColumn = 63

    Dim deviceStartIndex As String
    Dim deviceEndIndex As String

    Dim deviceList() As Variant
    ReDim deviceList(0 To (deviceListSize - 1))

    deviceStartIndex = Cells(firstRow, firstColumn)
    deviceEndIndex = Cells(lastRow, lastColumn)

    For i = firstColumn To lastColumn

        deviceList(loopIndex) = Cells(firstRow, i).Value

        loopIndex = loopIndex + 1
        Next i
    Return deviceList

End Function
5
  • 1
    VBA doesnt use return. Use set_device_list = deviceList Commented Jul 6, 2017 at 14:48
  • MacroMarc is absolutely right, see stackoverflow.com/a/2781710/5962841 Commented Jul 6, 2017 at 14:52
  • I'm sorry, what? So to return the string array I just put set_device_list = deviceList? I get an error in the calling function now, when I try to set a stringArray = set_device_list(num) Commented Jul 6, 2017 at 14:52
  • have to handle it as a variant, or loop over it and assign every item to the string array. yes. Commented Jul 6, 2017 at 14:55
  • I am OK with handling it as a Variant, but then how would I access the data? Thanks again. Commented Jul 6, 2017 at 14:56

3 Answers 3

1

Just use set_device_list = deviceList, this is the vba equivalent to the return expression of most c like languages.

To assign it to a string array, do this:

Dim myArray() As String
Dim device_list() as Variant

device_list = set_device_list(yourNumber)

For i = 1 To UBound(device_list)
  myArray(i) = CStr(device_list(i))    
Next
Sign up to request clarification or add additional context in comments.

13 Comments

Thank you. But now in the calling function it says "compile error: can't assign to array"
Thank you so much!! That works :) Is a function returning an Integer array OK or do I have to use the same trick with variants?
@Bob glad I could help. Please consider accepting the answer as we normally do on stack overflow when something solved your problem(s)
Sure thing I will do that. Any chance I can get some clarification on the edit I just made?
I'm getting syntax errors when trying to do this with an array of Integers
|
0

Please drop the parentheses at the end of function declaration: "...As Variant" not "...As Variant()"

Try instead of "Return deviceList" enter "set_device_list = deviceList"

3 Comments

Well either it is a Variant() where every object is a string, or it is a Variant that represents a string array. IIRC both work, is that right?
Thank you. But now in the calling function it says "compile error: can't assign to array" – Bob just now edit
Bob, the compilation error is obviously due to something else. Can you post the "NEW" code?
0

Actually there is no actual "return" function in VBA. What you can do is that you assign the result of the function to the variable.

For example:

Dim test As Integer
Dim dummy As Integer

Function do_something(parameter) As Integer
      For i = parameter to 5
            dummy = dummy + i
      Next i
do_something = dummy
End Function

Sub your_sub
    Dim test_var As Integer

    test_var = do_something(0)
End Sub

I hope that you can understand what I'm trying to tell

2 Comments

Technically there is a Return keyword in VBA. It's used to return from a GoSub jump, a piece of legacy that predates Sub procedures.
That's correct, but I was trying to answer him to his context ("return a string array from a function")

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.