ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Pass "array of strings" into a function?? (https://www.excelbanter.com/excel-programming/444234-pass-array-strings-into-function.html)

Robert Crandal[_2_]

Pass "array of strings" into a function??
 
I thought this would be easy to solve, but I am having trouble
finding information on how to define a VBA function (or sub)
that has an "array of strings" as an argument. Here is my
defintion so far:

Sub Sort_Array_of_Strings (ByRef Arr() As String)

' stuff in here

End Sub


Whenever I try passing an array of strings into this subroutine
I get a "type mismatch" type of error message.

Can anyone show me how to correctly pass an array of
strings into a function or sub? How do I define the function as well?

Thank you!



Rick Rothstein

Pass "array of strings" into a function??
 
Can anyone show me how to correctly pass an array of strings
to a function or sub? How do I define the function as well?


Here is an example for you to study (the subroutine names should tell you
what you need to know)...

Sub SubWithArrayArgument(Arr() As String)
MsgBox "UpperBound = " & UBound(Arr) & vbLf & "Last element = " &
Arr(UBound(Arr))
End Sub

Sub CallSubWithArrayArgument()
Dim MyArr() As String
MyArr = Split("one,two,three,four", ",")
SubWithArrayArgument MyArr
End Sub

Just run the CallSubWithArrayArgument subroutine (which could be a macro if
need be). It will create the MyArr array inside itself and then pass it to
the SubWithArrayArgument subroutine which will display the index for the
last element in that passed in array and also the value in that last
element. Obviously, the setup and passing mechanism would be the same for
functions as well. Hope this helps.

Rick Rothstein (MVP - Excel)



All times are GMT +1. The time now is 09:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com