VBA:有条件 - 没有 [英] VBA: Conditional - Is Nothing

查看:156
本文介绍了VBA:有条件 - 没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所示,VBA应用程序中有 If 条件:

 如果不是My_Object是没有
My_Object.Compute

当代码运行在调试模式下,我发现如果条件返回true,即使 My_Object 具有No Variables p>

有人可以解释一下吗?只有当 My_Object 存在时,才需要执行 My_Object.Compute

解决方案

根据您对Issun的评论:


感谢您的解释。在我的情况下,该对象在If条件之前被声明和创建。那么,如何使用If条件来检查<没有变量>?换句话说,我不想执行My_Object.Compute如果My_Object有<没有变量>


您需要检查对象的某个属性。没有告诉我们什么是对象,我们不能帮助你。



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

  Sub TestObj()
Dim Obj As Object
Set Obj = New Collection
如果Obj Is Nothing然后
Debug.Print对象未实例化
Else
如果Obj.Count = 0然后
Debug.Print<无变量>(即,否添加到集合中的项目)
Else
Debug.Print对象实例化和至少一个项目添加
End If
End If
End Sub






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

  Dim MyObject As New Collection 
如果MyObject是Nothing然后'< ---这个检查总是返回False

这似乎不是您的具体问题的原因。但是,由于其他人可能会通过Google搜索找到这个问题,所以我想包含它,因为它是一个常见的初学者错误。


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 is run in debug mode, I found that the If condition returns a true even when My_Object has "No Variables".

Could somebody please explain this? I want My_Object.Compute to be executed only when My_Object exists.

解决方案

Based on your comment to 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>

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 Collection with 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 .Count property:

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 New then the Is Nothing check becomes useless. The reason is that when you declare an object As New then it gets created automatically when it is first called, even if the first time you call it is to see if it exists!

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.

这篇关于VBA:有条件 - 没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆