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
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.
In a lazy-evaluation language, the ?:
operator would evaluate x>2
before even deciding which expression to evaluate.
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.
The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
IF is one of the Logical functions in Microsoft Excel, and there are 3 parts (arguments) to the IF function syntax: logical_test: TEST something, such as the value in a cell. value_if_true: Specify what should happen if the test result is TRUE. value_if_false: Specify what should happen if the test result is FALSE.
Nested IF statement to check multiple logical tests If you want to evaluate multiple logical tests within a single formula, then you can nest several functions one into another. Such functions are called nested IF functions.
What is an Excel IF Statement? The Excel IF Statement tests a given condition and returns one value for a TRUE result and another value for a FALSE result. For example, if sales total more than $5,000, then return a “Yes” for Bonus – Otherwise, return a “No” for Bonus.
Very east to test
? iif(true, 1, 1/0) 'run-time error: division by zero
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
In a cell: =IF(TRUE,truepart(),falsepart())
Only get one msgbox per calculation of the IF() cell.
As further validation, this gives you two msgbox - one for each:
Sub Tester()
Debug.Print IIf(True, TruePart(), FalsePart())
End Sub
It does not.
Excel 2013 only evaluates the necessary code.
I had a very complex and time consuming cell formula to copy through a couple hundred thousand rows. It would take a few hours to calculate. But fortunately, it was easy to determine based on some other criteria when the result would be zero.
So using an If Statement to avoid the calculation when other criteria suggested it must be zero, and performing the calculation only when necessary sped up the process immensely, cutting processing time to about 10% of the previous.
If Excel were evaluating both expressions, the If Statement would only have added complexity and time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With