VBA Erase函數

Erase 函數是用來複位固定尺寸數組的值,並釋放該動態陣列的存儲器。它的行爲取決於陣列的類型。

語法

Erase ArrayName

  1. 固定數值數組,數組中的每個元素重置了零。

  2. 固定字符串數組,數組中的每個元素都被重置了零長度 " ".

  3. 對象數組,數組中的每個元素重置了特殊值無。

例子 :

添加一個按鈕,並添加以下功能

Private Sub Constant_demo_Click() Dim NumArray(3) NumArray(0) = "VBScript" NumArray(1) = 1.05 NumArray(2) = 25 NumArray(3) = #23/04/2013# Dim DynamicArray() ReDim DynamicArray(9) ' Allocate storage space.

Erase NumArray ' Each element is reinitialized. Erase DynamicArray ' Free memory used by array.

' All values would be erased. msgbox("The value at Zeroth index of NumArray is " & NumArray(0)) msgbox("The value at First index of NumArray is " & NumArray(1)) msgbox("The value at Second index of NumArray is " & NumArray(2)) msgbox("The value at Third index of NumArray is " & NumArray(3)) End Sub

當執行函數輸出如下所示:

The value at Zeroth index of NumArray is
The value at First index of NumArray is
The value at Second index of NumArray is
The value at Third index of NumArray is