VBA如何处理“或”语句 [英] How does VBA treat 'or' statements

查看:215
本文介绍了VBA如何处理“或”语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

Dim qt As QueryTable
For Each qt In ActiveSheet.QueryTables
    If qt.Sql = SQLSTRING Then Exit For
Next qt
If qt Is Nothing Or qt.Sql <> SQLSTRING Then 
     DoStuff 
End If

我有一个问题,因为我可以看到'qt is nothing'正在评估为true,但在同一行上,我得到一个'91:Object变量或者使用块变量未设置'错误。这不是一个问题,因为我可以很容易地抓住错误,但是我很惊讶,因为我认为如果一个或的第一个子句评估为真,它甚至不应该试图评估第二个条款...这是不是在vba中可以依赖的东西?

I have a question because I can see that 'qt is nothing' is evaluating to true, but on the same line, I get a '91: Object variable or With block variable not set' error. It's not an issue as I can easily catch the error, but tbh I was surprised, as I thought if the first clause of an 'or' evaluates to true, it shouldn't even try to evaluate the second clause...is this not something I can rely on in vba?

推荐答案

VBA 不是很聪明,使用: / p>

VBA is not so clever, use:

If qt Is Nothing Then
    DoStuff
Else
    If qt.Sql <> SQLSTRING Then
        DoStuff
    End If
End If

这篇关于VBA如何处理“或”语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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