Collection.Remove Method

Named Arguments

Yes

Syntax

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.

Description

Removes a member from a collection.

Rules at a Glance

  • 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.

Example

colMyCollection.Remove "Name"

Programming Tips and Gotchas

  • 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 ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.