Consulting

Results 1 to 3 of 3

Thread: Dynamic Arrays Question

  1. #1
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location

    Dynamic Arrays Question

    I have a question about dynamic arrays:

    assume strX is a dynamic array of type string and is dimed in Sub A. Sub A calls Sub B and one of the passed arguements is strX. For example:

    Sub A
    Dim strX() as string
    Call Sub B ( ... , strX , ...)
    End Sub
    Obviously strX can be ReDimed in Sub A, but can it be ReDimed in Sub B? VBA Help states "Used at procedure level to reallocate storage space for dynamic array variables." But it is not clear that this means "only at the "level" where the variable was initially declared". Trying to do this does not create a "compile" error, but does create an execution error (at least the way(s) I have tried doing this).
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Yes you can, as long as the array is passed ByRef (the default).

    For example, the following code gives '20':

    Sub test()
        Dim strx() As String
        ReDim strx(1 To 10)
    Call test2(strx)
        MsgBox UBound(strx)
    End Sub
    
    Sub test2(ByRef stry() As String)
        ReDim stry(1 To 20)
    End Sub

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by BlueCactus
    Yes you can, as long as the array is passed ByRef (the default).

    For example, the following code gives '20':
    Sub test()
        Dim strx() As String
        ReDim strx(1 To 10)
    Call test2(strx)
        MsgBox UBound(strx)
    End Sub
    
    Sub test2(ByRef stry() As String)
        ReDim stry(1 To 20)
    End Sub
    Thanks. I thought that was the case, but my example was not working. I copied your example onto my system, it worked. I then tried my example again and it worked

    I have encountered this anomolous behavior quite a few times in the last week or so. Perhaps it is time for me to reload MS Office.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

Posting Permissions

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