VBA Erase()函式

2019-10-16 23:13:11

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