VBA——Erase 函式



Erase 函式用於重置固定大小陣列的值和釋放動態陣列的記憶體。它會根據陣列型別採取不同的行為。

語法

Erase ArrayName
  • 固定數值陣列,陣列中的每個元素都重置為零。
  • 固定字串陣列,陣列中的每個元素都重置為零長度“”。
  • 物件陣列,陣列中的每個元素都重置為特殊值“無”。

示例

新增按鈕並新增以下函式。

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_arrays.htm
廣告
© . All rights reserved.