Function Foo(thiscell As Range) As Boolean
Foo = thiscell.hasFormula And (InStr(1, UCase(Split(thiscell.formula, Chr(40))(0)), "bar") > 0)
End Function
Run Code Online (Sandbox Code Playgroud)
此函数用于在(.)之前测试某个子字符串(在本例中为bar)的存在.
我遇到问题的情况是当传入函数的单元格为空时,thisCell.hasFormula为false,但是仍然正在评估和之后的语句.这给了我运行时超出范围错误的下标.
VBA是否真的继续评估And的第二个参数,即使第一个参数是假的?
VBA不会短路
VBA不支持短路 - 显然是因为它只有按位和/或/不等操作.从VBA语言规范:"逻辑运算符是对其操作数执行按位计算的简单数据运算符." 从这个角度来看,使用true = &H1111和设计VBA是有道理的false = &H0000:这样,逻辑语句可以被评估为按位运算.
缺少短路可能会导致问题
性能:在ReallyExpensiveFunction()评估此语句时将始终运行,即使条件左侧的结果不需要
If IsNecessary() And ReallyExpensiveFunction() Then
'...
End If
错误:如果MyObj为Nothing,则此条件语句将导致运行时错误,因为VBA仍将尝试检查值 Property
If Not MyObj Is Nothing And MyObj.Property = 5 Then
'...
End If
我用来实现短效行为的解决方案是嵌套If的
If cond1 And cond2 Then
'...
End If
Run Code Online (Sandbox Code Playgroud)
变
If cond1 Then
If cond2 Then
'...
End If
End If
Run Code Online (Sandbox Code Playgroud)
通过这种方式,If语句给出了类似短路的行为,即不打算评估cond2是否cond1存在False.
如果存在Else子句,则会创建重复的代码块
If Not MyObj Is Nothing And MyObj.Property …Run Code Online (Sandbox Code Playgroud)