Consulting

Results 1 to 3 of 3

Thread: Solved: ReDim (Preserve) within procedure argument passed ByRef

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location

    Solved: ReDim (Preserve) within procedure argument passed ByRef

    Is it possible to ReDim an array within a procedure passing it by reference? Obviously the array was declared outside the procedure.

    Thanks!

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.

  3. #3
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location
    Thanks Oorang!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •