Declaring


Fixed Array - Explicit Data Type Array

You can declare a fixed array using the Dim statement.
It is good practice to always declare your arrays with an explicit data type.
This line creates an array of Integers with 4 items.
All VBA arrays (by default) start at zero.

Dim myArray(3) As Integer 

Debug.Print myArray(0) '= 0
Debug.Print myArray(1) '= 0
Debug.Print myArray(2) '= 0
Debug.Print myArray(3) '= 0

Fixed Array - Variant Data Type Array

This line creates an array of Variant with 4 items.

Dim myArray(3) As Variant 

Debug.Print myArray(0) '= Empty
Debug.Print myArray(1) '= Empty
Debug.Print myArray(2) '= Empty
Debug.Print myArray(3) '= Empty

Fixed Array - Variant Data Type

You can declare a fixed array as a single variant data type and then use the ARRAY function.
This function declares and adds the values in one statement.

Dim avNumbers As Variant 
avNumbers = Array(10,20,30)

An array created with a single variant data type will always create an array of Variants.


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