VBA 是否“和"?当第一个参数为假时,运算符评估第二个参数? [英] Does the VBA "And" operator evaluate the second argument when the first is false?

查看:36
本文介绍了VBA 是否“和"?当第一个参数为假时,运算符评估第二个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Function Foo(thiscell As Range) As Boolean
  Foo = thiscell.hasFormula And (InStr(1, UCase(Split(thiscell.formula, Chr(40))(0)), "bar") > 0)
End Function

此函数的存在是为了测试 (.

This function exists to test for the presence of a certain substring (bar, in this case) before the (.

我遇到问题的情况是,当传递给函数的单元格为空时,thisCell.hasFormula 为假,但 和 之后的语句仍在评估中.这给了我一个运行时下标超出范围的错误.

The case I'm having trouble with is when the cell passed into the function is empty, the thisCell.hasFormula is false, but the statement after the and is still being evaluated. This gives me a subscript out of range error in runtime.

VBA 是否真的继续评估 And 的第二个参数,即使第一个参数为假?

Does VBA actually continue evaluating the second argument to the And, even when the first was false?

推荐答案

您要查找的内容称为短路评估".

What you are looking for is called "short-circuit evaluation".

VBA 没有.

您可以在此处看到一种可能适合您情况的方法.

You can see an approach that is probably adaptable to your situation here.

那里选择的方法涉及用Select Case 代替If.还有一个使用嵌套Ifs的例子.

The approach that was chosen there involved substituting a Select Case for the If. There is also an example of using nested Ifs.

这篇关于VBA 是否“和"?当第一个参数为假时,运算符评估第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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