Name

Erase Statement

Syntax

Erase arraylist
arraylist

Use: Required

Datatype: Variant array

A list of array variables to clear.

Description

Resets the elements of an array to their initial (unassigned) values. In short, Erase “clears out” or empties an array.

Rules at a Glance

  • Specify more than one array to be erased by using commas to delimit arraylist.

  • Fixed array variables remain dimensioned; on the other hand, all memory allocated to dynamic arrays is released.

  • After the Erase statement executes, TypeName returns “Variant( )” for a fixed-length array; in addition, the IsEmpty function returns True when individual members of the array are passed to it, and comparisons of an individual member of the array with a null string (“”) and with zero both return True. On the other hand, the TypeName function returns Empty for a dynamic array, and comparisons of the array with a null string (“”) and zero also return True.

Programming Tips & Gotchas

Once you use Erase to clear dynamic arrays, they must be redimensioned with ReDim before being used again. This is because Erase releases the memory storage used by the dynamic array back to the operating system, which sets the array to have no elements.

VBA/VBScript Differences

Because VBA can be strongly typed, the behavior of the Erase statement in clearing a fixed array varies, depending on the array’s datatype. The effect of the Erase statement on a fixed variant array in VBScript is described earlier in the “Rules at Glance” section.

Get VBScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.