Excel course · Lesson 17 of 34
IF and logical formulas: AND, OR, NOT, IFS and SWITCH
Make decisions in formulas with IF, combine conditions with AND, OR and NOT, replace nested IFs with IFS, SWITCH or a lookup table, and avoid common traps.
Logical formulas let a sheet make decisions: pass or fail, bonus or no bonus, overdue or on time. They all rest on one idea: a test that is either TRUE or FALSE. Master that and IF, AND, OR, IFS and SWITCH become easy.
Logical tests
A logical test is any expression that returns TRUE or FALSE, usually with a comparison operator: B2>=50, C2="Paid", D2<TODAY(), E2<>"" (not empty). Type =B2>=50 into a cell and Excel shows TRUE or FALSE. Behind the scenes TRUE behaves like 1 and FALSE like 0 in arithmetic, a fact that becomes very useful later.
The IF function
IF(logical_test, value_if_true, [value_if_false])| A | B | C | D | |
|---|---|---|---|---|
| 1 | Student | Exam | Attendance % | Result |
| 2 | Ana | 91 | 95 | Pass |
| 3 | Ben | 58 | 80 | Pass |
| 4 | Carla | 74 | 62 | Pass |
| 5 | David | 45 | 90 | Fail |
| 6 | Elif | 82 | 98 | Pass |
Pass mark 50. D2 contains =IF(B2>=50,"Pass","Fail"), filled down. David (45) fails.
Result: Pass
- Click
D2and type=IF(. - Type the test
B2>=50and a comma. - Type what to show when the test is TRUE,
"Pass", and a comma. Text needs double quotes; numbers and formulas do not. - Type what to show when it is FALSE,
"Fail", close the parenthesis and press Enter. - Double-click the fill handle to copy the formula down.
The results can be calculations too. A 10% bonus for sales over the target in $F$1: =IF(B2>$F$1, B2*10%, 0). If you omit the third argument and the test is FALSE, IF returns the word FALSE, which looks untidy, so always give both values.
=IF(B2="", "", B2*1.24). This is the standard way to avoid zeros or errors in rows that are not filled in yet.AND, OR and NOT
| Function | Returns TRUE when | Example |
|---|---|---|
| AND(test1, test2, …) | every test is TRUE | =AND(B2>=50, C2>=75) |
| OR(test1, test2, …) | at least one test is TRUE | =OR(B2>=90, C2=100) |
| NOT(test) | the test is FALSE | =NOT(C2="Paid") |
Combine them with IF. To pass, a student needs an exam score of at least 50 and attendance of at least 75%:
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Student | Exam | Attendance % | Result |
| 2 | Ana | 91 | 95 | Pass |
| 3 | Ben | 58 | 80 | Pass |
| 4 | Carla | 74 | 62 | Fail |
| 5 | David | 45 | 90 | Fail |
| 6 | Elif | 82 | 98 | Pass |
Carla scored 74 but attended only 62%, so she fails. Ben passes with 58 and 80%.
Result: Fail
For an honours list, a student qualifies with an exam score of 90 or more or perfect-ish attendance of 95% or more: =IF(OR(B2>=90, C2>=95), "Honours", ""). Ana (91, 95) and Elif (82, 98) qualify.
Nested IF
When there are more than two outcomes, you can place an IF inside the value_if_false of another IF. A letter grade with five bands:
=IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", IF(B2>=60, "D", "F"))))Excel checks from left to right and stops at the first TRUE test. The order matters: test the highest band first. If you started with B2>=60, a score of 95 would get a D, because 95 is also ≥ 60.
| A | B | C | |
|---|---|---|---|
| 1 | Student | Exam | Grade |
| 2 | Ana | 91 | A |
| 3 | Ben | 58 | F |
| 4 | Carla | 74 | C |
| 5 | David | 45 | F |
| 6 | Elif | 82 | B |
Result: A
IFS: several conditions without nesting
=IFS(B2>=90, "A", B2>=80, "B", B2>=70, "C", B2>=60, "D", TRUE, "F")IFS takes pairs of test and result and returns the result of the first TRUE test. The last pair TRUE, "F" acts as the 'everything else' case; without it, a score below 60 would return #N/A. IFS is available in Microsoft 365, Excel 2019 and later.
SWITCH: match one value against a list
When you compare one cell with several exact values, SWITCH is cleaner than IF or IFS:
=SWITCH(C2, "N", "North", "S", "South", "E", "East", "W", "West", "Unknown")The first argument is the value to check, then pairs of match and result, and an optional default at the end. SWITCH only tests equality; for ranges such as scores, use IFS. Available in Microsoft 365 and Excel 2019 and later.
Often better: a lookup table
Grade bands, commission tiers and tax brackets change. Instead of hard-coding them in a formula, put them in a small table and use an approximate lookup, as shown in Lookup guide: VLOOKUP vs INDEX/MATCH vs XLOOKUP: with lower limits 0, 60, 70, 80, 90 in F2:F6 and grades F, D, C, B, A in G2:G6, the formula =XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, , -1) gives the same grades, and changing a band means editing a cell, not a formula.
Handling errors with IFERROR and IFNA
IFERROR returns an alternative when a formula produces any error: =IFERROR(B2/C2, 0) returns 0 instead of #DIV/0! when C2 is empty. IFNA catches only #N/A, the 'not found' error from lookups. Use them deliberately; hiding errors you do not understand hides bugs. See Formula errors: what each one means and how to fix it.
Useful tests with IS functions
=ISBLANK(A2)— TRUE if A2 is completely empty. Note that a formula returning""is not blank; testA2=""instead.=ISNUMBER(A2)— TRUE for numbers and real dates; great for spotting numbers stored as text.=ISTEXT(A2),=ISERROR(A2),=ISFORMULA(A2).=ISNUMBER(SEARCH("refund", B2))— TRUE if B2 contains the word refund anywhere, case-insensitive.
Common mistakes
- Writing
=IF(B2>=50, Pass, Fail)without quotes. Excel thinks Pass is a name and returns#NAME?. - Writing a range test as
=IF(50<=B2<=80, …). Excel does not read chained comparisons; useAND(B2>=50, B2<=80). - Comparing to a percentage typed as a whole number: if C2 holds 75%, the test is
C2>=0.75orC2>=75%, notC2>=75. - Typing numbers in quotes:
B2>="50"compares a number with text and gives unexpected results.
Check yourself
1. What does =IF(B2>=50, "Pass") return when B2 is 40?
With no value_if_false argument, IF returns FALSE.
2. A student has exam 58 and attendance 80%. What does =IF(AND(B2>=50, C2>=75%), "Pass", "Fail") return?
Both tests are TRUE, so AND returns TRUE.
3. Why must a nested grade formula test >=90 before >=60?
A score of 95 also passes the >=60 test; testing that first would return the wrong grade.
4. In =IFS(B2>=90,"A",B2>=80,"B"), what happens if B2 is 50?
If no test is TRUE, IFS returns #N/A. Add TRUE,"F" as the last pair.
5. Which formula correctly checks that B2 is between 50 and 80 inclusive?
Excel needs AND to combine the two comparisons.