VBA 检查变量是否为空 [英] VBA Check if variable is empty

查看:274
本文介绍了VBA 检查变量是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我想在其中检查某些属性是否设置为 false,例如:

If (not objresult.EOF) Then'一些代码万一

但不知何故,有时objresult.EOFEmpty,我该如何检查呢?

  • IsEmpty 函数仅适用于 Excel 单元格
  • objresult.EOF Is Nothing - 返回 Empty
  • objresult.EOF <>null - 也返回 Empty

解决方案

你的测试方式取决于属性的数据类型:

<前>|类型 |测试 |测试2|数字(Long、Integer、Double 等)|如果 obj.Property = 0 那么 ||布尔值(真/假)|如果不是 obj.Property 那么 |如果 obj.Property = False 那么|对象 |如果 obj.Property 什么都没有,那么 ||字符串 |如果 obj.Property = "" 然后 |如果 LenB(obj.Property) = 0 那么|变体 |如果 obj.Property = Empty 那么 |

您可以通过按 F2 启动对象浏览器并查找对象来告诉数据类型.另一种方法是只使用 TypeName 函数:MsgBox TypeName(obj.Property)

I have an object and within it I wanna check if some properties is set to false, like:

If (not objresult.EOF) Then
  'Some code 
End if

But somehow, sometimes objresult.EOF is Empty, and how can I check it?

  • IsEmpty function is for excel cells only
  • objresult.EOF Is Nothing - return Empty
  • objresult.EOF <> null - return Empty as well!

解决方案

How you test depends on the Property's DataType:

| Type                                 | Test                            | Test2
| Numeric (Long, Integer, Double etc.) | If obj.Property = 0 Then        | 
| Boolen (True/False)                  | If Not obj.Property Then        | If obj.Property = False Then
| Object                               | If obj.Property Is Nothing Then |
| String                               | If obj.Property = "" Then       | If LenB(obj.Property) = 0 Then
| Variant                              | If obj.Property = Empty Then    |

You can tell the DataType by pressing F2 to launch the Object Browser and looking up the Object. Another way would be to just use the TypeName function:MsgBox TypeName(obj.Property)

这篇关于VBA 检查变量是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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