PDA

View Full Version : [SOLVED] Dynamic Arrays Question



MWE
08-22-2005, 06:20 AM
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).

BlueCactus
08-22-2005, 06:34 AM
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

MWE
08-22-2005, 07:39 AM
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:dunno:dunno

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.