Solved

How do i clear a collection in vba

Posted on 2009-04-01
19
5,759 Views
Last Modified: 2012-05-06
In Visual Basic there seems to be a collection.clear() command but not in Visual Basic for Applications.
Anyone know how to do it in vba?
0
Comment
Question by:JoseJalapeno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +1
19 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036698
You'll need to use the Remove method to remove each item in the collection. The below function shows an example.

Wayne
Public Function ClearCollection(ByRef coll As Collection)
     Do
          coll.Remove (1)
     Loop While coll.Count > 0
End Function

Open in new window

0
 

Author Comment

by:JoseJalapeno
ID: 24036735
Thanks for that.
One more question though:
If all you do is empty the items surely the collection still exists. If you set a new collectio you end up with 2
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036751
>>If all you do is empty the items surely the collection still exists.

Yeah, sure, but you didn't want to dispose of the collection - simply clear the items. To dispose of a collection, use this....

    Set coll = Nothing

>>If you set a new collectio you end up with 2

Do you mean if you do this to an already instantiated collection?

    Set coll = New Collection

If so, then no. The old collection is disposed of.

Wayne
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 17

Expert Comment

by:ExcelGuide
ID: 24036754
sorry for the offtopic, but webtubbs I love that mask!!! :)
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036760
Yeah, the folks in the office are into the April Fool's spirit :)
0
 

Author Comment

by:JoseJalapeno
ID: 24036785
OK sorry to appear to have a single brain cell but...
...i understood that = New Collection meant a new instance of the collection, same as a new instance of an object, am i wrong?
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036804
You are not wrong. Using "Set coll = New Collection" sets the collection *variable* to a new collection. The previous instance of the collection is no longer "connected" to a variable and is released from memory.
0
 

Author Comment

by:JoseJalapeno
ID: 24036885
How come then if you open multiple instances of a form you use frm = new Form_X which produces a new copy of the form. The previous one does not dissapear.
Is it a different set of rules for collections?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24037088
Userforms are a little bit odd. You have to actually close them, either manually or in code.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24037109
This is basically because they are added to the Userforms collection which maintains a reference to them until they are unloaded.
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 24037486
I don't mean to be rude, but I answered your question, "How do i clear a collection in vba" in my first comment.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24037543
I agree. Can you reopen it, or do we need a VB ZAPE?
0
 

Author Comment

by:JoseJalapeno
ID: 24037743
Sorry but when i clicked on "Accept as Solution" against webtubbs it did not offer me the opportunity to assign any points.
If i am doing something wrong please educate me.
0
 

Author Comment

by:JoseJalapeno
ID: 24038208
I didn't want to assign points to more than one person.
I accepted webtubbs solution, or at least i thought i had.
How then do points get awarded to one person?
0
 

Author Comment

by:JoseJalapeno
ID: 24038222
Also can you tell me where the help is for assigning points as i must have missed it.
0
 

Author Closing Comment

by:JoseJalapeno
ID: 31565206
Apologies webtubbs, genuine mistake.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question