位置:首页 » 文章/教程分享 » 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