Not every array can be redimensioned. This error has the following causes and solutions:
A Variant can contain an array, but if it isn't explicitly declared, you can't use ReDim to make it into an array. Declare the Variant before using ReDim to specify the number of elements it can contain. For example, in the following code, ReDim AVar(10)
causes an invalid ReDim error, but ReDim BVar(10)
does not:
AVar = 1 ' Implicit declaration of AVar.
ReDim AVar(10) ' Causes invalid ReDim error.
.
.
.
Dim BVar ' Explicit declaration of BVar.
ReDim BVar(10) ' No error.
You can only use ReDim to change the size of the last dimension of an array in a Variant. To create an array with multiple dimensions that can be redimensioned, the array can't be contained within a Variant, and you have to declare it the normal way.
If you want an array in which you can change the types of the elements, use an array contained within a Variant. If you declare the array first, changing the types and the number of its elements can be accomplished as follows:
Dim MyVar As Variant ' Declare the variable.
ReDim MyVar(10) As String ' ReDim it as array of String subtypes.
ReDim MyVar(20) As Integer ' ReDim it as array of Integer subtypes.
ReDim MyVar(5) As Variant ' ReDim it as array of Variant subtypes.
Remove the ReDim.
Note If you don't specify a type for a variable, the variable receives the default type, Variant. This isn't always obvious. For example, the following code declares two variables, the first, MyVar
, is a Variant; the second, AnotherVar
, is an Integer.
Dim MyVar, AnotherVar As Integer
For additional information, select the item in question and press F1.