VBA Erase()函數

Erase()函數用於重置固定大小數組的值並釋放動態數組的記憶體。 它的行為取決於數組的類型。

語法

Erase ArrayName
  • 固定數值數組,數組中的每個元素重置為零。
  • 固定字串數組,數組中的每個元素被重置為零長度""
  • 對象數組,數組中的每個元素被重置為特殊值Nothing

例子

添加一個模組,並添加以下代碼 -

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

上一篇: VBA數組 下一篇: VBA用戶自定義函數