Reinitialises the elements of an array.

arraylistThe name of the array.

* The "arraylist" can be a single array variable or can be multiple array variables, separated by a comma.
* This statement behaves slightly differently depending on the type of array (either a fixed size or dynamic). No memory is recovered.
* After running this statement a fixed size (or static) array will be filled with blank elements.
* After running this statement a dynamic array will need the REDIM statement run again before any elements can be added.
* This will delete all the current elements and replaces them with either 0, zero length string (""), Empty or Nothing.
* If the array is an array of Variants then the elements are set to Empty.
* If the array is an array of Objects then the elements are set to Nothing.
* It is possible to resize a dynamic array without losing the existing values by using the Preserve keyword.
* You can use the ARRAY function to return an array containing specific values.
* You can use the ISEMPTY function to return the value True or False depending if the Variant variable has been initialised (Boolean).
* You can use the REDIM statement to initialise and resize a dynamic size array.
* For more information, refer to the Arrays > Erase Statement page.
* For the Microsoft documentation refer to

Dim myArray1(2) As Integer            'Array of Integers  
myArray1(0) = 10
myArray1(1) = 20
myArray1(2) = 30
Erase myArray1
Debug.Print "'" & myArray1(2) & "'" '= 0

Dim myArray2(3) As String 'Array of Strings
myArray2(0) = "first"
myArray2(1) = "second"
myArray2(2) = "last"
Erase myArray2
Debug.Print "'" & myArray2(2) & "'" '= Zero length string

Dim myArray3(3) As Variant 'Array of Variants
myArray3(0) = 10
myArray3(1) = "second"
myArray3(2) = False
Erase myArray3
Debug.Print IsEmpty(myArray3(2)) '= Empty

Dim myArray4(3) As Object 'Array of Objects
Set myArray4(0) = New Collection
Set myArray4(1) = New Collection
Set myArray4(2) = New Collection
Erase myArray4
Debug.Print (myArray4(2) Is Nothing) '= Nothing

Dim myArray5 As Variant 'Variant can contain an array
myArray5 = Array(1, 2, 3, 4, 5)
Erase myArray5 'Variants are reset completely

'you can reinitialise more than one array at a time
Erase myArray1, myArray2, myArray3

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top