VBA:有条件的 - 什么都不是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8472907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:16:47  来源:igfitidea点击:

VBA: Conditional - Is Nothing

vbaif-statementexcel-vbaexcel-2007nothing

提问by StarDotStar

There is an Ifcondition in a VBA application as seen below:

IfVBA 应用程序中有一个条件,如下所示:

If Not My_Object Is Nothing Then
My_Object.Compute

When the code is run in debug mode, I found that the Ifcondition returns a true even when My_Objecthas "No Variables".

当代码在调试模式下运行时,我发现If即使My_Object有“无变量” ,条件也会返回 true 。

Could somebody please explain this? I want My_Object.Computeto be executed only when My_Objectexists.

有人可以解释一下吗?我只想My_Object.ComputeMy_Object存在时执行。

回答by mwolfe02

Based on your comment to Issun:

根据您对 Issun 的评论:

Thanks for the explanation. In my case, The object is declared and created prior to the If condition. So, How do I use If condition to check for < No Variables> ? In other words, I do not want to execute My_Object.Compute if My_Object has < No Variables>

感谢您的解释。就我而言,对象是在 If 条件之前声明和创建的。那么,如何使用 If 条件来检查 < No Variables> ?换句话说,如果 My_Object 有 <No Variables>,我不想执行 My_Object.Compute

You need to check one of the properties of the object. Without telling us what the object is, we cannot help you.

您需要检查对象的属性之一。不告诉我们对象是什么,我们无法帮助您。

I did test several common objects and found that an instantiated Collectionwith no items added shows <No Variables>in the watch window. If your object is indeed a collection, you can check for the <No Variables>condition using the .Countproperty:

我确实测试了几个常见的对象,发现Collection没有添加任何项目的实例化显示<No Variables>在监视窗口中。如果您的对象确实是一个集合,则可以<No Variables>使用以下.Count属性检查条件:

Sub TestObj()
Dim Obj As Object
    Set Obj = New Collection
    If Obj Is Nothing Then
        Debug.Print "Object not instantiated"
    Else
        If Obj.Count = 0 Then
            Debug.Print "<No Variables> (ie, no items added to the collection)"
        Else
            Debug.Print "Object instantiated and at least one item added"
        End If
    End If
End Sub


It is also worth noting that if you declare any object As Newthen the Is Nothingcheck becomes useless. The reason is that when you declare an object As Newthen it gets created automatically when it is first called, even if the first time you call it is to see if it exists!

还值得注意的是,如果您声明任何对象,As NewIs Nothing检查将变得无用。原因是当你声明一个对象As New时,它会在第一次调用时自动创建,即使你第一次调用它是为了查看它是否存在!

Dim MyObject As New Collection
If MyObject Is Nothing Then  ' <--- This check always returns False

This does not seem to be the cause of your specific problem. But, since others may find this question through a Google search, I wanted to include it because it is a common beginner mistake.

这似乎不是您特定问题的原因。但是,由于其他人可能会通过谷歌搜索找到这个问题,我想把它包括在内,因为这是一个常见的初学者错误。

回答by aevanko

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

回答by WannabeProger

In my sample code, I was setting my objectto nothing, and I couldn't get the "not" part of the if statement to work with the object. I tried if My_Object is not nothingand also if not My_Object is nothing. It may be just a syntax thing I can't figure out but I didn't have time to mess around, so I did a little workaround like this:

在我的示例代码中,我没有设置my object任何内容,并且无法让 if 语句的“not”部分与对象一起使用。我if My_Object is not nothing也试过了if not My_Object is nothing。这可能只是我无法弄清楚的语法问题,但我没有时间乱搞,所以我做了一些像这样的解决方法:

if My_Object is Nothing Then
    'do nothing
Else
    'Do something
End if