Declaring

You can declare a dynamic array using the Dim statement.
When you need to define the size of the array you can use the ReDim statement.
You can declare a dynamic array by using an empty set of parentheses.
Dynamic arrays are always declared with empty parentheses.
The parentheses must always be attached to the variable, never the data type.


Dynamic Array - Explicit Data Type Array

These two lines create an array that can contain 4 string elements.

Dim arValues() As String     'this is a String Array  
ReDim arValues(3)

Dynamic Array - Variant Data Type Array

These two lines create an array that can contain 4 elements of any data type.

Dim arValues() As Variant    'this is a Variant Array  
ReDim arValues(3)

Dynamic Array - Variant Data Type

A Variant that is not declared as an array can still contain an array.
The first line creates a single variable (not an array) that can contain any data type.
The second line declares an array containing 4 elements and assigns it to this single variable.

Dim arValues As Variant      'this is just a Variant  
ReDim arValues(3)

Declaring your array as a single variable and then creating the array when the array is resized is More Advanced


Default Values

After declaring an array it will contain default values.
Numeric arrays will contain the number 0.
String arrays will contain the Empty String "".
Variant arrays will contain the Empty keyword.
Object arrays will contain the Nothing keyword.



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext