FixedArr
is received by SomeArr
in the NextOne
procedure, and then an attempt is made to resize SomeArr
:
Sub FirstOne Dim FixedArr(25) As Integer ' Create a fixed-size array and NextOne FixedArr() ' pass it to another procedure. End Sub Sub NextOne(SomeArr() As Integer) ReDim SomeArr(35) ' Error 10 occurs here. . . . End Sub
Make the original array dynamic rather than fixed by declaring it with ReDim (if the array is declared within a procedure), or by declaring it without specifying the number of elements (if the array is declared at module level).
ModArray
is a dynamic, module-level array whose forty-fifth element is being passed by reference to the Test
procedure:
Dim ModArray () As Integer ' Create a module-level dynamic array. . . . Sub AliasError() ReDim ModArray (1 To 73) As Integer Test ModArray (45) ' Pass an element of the module-level ' array to the Test procedure. End Sub Sub Test(SomeInt As Integer) ReDim ModArray (1 To 40) As Integer ' Error occurs here. End Sub
There is no need to pass an element of the module-level array in this case, since it's visible within all procedures in the module. However, if an element is passed, the array is locked to prevent a deallocation of memory for the reference parameter within the procedure, causing unpredictable behavior when the procedure returns.
SomeArray = Array(9,8,7,6,5,4,3,2,1) For Each X In SomeArray SomeArray = 301 ' Causes error since array is locked. Next X
Use a For...Next rather than a For Each...Next loop to iterate. When an array is the object of a For Each...Next loop, you can read the array, but not write to it.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
For Each...Next Statement | For...Next Statement | ReDim Statement | Sub Statement | Variant Data Type