VBA 检查变量是否为空

发布于 2024-09-11 07:07:18 字数 437 浏览 4 评论 0原文

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

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

但有时,objresult.EOFEmpty ;我该如何检查?

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

I have an object and within it I want to check if some properties are set to False, like:

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

But sometimes, objresult.EOF is Empty; how can I check for this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天气好吗我好吗 2024-09-18 07:07:18

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

| 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    |

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

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)

人│生佛魔见 2024-09-18 07:07:18

要检查 Variant 是否为 Null,您需要这样做:

Isnull(myvar) = True

Not Isnull(myvar)

To check if a Variant is Null, you need to do it like:

Isnull(myvar) = True

or

Not Isnull(myvar)
城歌 2024-09-18 07:07:18

对于数字,这很棘手,因为如果数字单元格为空,VBA 会为其分配默认值 0,因此 VBA 代码很难区分输入的零和输入的数字空白数字单元格。

以下检查对我有用,以查看单元格中是否输入了实际的 0:

If CStr(rng.value) = "0" then
    'your code here'
End If

For a number, it is tricky because if a numeric cell is empty VBA will assign a default value of 0 to it, so it is hard for your VBA code to tell the difference between an entered zero and a blank numeric cell.

The following check worked for me to see if there was an actual 0 entered into the cell:

If CStr(rng.value) = "0" then
    'your code here'
End If
纸短情长 2024-09-18 07:07:18

我遇到了类似的问题,整数可以在 Access VBA 中合法地分配为 0。上述解决方案都不适合我。

起初我只使用了布尔变量和 IF 语句:

Dim i as integer, bol as boolean
   If bol = false then
      i = ValueIWantToAssign
      bol = True
   End If

在我的例子中,我的整数变量赋值是在 for 循环和另一个 IF 语句中,所以我最终使用了“Exit For”,因为它是更简洁。

像这样:

Dim i as integer
ForLoopStart
   If ConditionIsMet Then
      i = ValueIWantToAssign
   Exit For
   End If
ForLoopEnd

I had a similar issue with an integer that could be legitimately assigned 0 in Access VBA. None of the above solutions worked for me.

At first I just used a boolean var and IF statement:

Dim i as integer, bol as boolean
   If bol = false then
      i = ValueIWantToAssign
      bol = True
   End If

In my case, my integer variable assignment was within a for loop and another IF statement, so I ended up using "Exit For" instead as it was more concise.

Like so:

Dim i as integer
ForLoopStart
   If ConditionIsMet Then
      i = ValueIWantToAssign
   Exit For
   End If
ForLoopEnd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文