Erase Statement

See Also149YHMZ                 Example5A4IQ3>Low

Reinitializes the elements of fixed arraysYPCGZO and deallocates dynamic-array storage space.


Erase arrayname [, arrayname] . . .


The argument arrayname is the name of the array to be erased.  It is important to know whether an array is fixed (ordinary) or dynamic because Erase behaves differently depending on the type of array.  For fixed arrays, no memory is recovered. Erase sets the elements of a fixed array as follows:


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 zero-length ("")

Fixed string array (fixed-length)

Sets each element to zero

Fixed Variant8PHEAW3 array

Sets each element to Empty1L2JEZ4

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


For dynamic arrays,  Erase frees the memory used by the array.  Before your program can refer to the dynamic array again, it must redeclare the array variable's dimensions using a ReDim statement.

See Also

Dim StatementLANDIM

ReDim StatementQ1CGU1

Erase Statement Example

In this example, a two-dimensional array is created, filled, and erased.  When the Erase statement is executed, zeros replace the contents of each element, but no memory space is recovered.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Static Sub Form_Click ()

  Dim I As Integer, J As Integer   ' Declare Variables.

  Dim Total As Long

  Dim Matrix(50, 50) As Integer    ' Create 2-D integer array.

  For I = 1 To 50

    For J = 1 To 50

    Matrix(I, J) = J               ' Put some values into array.

    Next J

  Next I

  Erase Matrix                     ' Erase array.

  Total = 0                        ' Initialize total counter.

  For I = 1 To 50

    For J = 1 To 50

      Total = Total + Matrix(I, J) ' Sum elements after Erase

    Next J                         ' to make sure all are set

  Next I                           ' to zero.


  Msg = "An array has been created, filled, and erased. When the "

  Msg = Msg & "Erase statement was executed, zeros replaced the "

  Msg = Msg & "previous contents of each element.  The total of "

  Msg = Msg & "all elements is now " & Total & ", "

  Msg = Msg & "demonstrating that all elements have been cleared."

  MsgBox Msg                       ' Display message.

End Sub