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.

Intermediate 18 min read

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

Formula
IF(logical_test, value_if_true, [value_if_false])
D3fx=IF(B3>=50, "Pass", "Fail")
ABCD
1StudentExamAttendance %Result
2Ana9195Pass
3Ben5880Pass
4Carla7462Pass
5David4590Fail
6Elif8298Pass

Pass mark 50. D2 contains =IF(B2>=50,"Pass","Fail"), filled down. David (45) fails.

Result: Pass

  1. Click D2 and type =IF(.
  2. Type the test B2>=50 and a comma.
  3. Type what to show when the test is TRUE, "Pass", and a comma. Text needs double quotes; numbers and formulas do not.
  4. Type what to show when it is FALSE, "Fail", close the parenthesis and press Enter.
  5. 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.

Tip. To leave a cell visually empty, return an empty string: =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

FunctionReturns TRUE whenExample
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%:

D4fx=IF(AND(B4>=50, C4>=75), "Pass", "Fail")
ABCD
1StudentExamAttendance %Result
2Ana9195Pass
3Ben5880Pass
4Carla7462Fail
5David4590Fail
6Elif8298Pass

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:

Formula
=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.

C2fx=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C",IF(B2>=60,"D","F"))))
ABC
1StudentExamGrade
2Ana91A
3Ben58F
4Carla74C
5David45F
6Elif82B

Result: A

Watch out. Nested IFs are hard to read and easy to break: one missing parenthesis or a band in the wrong order and results go wrong silently. Beyond three levels, prefer IFS, SWITCH or a lookup table.

IFS: several conditions without nesting

Formula
=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:

Formula
=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; test A2="" 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; use AND(B2>=50, B2<=80).
  • Comparing to a percentage typed as a whole number: if C2 holds 75%, the test is C2>=0.75 or C2>=75%, not C2>=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?

2. A student has exam 58 and attendance 80%. What does =IF(AND(B2>=50, C2>=75%), "Pass", "Fail") return?

3. Why must a nested grade formula test >=90 before >=60?

4. In =IFS(B2>=90,"A",B2>=80,"B"), what happens if B2 is 50?

5. Which formula correctly checks that B2 is between 50 and 80 inclusive?

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB