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

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

    It requires some additional adjustments in case the items in the collection are not Objects, but Arrays. Other than that it worked fine for me.

    Public Function CheckExists(vntIndexKey As Variant) As Boolean
        On Error Resume Next
        Dim cObj As Object
    
        ' just get the object
        Set cObj = mCol(vntIndexKey)
    
        ' here's the key! Trap the Error Code
        ' when the error code is 5 then the Object is Not Exists
        CheckExists = (Err <> 5)
    
        ' just to clear the error
        If Err <> 0 Then Call Err.Clear
        Set cObj = Nothing
    End Function
    

    Source: http://coderstalk.blogspot.com/2007/09/visual-basic-programming-how-to-check.html

提交回复
热议问题