Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear/empty a collection object? 4

Status
Not open for further replies.

VBAjedi

Programmer
Dec 12, 2002
1,197
KH
I can't remember a fast way to clear/empty a Collection object. I have a Collection called myItems with thousands of items in it that I want to clear so I can reuse it, but I don't want to iterate through it. . .

VBAjedi [swords]
 
Collections unfortunately do not have RemoveAll - unlike a Dictionary object. This is a flaw IMHO. I don't think you can do it except by iterating Remove. Bummer. I am going to flag this one, just in case someone DOES know....but if you are asking, I'm kidda doubting.

Gerry
 
Well, the good news is that to loop through a collection of 64,000 objects to clear it takes less than half a second. That definitely suprised me! The code I used is:
Code:
For x = 1 To myList.Count
   myList.Remove 1
Next x

So I guess it isn't a big deal. . .

VBAjedi [swords]
 
Hi Jedi,

Can you not just Set the collection to Nothing?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Doh! Att'll do. . .

[rofl]

Sometimes the answer is too obvious to see! Have a star.

VBAjedi [swords]
 
Does that not simply set the instance of collection object to nothing? Does it really empty the collection of items? Learn something every day. Doh, indeed.

Gerry
 
Hi Gerry,

I'm not quite 100% sure but my understanding is that if there are no references to an object, it is destroyed. I just ran a few tests and execution of setting a collection object to nothing took about 30 seconds for a collection of 3million+ elements suggesting it does something more than just zeroising the pointer.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Ah, thanks Tony! Something to add to the collection (pun intended) of knowledge.

Gerry
 
Didn't know you could put 3 million items in a collection! Which brings up a related question - how many objects CAN you put in a collection? Is it limited only by system resources, or is there a built-in "cap" in Excel?

VBAjedi [swords]
 
AFAIK it's limited only by resources available.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top