Excel会对提供给IF函数的结果参数进行评估吗? [英] Does Excel evaluate both result arguments supplied to the IF function?

查看:153
本文介绍了Excel会对提供给IF函数的结果参数进行评估吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Excel的如果函数接受三个参数,一个条件,一个if-true值和一个if-false值。 Excel是否计算出所有三个参数的值,还是仅解决了条件的值和相应的结果?

Excel's if function takes three arguments, a condition, an if-true value, and an if-false value. Does Excel work out the value of all three arguments, or does it only work out the value of the condition and the corresponding result?

澄清:我不知道如果结果将是,我想知道是否计算所有参数的值计算函数的结果。

Clarification: I'm not wondering what the result of the if will be, I'm wondering whether or not it calculates the value of all arguments before calculating the result of the function.

这相当于询问如果函数是否使用懒惰或严格评价。例如,以下伪代码:

This is equivalent to asking whether or not the if function uses lazy or strict evaluation. For example, the following pseudocode:

x = 5;
print x>2 ? "Bigger" : "Smaller" + 1/0

将抛出一个除以零异常一种具有完全严格评估的语言,因为它将评估 1/0 ,即使?不需要结果: operator。

would throw a divide-by-zero exception in a language with fully strict evaluation, as it would evaluate the 1/0, even though the result wouldn't be required for the ?: operator.

使用懒惰评估语言,?:操作符将评估 x> 2 甚至决定要评估哪个表达式。

In a lazy-evaluation language, the ?: operator would evaluate x>2 before even deciding which expression to evaluate.

问题是在Excel中, 1/0 产生一个合法的值(恰好是#DIV / 0!),它们可以存在于表达式中。因此,简单地调用 = if(true,1,1 / 0)不会显示Excel是否正在评估 1/0

The problem is that in Excel, 1/0 produces a legitimate value (which happens to be #DIV/0!) that can exist in expressions. Therefore, simply calling =if(true,1,1/0) doesn't show whether Excel is evaluating the 1/0 or not.

推荐答案

非常东部测试

? iif(true, 1, 1/0) 'run-time error: division by zero

假设你真的意味着iif() - 在VBA中这不会短路,所以你应该使用 If..Then..Else..End如果在情况下这可能是一个问题。

I'm assuming you really mean iif() - in VBA this does not "short-circuit", so you should use If..Then..Else..End If in cases where that could be a problem.

确定 - 测试你真正提出的问题:

Ok - testing what you really asked:

'In a VBA module
Function TruePart()
      MsgBox "True part"
      TruePart = "True"
End Function


Function FalsePart()
      MsgBox "False part"
      FalsePart = "False"
End Function

在单元格中: = IF(TRUE,truepart(),falsepart())

每个IF()单元格的计算只能获得一个msgbox。

Only get one msgbox per calculation of the IF() cell.

作为进一步的验证,这给你两个msgbox - 每个一个:

As further validation, this gives you two msgbox - one for each:

Sub Tester()
    Debug.Print IIf(True, TruePart(), FalsePart())
End Sub 

这篇关于Excel会对提供给IF函数的结果参数进行评估吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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