如何清除整个arrays?

我有一个这样的数组:

Dim aFirstArray() As Variant 

如何清除整个arrays?
怎么样收集?

您可以使用EraseReDim语句清除数组:

 Dim threeDimArray(9, 9, 9), twoDimArray(9, 9) As Integer Erase threeDimArray, twoDimArray ReDim threeDimArray(4, 4, 9) 

在这里查看每种方法的不同用法。

更新

要移除一个集合,可以迭代它的项目并使用remove方法:

 For i = 1 to MyCollection.Count MyCollection.Remove 1 ' Remove first item Next i 

要在VBA中删除dynamic数组,请使用Erase指令。

例:

 Dim ArrayDin() As Integer ReDim ArrayDin(10) 'Dynamic allocation Erase ArrayDin 'Erasing the Array 

希望这个帮助!

这是如此简单:

 Erase aFirstArray 
 [your Array name] = Empty 

然后数组将会没有内容,可以再次填充。

我陷入了清除整个arrays失败dim / redim的情况下:

具有2个模块范围的数组,私有在一个用户表单中,

一个数组是dynamic的,使用一个类模块,另一个是固定的,并有一个特殊的types。

 Option Explicit Private Type Perso_Type Nom As String PV As Single 'Long 'max 1 Mana As Single 'Long Classe1 As String XP1 As Single Classe2 As String XP2 As Single Classe3 As String XP3 As Single Classe4 As String XP4 As Single Buff(1 To 10) As IPicture 'Disp BuffType(1 To 10) As String Dances(1 To 10) As IPicture 'Disp DancesType(1 To 10) As String End Type Private Data_Perso(1 To 9, 1 To 8) As Perso_Type Dim ImgArray() As New ClsImage 'ClsImage is a Class module 

我有一个声明为公共的子从这个用户表单内部和外部清除这些数组(和相关的运行时创build的控件),如下所示:

 Public Sub EraseControlsCreatedAtRunTime() Dim i As Long On Error Resume Next With Me.Controls 'removing all on run-time created controls of the Userform : For i = .Count - 1 To 0 Step -1 .Remove i Next i End With Err.Clear: On Error GoTo 0 Erase ImgArray, Data_Perso 'ReDim ImgArray() As ClsImage ' i tried this, no error but wouldn't work correctly 'ReDim Data_Perso(1 To 9, 1 To 8) As Perso_Type 'without the erase not working, with erase this line is not needed. End Sub 

注意:这最后一个子是从外部(其他窗体和类模块) Call FormName.SubName第一次调用,但必须用Application.Run FormName.SubNamereplace它,less错误,不要问为什么…

只能使用Redim语句

  Dim aFirstArray() As Variant Redim aFirstArray(nRows,nColumns)