清除数组的最快方法是ReDim(如果数组是动态的)或擦除它(如果数组是静态的)。但是,如果要清除一部分对于数组,似乎必须编写一个For Next循环。
如果您正在处理数字数组,有一种更快的替代方法,它基于ZeroMemory API函数,顾名思义,该函数可以填充带有零的内存区域。查看如何使用此函数清除Longs的子数组:
新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (dest As Any, ByVal Bytes As Long)
Private Sub Command1_Click()
' Declare an array and fill it with data
Dim a(1000) As Long
For i = 1 To UBound(a)
a(i) = i
Next
MsgBox a(5) '显示5
' Clear 100 elements, starting at a(200)
ZeroMemory a(200), 100 * Len(a(1))
MsgBox a(299) '显示0
MsgBox a(300) '显示300
End Sub