How to clear the entire array?
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
You can either use the Erase
or ReDim
statements to clear the array. Examples of each are shown in the MSDN documentation. For example:
Dim threeDimArray(9, 9, 9), twoDimArray(9, 9) As Integer
Erase threeDimArray, twoDimArray
ReDim threeDimArray(4, 4, 9)
To remove a collection, you iterate over its items and use the Remove
method:
For i = 1 to MyCollection.Count
MyCollection.Remove 1 ' Remove first item
Next i
It is as simple as :
Erase aFirstArray
For deleting a dynamic array in VBA use the instruction Erase
.
Example:
Dim ArrayDin() As Integer
ReDim ArrayDin(10) 'Dynamic allocation
Erase ArrayDin 'Erasing the Array
Hope this help!