To clear an Excel VBA Collection, the fastest and most effective method is to set the collection object to Nothing and then re-initialize it using the New keyword. Because the VBA Collection object does not have a built-in .Clear method like a Dictionary does, you must destroy the existing instance and create a fresh, empty one to remove all stored items.

What You'll Need

  • Microsoft Excel installed on Windows or macOS.
  • The Visual Basic for Applications (VBA) editor enabled (accessible via the Developer tab).
  • An existing Collection object defined in your code.

Step-by-Step

  1. Open the VBA Editor: Press Alt + F11 on your keyboard to open the Visual Basic Editor from Excel.
  2. Locate Your Collection: Find the procedure where your collection is declared. Ensure the collection is declared at a scope (Module level or Global) that allows it to be accessed by the clearing routine.
  3. Set the Collection to Nothing: To wipe the current data and free up the memory associated with the collection, use the Set statement to assign the object to Nothing.
    Set myCollection = Nothing
  4. Re-initialize the Collection: Since setting the object to Nothing deletes the collection entirely, you must create a new instance of it if you plan to add more data to it later in the same session.
    Set myCollection = New Collection
  5. Verify the Clear: You can verify the collection is empty by checking the .Count property. If the clear was successful, myCollection.Count will return 0.

Common Mistakes

The most frequent error developers make is attempting to call myCollection.Clear. Unlike the Scripting.Dictionary object, the standard VBA Collection does not possess a .Clear method. Attempting to use this will result in a "Run-time error '438': Object doesn't support this property or method."

Another common mistake is forgetting to re-initialize the collection with New Collection after setting it to Nothing. If you try to add an item to a collection that has been set to Nothing without recreating it, VBA will trigger a "Run-time error '91': Object variable or With block variable not set."

Tips

Use a Dedicated Reset Procedure: If you need to clear your collection frequently across different macros, create a small helper subroutine. This prevents you from writing the two-line "Set Nothing/Set New" sequence repeatedly throughout your project.

Consider a Dictionary for Large Sets: If your project requires frequent clearing, adding, and removing of specific items by key, consider using the Scripting.Dictionary (available via the Microsoft Scripting Runtime library). Dictionaries include a native .RemoveAll method, which is more efficient than recreating the object.

Scope Management: If the collection is only needed within a single subroutine, you don't need to manually clear it. Once the subroutine finishes executing, the local collection variable is automatically destroyed by VBA's garbage collection.

Troubleshooting

Error 91 (Object variable not set): This happens when you try to access the collection after setting it to Nothing but before calling Set ... = New Collection. Ensure the New keyword is used immediately after the reset.

Collection still contains data: If the collection seems to persist despite your clear command, check if you have multiple collections with similar names or if the collection is being repopulated by another event-driven macro (like Worksheet_Change) immediately after it is cleared.

Memory Leaks: In extremely large loops where collections are created and destroyed thousands of times, ensure you are explicitly setting the object to Nothing to help VBA manage system resources more effectively.

Frequently Asked Questions

Is there a .Clear method for VBA Collections?

No, the standard VBA Collection object does not have a .Clear method. You must set the object to Nothing and then re-initialize it using New Collection.

Can I remove just one item instead of clearing the whole list?

Yes. Use the .Remove method. You can specify the index of the item (e.g., myCollection.Remove 1) to delete a specific entry without wiping the entire collection.

Does setting a collection to Nothing delete the data permanently?

Yes, it removes the references to the objects stored in that collection from the computer's memory. Once the reference is gone and the object is not used elsewhere, the memory is reclaimed.

Which is faster: looping through and removing items or setting to Nothing?

Setting the collection to Nothing and recreating it is significantly faster. Looping through a collection to remove items one by one is computationally expensive and can lead to errors if the index shifts during the removal process.

Do I need to add a special library to use Collections?

No, the Collection object is a native part of VBA and is available in all Excel projects by default without needing any external references.