Using the Erase statement to clear an array

Article contributed by Ibby

Clearing the data in an array can be done by looping through the elements in the array and clearing each one separately. However, it is much easier to use the Erase statement, which can be used to clear all the data in several arrays at the same time. The individual array variables need to be separated by commas:

Erase array1, array2, array3 ........

The behaviour of the Erase statement varies with the type of array as follows:

Dynamic Array (an array dimensioned with a Redim statement): Clears all the elements of the array. The array needs to be redimensioned using a ReDim statement before it can be filled with data again.

Static Array (an array dimensioned with a Dim statement): Clears all the data from the array, leaving the elements in place. The elements are filled with data as follows (from the Help file):

Type of Array

Effect of Erase on Fixed-Array Elements

Fixed numeric array

Sets each element to zero

Fixed string array (variable length)

Sets each element to a zero-length string ("")

Fixed string array (fixed length)

Sets each element to zero

Fixed Variant array

Sets each element to Empty

Array of user-defined types

Sets each element as if it were a separate variable

Array of objects

Sets each element to the special value Nothing


Click to view Terms of Use page

Click to view Disclaimer page