Determining whether an object is a member of a collection in VBA

前端 未结 15 1842
自闭症患者
自闭症患者 2020-11-27 05:35

How do I determine whether an object is a member of a collection in VBA?

Specifically, I need to find out whether a table definition is a member of the TableDe

15条回答
  •  时光说笑
    2020-11-27 06:06

    I created this solution from the above suggestions mixed with microsofts solution of for iterating through a collection.

    Public Function InCollection(col As Collection, Optional vItem, Optional vKey) As Boolean
    On Error Resume Next
    
    Dim vColItem As Variant
    
    InCollection = False
    
    If Not IsMissing(vKey) Then
        col.item vKey
    
        '5 if not in collection, it is 91 if no collection exists
        If Err.Number <> 5 And Err.Number <> 91 Then
            InCollection = True
        End If
    ElseIf Not IsMissing(vItem) Then
        For Each vColItem In col
            If vColItem = vItem Then
                InCollection = True
                GoTo Exit_Proc
            End If
        Next vColItem
    End If
    
    Exit_Proc:
    Exit Function
    Err_Handle:
    Resume Exit_Proc
    End Function
    

提交回复
热议问题