Home > How-To Library > Arrays, Collections, Lists

How to check whether an item exists in a collection

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
'If you pass a numeric index that's either negative or greater than the number 'of items currently in the collection, you get an error code 9-"Subscript out 'of range" (exactly as if you were acting on a standard array); if you pass a 'nonexistent string key, you get error code 5-"Invalid procedure call or 'argument." Function ItemExists(col As Collection, Key As String) As Boolean Dim dummy As Variant On Error Resume Next dummy = col.Item(Key) ItemExists = (Err <> 5) End Function

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.