Erasing Collection

I want to erase the contents of a collection but this doesn't work, just say TheCollection already stored 23 entrees

For X = 1 To TheCollection.Count
Next X

Can someone give me code that would work
Who is Participating?
Brendt HessSenior DBACommented:
Don't remove them using X as the item to remove.  Instead, just remove the first item in the collection each time (there will always be a first item)

Also, shouldn't this be .Remove, not .Delete?

For X = 1 to TheCollection.Count
  TheCollection.Remove 1
Next X
adam8Author Commented:
it is delete, i just got mixed up.
The problem is that i store info to the collection until the user changes his/her selection, then if that happens i need to remove every item in the collection and then add different info to the collection the second time.
Get it, i need to remove all the items in the collection, not just the first one.
adam8Author Commented:
I think it is delete,
It doesn't matter anyway,
I know what you are talking about
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

adam8Author Commented:
it is very important that the entire contents of the collection is deleted.
Brendt HessSenior DBACommented:
The .Delete versus .Remove doesn't matter - it could be version differences.   Just go with removing the first entry each time in your loop, and you'll be fine.
Brendt HessSenior DBACommented:
The reason this will work:

Assume you have five items in your collection.  When you delete the first item, the 2nd item is automatically renumbered to be the first item, the 3rd becomes the 2nd, etc.

Given a collection:

1  2  3  4  5
After deleting #1, your collection is:

1  2  3  4
After deleting #2, your collection is:

1  2  3

adam8Author Commented:
ok, i forgot to think about that.
so how would i go about deleting these items.

For X = 1 to Items.Count
Next X

Would that work?
Brendt HessSenior DBACommented:
That should be just perfect.
For collection it's better to use For Each statement:
Dim X As Variant, TheCollection As Collection
For Each X in TheCollection
    TheCollection.Remove (1)
Next X
BTW, if your collection has delete method, may be it has clear method?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.