Collection.Remove Method |
Yes
objectvariable.Remove index
objectvariable
Use: Required
Data Type: Collection object
An object variable of the Collection type.
index
Use: Required
Data Type: Numeric or String
If a string, index is interpreted as the key; if numeric, index is treated as the ordinal position.
Removes a member from a collection.
If index is a string data type or a variant of the string data subtype, index is taken to be the key, and the member whose key corresponds to index is removed.
If index is a numerical data type or a variant of a numeric data subtype, index is taken to be the index number, and the member in the index ordinal position is removed.
If index is a string and the key doesn't exist in the collection, an error (run-time error 5, "Invalid procedure call or argument") is generated.
If index is numeric and at least one member has been added to the collection, its value must be between 1 and the maximum number of items in the collection or an error (runtime error 9, "Subscript out of range") is generated.
colMyCollection.Remove "Name"
Members of the collection that follow the removed member are automatically moved downward by one position; therefore, no gaps are left in the collection.
Because the collection is reindexed after each deletion, you should be sure not to delete a member of the collection based on a stored numeric value of index, since this value is capable of changing. Instead, you should either delete the member by key or retrieve the index value immediately before calling the Remove method.
If you are deleting multiple members of a collection by numeric index value, you should delete them backwards, from highest index value to lowest, because the collection is reindexed after each deletion.
If you are using a collection as the basis for a class module, or if you are using functions in your application to wrap and enhance the limited functionality of a collection, you can include a Clear method to remove all the members in your collection. The method should be written to remove the member in position 1 until no members are left, as the following code demonstrates:
Public Sub Clear() Dim i As Integer For i = 1 To mcolMyCollection.Count mcolMyCollection.Remove 1 Next i End Sub
Alternately, you could do the same thing by working from the end of the collection forward, as the following code illustrates:
Dim intCtr As Integer For intCtr = objCollec.Count To 1 Step -1 objCollec.Remove intCtr Next
13.58.81.111