how can test the existence of an item within a collection vba

C

Chris

how can test the existence of an item within a collection vba ?

i declare as it

dim toto as new collection

toto.add item:=titi, key:=1

toto.add item:=tata, key:=2

then i need to know if tutu is in the collection but my problem is that using

toto.item("tutu") guve me an error and it seems that vba collection doesn't not have an exist method.

Do you have a solution ?
 
K

Karl E. Peterson

Chris said:
how can test the existence of an item within a collection vba ?
toto.item("tutu") guve me an error and it seems that vba collection
doesn't not have an exist method.

Do you have a solution ?

Sounds like you found the solution. Here's a rather generic algorithm taken
off the first page of results from Google:

Private Function ContainsItem(col As Collection, val As Variant) As
Boolean
Dim itm As Variant
On Error Resume Next
itm = col.Item(val)
ContainsItem = Not (Err.Number = 5 Or Err.Number = 9)
On Error GoTo 0
End Function

Later... Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top