VBA: Conditional - Is Nothing

后端 未结 3 2083
半阙折子戏
半阙折子戏 2020-12-16 11:34

There is an If condition in a VBA application as seen below:

If Not My_Object Is Nothing Then
My_Object.Compute

When the code

3条回答
  •  时光取名叫无心
    2020-12-16 12:06

    Just becuase your class object has no variables does not mean that it is nothing. Declaring and object and creating an object are two different things. Look and see if you are setting/creating the object.

    Take for instance the dictionary object - just because it contains no variables does not mean it has not been created.

    Sub test()
    
    Dim dict As Object
    Set dict = CreateObject("scripting.dictionary")
    
    If Not dict Is Nothing Then
        MsgBox "Dict is something!"  '<--- This shows
    Else
        MsgBox "Dict is nothing!"
    End If
    
    End Sub
    

    However if you declare an object but never create it, it's nothing.

    Sub test()
    
    Dim temp As Object
    
    If Not temp Is Nothing Then
        MsgBox "Temp is something!"
    Else
        MsgBox "Temp is nothing!" '<---- This shows
    End If
    
    End Sub
    

提交回复
热议问题