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

前端 未结 15 1740
自闭症患者
自闭症患者 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:04

    In your specific case (TableDefs) iterating over the collection and checking the Name is a good approach. This is OK because the key for the collection (Name) is a property of the class in the collection.

    But in the general case of VBA collections, the key will not necessarily be part of the object in the collection (e.g. you could be using a Collection as a dictionary, with a key that has nothing to do with the object in the collection). In this case, you have no choice but to try accessing the item and catching the error.

    0 讨论(0)
  • 2020-11-27 06:06

    Isn't it good enough?

    Public Function Contains(col As Collection, key As Variant) As Boolean
    Dim obj As Variant
    On Error GoTo err
        Contains = True
        obj = col(key)
        Exit Function
    err:
    
        Contains = False
    End Function
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 06:08

    I did it like this, a variation on Vadims code but to me a bit more readable:

    ' Returns TRUE if item is already contained in collection, otherwise FALSE
    
    Public Function Contains(col As Collection, item As String) As Boolean
    
        Dim i As Integer
    
        For i = 1 To col.Count
    
        If col.item(i) = item Then
            Contains = True
            Exit Function
        End If
    
        Next i
    
        Contains = False
    
    End Function
    
    0 讨论(0)
  • 2020-11-27 06:09

    For the case when key is unused for collection:

    Public Function Contains(col As Collection, thisItem As Variant) As   Boolean
    
      Dim item As Variant
    
      Contains = False
      For Each item In col
        If item = thisItem Then
          Contains = True
          Exit Function
        End If
      Next
    End Function
    
    0 讨论(0)
  • 2020-11-27 06:12

    Not my code, but I think it's pretty nicely written. It allows to check by the key as well as by the Object element itself and handles both the On Error method and iterating through all Collection elements.

    https://danwagner.co/how-to-check-if-a-collection-contains-an-object/

    I'll not copy the full explanation since it is available on the linked page. Solution itself copied in case the page eventually becomes unavailable in the future.

    The doubt I have about the code is the overusage of GoTo in the first If block but that's easy to fix for anyone so I'm leaving the original code as it is.

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'INPUT       : Kollection, the collection we would like to examine
    '            : (Optional) Key, the Key we want to find in the collection
    '            : (Optional) Item, the Item we want to find in the collection
    'OUTPUT      : True if Key or Item is found, False if not
    'SPECIAL CASE: If both Key and Item are missing, return False
    Option Explicit
    Public Function CollectionContains(Kollection As Collection, Optional Key As Variant, Optional Item As Variant) As Boolean
        Dim strKey As String
        Dim var As Variant
    
        'First, investigate assuming a Key was provided
        If Not IsMissing(Key) Then
    
            strKey = CStr(Key)
    
            'Handling errors is the strategy here
            On Error Resume Next
                CollectionContains = True
                var = Kollection(strKey) '<~ this is where our (potential) error will occur
                If Err.Number = 91 Then GoTo CheckForObject
                If Err.Number = 5 Then GoTo NotFound
            On Error GoTo 0
            Exit Function
    
    CheckForObject:
            If IsObject(Kollection(strKey)) Then
                CollectionContains = True
                On Error GoTo 0
                Exit Function
            End If
    
    NotFound:
            CollectionContains = False
            On Error GoTo 0
            Exit Function
    
        'If the Item was provided but the Key was not, then...
        ElseIf Not IsMissing(Item) Then
    
            CollectionContains = False '<~ assume that we will not find the item
    
            'We have to loop through the collection and check each item against the passed-in Item
            For Each var In Kollection
                If var = Item Then
                    CollectionContains = True
                    Exit Function
                End If
            Next var
    
        'Otherwise, no Key OR Item was provided, so we default to False
        Else
            CollectionContains = False
        End If
    
    End Function
    
    0 讨论(0)
提交回复
热议问题