If Function
If Function
he IF function is one of the most popular functions in Excel, and it allows you to
T
make logical comparisons between a value and what you expect.
o an IF statement can have two results. The first result is if your comparison is
S
True, the second if your comparison is False.
orexample,=IF(C2=”Yes”,1,2)saysIF(C2=Yes,thenreturna1,otherwisereturna
F
2).
Example 1
■ =IF(C2=”Yes”,1,2)
Intheaboveexample,cellD2says:IF(C2=Yes,thenreturna1,otherwisereturna
2)
■ =IF(C2=1,”Yes”,”No”)
In this example, the formula in cell D2 says: IF(C2 = 1,thenreturnYes,otherwise
returnNo)Asyousee,theIFfunctioncanbeusedtoevaluatebothtextandvalues.It
canalsobeusedtoevaluateerrors.Youarenotlimitedtoonlycheckingifonething
is equal to another and returning a single result, you can also use mathematical
operators and perform additional calculations depending on your criteria. You can
also nest multiple IF functions together in order to perform multiple comparisons.
Example 2
Intheaboveexample,theIFfunctioninD2issayingIF(C2IsGreaterThanB2,then
return “Over Budget”, otherwise return “Within Budget”)
■ =IF(C2>B2,C2-B2,0)
In the above illustration, instead of returning a text result, we aregoingtoreturna
mathematical calculation. So the formula in E2 is saying IF(Actual is Greater than
Budgeted, then Subtract the Budgeted amount from the Actual amount, otherwise
return nothing).
Example 3
■ =IF(E7=”Yes”,F5*0.0825,0)
In this example, the formula in F7 is saying IF(E7 = “Yes”,thencalculatetheTotal
Amount in F5 * 8.25%, otherwise no Sales Tax is due so return 0)
ote:Ifyouaregoingtousetextinformulas,youneedtowrapthetextinquotes(e.g.“Text”).
N
The only exception to that is using TRUE or FALSE, which Excel automatically understands.
Common problems
Problem What went wrong
(zero)in
0 here was noargumentforeithervalue_if_trueorvalue_if_False
T
cell arguments.Toseetherightvaluereturned,addargumenttexttothe
two arguments, or add TRUE or FALSE to the argument.
NAME?
# This usually means that the formula is misspelled.
in cell
IF function – nested formulas and avoiding pitfalls
heIFfunctionallowsyoutomakealogicalcomparisonbetweenavalueandwhat
T
you expect by testing for a condition and returning a result if True or False.
o an IF statement can have two results. The first result is if your comparison is
S
True, the second if your comparison is False.
IFstatementsareincrediblyrobust,andformthebasisofmanyspreadsheetmodels,
but they are also the root cause of many spreadsheet issues. Ideally, an IF
statementshouldapplytominimalconditions,suchasMale/Female,Yes/No/Maybe,
to nameafew,butsometimesyoumightneedtoevaluatemorecomplexscenarios
that require nesting* more than 3 IF functions together.
* “Nesting” refers to the practice of joining multiple functions together in one formula.
Technical details
hile Excel will allow you to nest up to 64 different IF functions, it’s not at all
W
advisable to do so. Why?
■ M
ultiple IF statements require a great deal of thought to build correctly
and make sure that their logic can calculate correctly through each
condition all the way to the end. If you don’t nest your formula 100%
accurately, then it might work 75% of the time, but return unexpected
results 25% of the time. Unfortunately, the odds of you catching the 25%
are slim.
■ M
ultiple IF statements can become incredibly difficult to maintain,
especially when you come back some time later and try to figure out what
you, or worse someone else, was trying to do.
IfyoufindyourselfwithanIFstatementthatjustseemstokeepgrowingwithnoend
in sight, it’s time to put down the mouse and rethink your strategy.
et’s look at how to properly create a complex nested IF statement using multiple
L
IFs, and when to recognize that it’s time to use another tool in your Excel arsenal.
Examples
ollowing is an example of a relatively standard nested IF statement to convert
F
student test scores to their letter grade equivalent.
■ =
IF(D2>89,"A",IF(D2>79,"B",IF(D2>69,"C",IF(D2>59,"D","F"))))
This complex nested IF statement follows a straightforward logic:
1. If the Test Score (in cell D2) is greater than 89, then the student gets an A
2. If the Test Score is greater than 79, then the student gets a B
3. If the Test Score is greater than 69, then the student gets a C
4. If the Test Score is greater than 59, then the student gets a D
his particular example is relatively safe because it’s not likely that the correlation
T
between test scores and letter grades will change, so it won’t require much
maintenance. But here’s a thought – what if you need to segment the grades
between A+, A and A-(andsoon)?NowyourfourconditionIFstatementneedsto
be rewritten to have 12 conditions! Here’s what your formula would look like now:
■ =
IF(B2>97,"A+",IF(B2>93,"A",IF(B2>89,"A-",IF(B2>87,"B+",IF(B2>83,"B",
IF(B2>79,"B-",
IF(B2>77,"C+",IF(B2>73,"C",IF(B2>69,"C-",IF(B2>57,"D+",IF(B2>53,"D",I
F(B2>49,"D-","F"))))))))))))
It’s still functionally accurate and will work as expected, but it takes a long time to
writeandlongertotesttomakesureitdoeswhatyouwant.Anotherglaringissueis
that you’ve had to enter thescoresandequivalentlettergradesbyhand.Whatare
theoddsthatyou’llaccidentallyhaveatypo?Nowimaginetryingtodothis64times
with more complex conditions! Sure, it’s possible, butdoyoureallywanttosubject
yourself to this kind of effort and probable errors that will be really hard to spot?
ip: Every function in Excel requires an opening and closing parenthesis (). Excel
T
will try to help you figure out what goes where by coloring different parts of your
formulawhenyou’reeditingit.Forinstance,ifyouweretoedittheaboveformula,as
you move the cursor past each of the ending parentheses “)”, its corresponding
opening parenthesis will turn the same color. This can be especially useful in
complex nested formulas when you’re trying to figure out if you have enough
matching parentheses.