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 aStrings() As String      'this is a String Array  
ReDim aStrings(3)

Dynamic Array - Variant Data Type Array

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

Dim aVariants() As Variant    'this is a Variant Array  
ReDim aVariants(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 avNumbers As Variant      'this is just a Variant  

ReDim avNumbers(2) 'creates an array of Variants
ReDim avNumbers(3) As Long 'creates an array of Longs

This approach declares your array as a single variable and then creates the array.
When you declare as a Variant Data Type you can define the data type when the array is created.


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.


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