Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following workarounds assume that you created a Visual Basic module
with the following code:
Dim MyArray() As Integer
Sub Macro1() 'The first macro
ReDim MyArray(10)
Macro2 MyArray(1)
End Sub
Sub Macro2(X) 'The second macro
ReDim MyArray(20)
End Sub
The array MyArray is initialized at the module level; so, it is available
to all macros within the module. The first macro redimensions the array,
and then passes an element of the array to the second macro. The second
macro refers to the element in the array by reference, not by value.
NOTE: If you run Macro1, you receive the error message.
To prevent the problem from occurring, use either of the following methods.
Method 1
In the second macro, use the keyword ByVal to force the macro to refer to
the element in the array by value, not by reference. For example, change
the second macro to the following:
Sub Macro2(ByVal X) 'Note the "ByVal" keyword.
ReDim MyArray(20)
End Sub
Method 2
Use an intermediate variable to pass the value from one macro to the other.
For example, change the first macro to the following:
Sub Macro1()
ReDim MyArray(10)
Z = MyArray(1) 'Set the intermediate variable value.
Macro2 Z 'Run the second macro and pass the value.
End Sub
By using an intermediate variable to send the value to the second macro,
you prevent the second macro from referring to locations within the array's
memory. You can then redimension the array without receiving an error
message.