vba collection item exists code example

Example 1: vba collection contains

' Vba collection contains
Public Function ExistsInCollection(col As Collection, pKey As Variant) As Boolean
    On Error GoTo err
    ExistsInCollection = True
    IsObject (col.Item(pKey))
    Exit Function
err:
    ExistsInCollection = False
End Function

Example 2: vba collection key exists

Public Function KeyExistsInCollection(ByVal key As String, _
                                      ByRef container As Collection) As Boolean
    With Err
        If container Is Nothing Then .Raise 91
        On Error Resume Next
        Dim temp As Variant
        temp = container.Item(key)
        On Error GoTo 0

        If .Number = 0 Then
            KeyExistsInCollection = True 
        ElseIf .Number <> 5 Then
            .Raise .Number
        End If
    End With
End Function

Tags:

Vb Example