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

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

问题描述

函数Foo(thiscell As Range)As Boolean
Foo = thiscell.hasFormula And(InStr(1,UCase(Split(thiscell.formula,Chr(40))( 0)),bar)> 0)
结束函数

在(。



之前测试某个子字符串(在这种情况下为bar)的存在情况我遇到的情况是当单元格进入函数是空的,thisCell.hasFormula是false,但是之后的语句仍然被计算,这给我在运行时间内超出范围的错误。



VBA实际上继续评估And的第二个参数,即使第一个是false?

解决方案

你正在寻找什么叫做短路评估



VBA没有。



你可以看到一种可能适应你的情况的方法这里



在那里选择的方法是替换选择案例如果。还有一个使用嵌套的例子 Ifs


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

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.

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 doesn't have it.

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

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

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

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