Is it possible to ReDim an array within a procedure passing it by reference? Obviously the array was declared outside the procedure.
Thanks!
Is it possible to ReDim an array within a procedure passing it by reference? Obviously the array was declared outside the procedure.
Thanks!
In short... Yes that will work fine Here is an example to play with.
[vba]Option Explicit
Public Sub Example()
Dim strLocalArray() As String
ReDim strLocalArray(2) As String
strLocalArray(0) = "Foo"
strLocalArray(1) = "Bar"
strLocalArray(2) = "Baz"
DisplayArray strLocalArray, "This is the array before we pass it:"
AddElement strLocalArray
DisplayArray strLocalArray, "This is the redimmed array while in original scope:"
End Sub
Private Sub AddElement(ByRef myArray() As String)
ReDim Preserve myArray(3)
myArray(3) = "Pie"
DisplayArray myArray, "This is the redimmed array while in new scope:"
End Sub
Private Sub DisplayArray(display() As String, title As String)
Dim strMsg As String
Dim lngIndx As Long
strMsg = title
For lngIndx = LBound(display) To UBound(display)
strMsg = strMsg & (vbNewLine & Format$(lngIndx, "(0)=") & display(lngIndx))
Next
MsgBox strMsg, vbInformation, title
End Sub[/vba]
Cordially,
Aaron
Keep Our Board Clean!
- Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
- Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.
Thanks Oorang!