vba 如何清除整个数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3018510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:32:52  来源:igfitidea点击:

How to clear the entire array?

arraysexcelvba

提问by l--''''''---------''''''''''''

I have an array like this:

我有一个这样的数组:

Dim aFirstArray() As Variant

How do I clear the entire array? What about a collection?

如何清除整个数组?一个集合呢?

回答by Sarfraz

You can either use the Eraseor ReDimstatements to clear the array:

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

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

See the different usage of each method here.

在此处查看每种方法的不同用法。

Update

更新

To remove a collection, you iterate over its items and use the removemethod:

要删除集合,请遍历其项目并使用以下remove方法:

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

回答by Andres Sommerhoff

For deleting a dynamic array in VBA use the instruction Erase.

要在 VBA 中删除动态数组,请使用指令Erase

Example:

例子:

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

Hope this help!

希望这有帮助!

回答by Jaanus

It is as simple as :

它很简单:

Erase aFirstArray

回答by Koke

[your Array name] = Empty

Then the array will be without content and can be filled again.

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

回答by Rafiki

Find a better use for myself: I usually test if a variant is empty, and all of the above methods fail with the test. I found that you can actually set a variant to empty:

给自己找一个更好的用途:我通常测试一个变体是否为空,并且上述所有方法都通过测试失败。我发现您实际上可以将变体设置为空:

Dim aTable As Variant
If IsEmpty(aTable) Then
    'This is true
End If
ReDim aTable(2)
If IsEmpty(aTable) Then
    'This is False
End If
ReDim aTable(2)
aTable = Empty
If IsEmpty(aTable) Then
    'This is true
End If
ReDim aTable(2)
Erase aTable
If IsEmpty(aTable) Then
    'This is False
End If

this way i get the behaviour i want

这样我就得到了我想要的行为

回答by SUNIL KUMAR

ReDim aFirstArray(0)

This will resize the array to zero and erase all data.

这会将数组大小调整为零并擦除所有数据。

回答by Patrick Lepelletier

i fell into a case where clearing the entire array failed with dim/redim :

我陷入了一种情况,即使用 dim/redim 清除整个阵列失败:

having 2 module-wide arrays, Private inside a userform,

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

One array is dynamic and uses a class module, the other is fixed and has a special type.

一个数组是动态的并使用类模块,另一个是固定的并具有特殊类型。

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

And i have a sub declared as public to clear those arrays (and associated run-time created controls) from inside and outside the userform like this :

我有一个声明为 public 的子程序,用于从用户窗体内部和外部清除这些数组(以及相关的运行时创建的控件),如下所示:

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

note : this last sub was first called from outside (other form and class module) with Call FormName.SubNamebut had to replace it with Application.Run FormName.SubName, less errors, don't ask why...

注意:这最后一个子程序首先从外部(其他表单和类模块)调用,Call FormName.SubName但必须将其替换为Application.Run FormName.SubName,更少的错误,不要问为什么...

回答by Moreno

Only use Redimstatement

只使用Redim语句

 Dim aFirstArray() As Variant

Redim aFirstArray(nRows,nColumns)