AnsweredAssumed Answered

How to pass arrays to/from functions

Question asked by Keaton Warn on May 6, 2019
Latest reply on May 6, 2019 by Keaton Warn

I'm trying to pass a a string array to a function, as well as return string arrays from functions to the main function. I'm having a heck of a time making it work. I was messing with examples/stuff I found online and I would've posted my code but apparently it didn't save even though the file system said it saved.


Any help/compilable examples would be appreciated. Thanks.


EDIT: wrote something that's kind of like what I had before.


Dim swApp As Object
Sub main()
    Set swApp = Application.SldWorks

    Dim myArray(4) As Variant
    Dim myArray2(4) As Variant

    myArray(0) = (1)
    myArray(1) = (2)
    myArray(2) = (3)
    myArray(3) = (4)

    For i = 0 To 3
        Debug.Print myArray(i)
    Next i
    'Dim myArray2(4) As Variant
    myArray2 = getStats(myArray) 'error can't pass to array

    'Pass myArray array to GetStats. Assign getStats array (11,12,13,14) to myArray array.
    'myArray2 = getStats(myArray)
    For i = 0 To 3
        Debug.Print "myArray2("; i; ") is:"; myArray2(i)
    Next i
    Debug.Print ""
End Sub
Function getStats() As Variant

    Dim myArrayVals(4) As String

    myArrayVals(0) = 11
    myArrayVals(1) = 12
    myArrayVals(2) = 13
    myArrayVals(3) = 14
    For i = 0 To 3
        Debug.Print "myArrayVals("; i; ") is: "; myArrayVals(i)
    Next i

    getStats = myArrayVals

End Function


Trying to at least define and pass an array from getStats. At most, pass myArray to getStats, manipulate it there, and then pass the resulting array back to Main.