5.4.3.3 ReDim Statement

 redim-statement = "Redim" ["Preserve"] redim-declaration-list
  
 redim-declaration-list = redim-variable-dcl *("," redim-variable-dcl) 
 redim-variable-dcl = redim-typed-variable-dcl / redim-untyped-dcl / with-expression-dcl / member-access-expression-dcl
 redim-typed-variable-dcl = TYPED-NAME dynamic-array-dim 
 redim-untyped-dcl = untyped-name dynamic-array-clause 
 with-expression-dcl = with-expression dynamic-array-clause
 member-access-expression-dcl = member-access-expression dynamic-array-clause
  
 dynamic-array-dim = "(" dynamic-bounds-list ")" 
 dynamic-bounds-list = dynamic-dim-spec *[ "," dynamic-dim-spec ] 
 dynamic-dim-spec = [dynamic-lower-bound] dynamic-upper-bound 
 dynamic-lower-bound = integer-expression  "to"  
 dynamic-upper-bound = integer-expression 
  
 dynamic-array-clause = dynamic-array-dim [as-clause] 

Static Semantics.

§ Each <TYPED-NAME> or <untyped-name> is first matched as a simple name expression in this context.

§ If the name has no matches, then the <redim-statement> is instead interpreted as a <local-variable-declaration> with a <variable-declaration-list> declaring a resizable array with the specified name and the following rules do not apply.

§ Otherwise, if the name has a match, this match is the redimensioned variable.

§ A <redim-typed-variable-dcl> has the same static semantics as if the text of its elements were parsed as a <typed-variable-dcl>.

§ A <redim-untyped-dcl> has the same static semantics as if the text of its elements were parsed as an <untyped-variable-dcl>.

§ The declared type of the redimensioned variable MUST be Variant or a resizable array.

§ Any <as-clause> contained within a <redim-declaration-list> MUST NOT be an <as-auto-object>; it MUST be an <as-type>.

§ The redimensioned variable might not be a param array.

§ A redimensioned variable might not be a with block variable (section 5.4.2.21).

 

Runtime Semantics.

§ Runtime Error 13 is raised if the declared type of a redimensioned variable is Variant and its value type is not an array.

§ Each array in a <redim-statement> is resized according to the dimensions specified in its <bounds-list>. Each element in the array is reset to the default value for its data type, unless the word "preserve" is specified.

§ If the Preserve keyword is present, a <redim-statement> can only change the upper bound of the last dimension of an array and the number of dimensions might not be changed. Attempting to change the lower bound of any dimension, the upper bound of any dimension other than the last dimension or the number of dimensions will result in Error 9 (“Subscript out of range”).

§ If a <redim-statement> containing the keyword Preserve results in more elements in a dimension, each of the extra elements is set to its default data value.

§ If a <redim-statement> containing the keyword Preserve results in fewer elements in a dimension, the data value of the elements at the indices which are now outside the array’s bounds are discarded. Each of these discarded elements is set to its default data value before resizing the array.

§ If the redimensioned variable was originally declared as an automatic instantiation variable (section 2.5.1), each dependent variable of the redimensioned variable remains an automatic instantiation variable after execution of the <redim-statement>.

§ If the redimensioned variable is currently locked by a ByRef formal parameter runtime Error 10 is raised.