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

前端 未结 15 1844
自闭症患者
自闭症患者 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 05:52

    i used this code to convert array to collection and back to array to remove duplicates, assembled from various posts here (sorry for not giving properly credit).

    Function ArrayRemoveDups(MyArray As Variant) As Variant
    Dim nFirst As Long, nLast As Long, i As Long
    Dim item As Variant, outputArray() As Variant
    Dim Coll As New Collection
    
    'Get First and Last Array Positions
    nFirst = LBound(MyArray)
    nLast = UBound(MyArray)
    ReDim arrTemp(nFirst To nLast)
    i = nFirst
    'convert to collection
    For Each item In MyArray
        skipitem = False
        For Each key In Coll
            If key = item Then skipitem = True
        Next
        If skipitem = False Then Coll.Add (item)
    Next item
    'convert back to array
    ReDim outputArray(0 To Coll.Count - 1)
    For i = 1 To Coll.Count
        outputArray(i - 1) = Coll.item(i)
    Next
    ArrayRemoveDups = outputArray
    End Function
    

提交回复
热议问题