Sub topic 7
Combining Conditions with AND, OR, and NOT
This chapter assumes you can write a basic IF formula with a logical test, a value if true, and a value if false. It also assumes you know comparison operators such as >= and <>, and that you can copy a formula down a column. Here you will learn to ask several questions inside one IF.
Why one test is not always enough
A single comparison answers one question, such as "Is the score at least 60?" Real decisions usually involve more than one fact:
- A student passes only if both exam scores reach 60.
- A customer gets priority service if they are Gold or Platinum.
- An order counts as active if its status is anything other than "Cancelled".
Excel provides three logical functions for these cases: AND, OR, and NOT. Each one takes one or more conditions and returns a single TRUE or FALSE. That result is what the first argument of IF needs.
How the three functions decide
The table below shows how AND and OR treat two conditions. Condition 1 and Condition 2 stand for any two logical tests.
| Condition 1 | Condition 2 | AND | OR |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| FALSE | TRUE | FALSE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
ANDreturnsTRUEonly when every condition is true.ORreturnsTRUEwhen at least one condition is true.NOTtakes one condition and flips it.NOT(TRUE)isFALSE, andNOT(FALSE)isTRUE.
Both AND and OR accept up to 255 conditions, separated by commas.
The diagram shows the logic of AND. Excel does not actually stop at the first failed check, a point covered in the edge cases below.
Using AND inside IF
Place the AND function where the logical test normally goes. Each condition is a separate argument of AND.
=IF(AND(B2>=60,C2>=60),"Pass","Fail")Suppose B2 holds a mathematics score and C2 holds a science score:
| Row | B2 (Maths) | C2 (Science) | Result |
|---|---|---|---|
| 2 | 72 | 65 | Pass |
| 3 | 72 | 55 | Fail |
| 4 | 40 | 90 | Fail |
Row 4 fails even though the science score is high, because AND needs every condition to be true.
A common use of AND is to test whether a value falls inside a range. Write both limits as separate conditions:
=IF(AND(B2>=60,B2<=100),"Valid pass","Check score")Do not write 60<=B2<=100. Excel reads it left to right. It first evaluates 60<=B2 as TRUE or FALSE, then compares that result with 100. A logical value is never less than a number, so the answer is almost always wrong, and Excel gives no warning.
Using OR inside IF
OR has the same shape. The test is true as soon as one condition is true.
=IF(OR(D2="Gold",D2="Platinum"),"Priority","Standard")Each condition must be a complete comparison. This is the most common beginner error with OR:
=IF(OR(D2="Gold","Platinum"),"Priority","Standard")This formula does not test whether D2 equals "Platinum". The second argument is just the text "Platinum", which is not a logical value. Excel returns #VALUE! when OR receives text it cannot read as a logical value. Repeat the cell reference and the operator in every condition.
If you must compare one cell against many allowed values, OR grows long. Later chapters and a lookup table are better options for long lists. For two or three values, OR is clear and easy to check.
Using NOT inside IF
NOT reverses a test. It is most useful when the thing you want to exclude is easier to describe than the things you want to include.
=IF(NOT(D2="Cancelled"),B2,0)This returns the value in B2 unless D2 says "Cancelled". For a simple comparison, NOT(D2="Cancelled") is the same as D2<>"Cancelled", and the <> form is shorter. NOT earns its place when you wrap a whole AND or OR:
=IF(NOT(OR(D2="Cancelled",D2="Returned")),B2,0)This reads: "If the status is neither Cancelled nor Returned, return B2. Otherwise return 0."
NOT also works on a number, which Excel treats as a logical value. Zero counts as FALSE and any other number counts as TRUE. So NOT(0) returns TRUE. You rarely need this, but it explains odd results when a formula passes a number to NOT.
Mixing AND and OR
You can put OR inside AND, or AND inside OR. Work from the inside out and let each function return TRUE or FALSE to the one around it.
=IF(AND(B2>=1000,OR(D2="East",D2="West")),"Bonus","No bonus")Here the sale must be at least 1000, and the region must be East or West. Both parts must hold, so AND is the outer function. The region check is itself a choice between two values, so it is an inner OR.
When you build a formula like this, follow three steps:
- Say the rule in plain words: "Sales of at least 1000 in East or West."
- Find the words "and" and "or" and decide which one joins the biggest pieces. That one becomes the outer function.
- Write each piece as its own small test, then place the pieces inside the functions.
Comparing text inside AND and OR
Text rules from the earlier chapter still apply inside these functions. Text must be in double quotes, and the comparison ignores case, so D2="gold" matches "Gold", "GOLD", and "gold". Spaces matter, however. A cell containing "Gold " with a trailing space does not equal "Gold". If values come from pasted data, wrap the reference in TRIM:
=IF(OR(TRIM(D2)="Gold",TRIM(D2)="Platinum"),"Priority","Standard")Edge cases and gotchas
AND and OR do not skip conditions
In many programming languages, A AND B stops as soon as A is false. Excel's AND and OR evaluate every argument. This matters when a later condition can produce an error:
=IF(AND(C2<>0,B2/C2>1),"Above 1","Not above 1")When C2 is 0, the first condition is FALSE, but Excel still calculates B2/C2 and gets #DIV/0!. That error passes through AND and the formula shows an error. The fix is to nest two IF functions so the risky division only runs when it is safe:
=IF(C2=0,"Not above 1",IF(B2/C2>1,"Above 1","Not above 1"))Blank cells count as zero or empty text
A blank cell compared with a number behaves like 0, so B2>=60 is FALSE for a blank B2. A blank cell compared with "" is equal to it. If a blank score should not produce "Fail", test for blanks first:
=IF(OR(B2="",C2=""),"",IF(AND(B2>=60,C2>=60),"Pass","Fail"))This returns an empty string when either score is missing. Otherwise it runs the pass test.
Ranges inside AND and OR
AND and OR accept a range as one argument when the range already contains logical values. If a range holds numbers or text, Excel ignores the text and empty cells. It treats the numbers as TRUE or FALSE. If the range contains nothing usable, you get #VALUE!. To test a condition against every cell in a range, use a function built for the job, such as COUNTIF, and not AND(B2:B6>=60). In older Excel versions the range form needs array entry to work correctly.
Order of the logic
NOT applies only to the condition inside its own parentheses. NOT(B2>5) is the opposite of B2>5. Writing NOT B2>5 without parentheses is a syntax error. Make sure each opening parenthesis has a closing one. The formula bar highlights matching pairs as you move the cursor, which helps with deeply nested formulas.
Quick troubleshooting guide
| Symptom | Likely cause | Fix |
|---|---|---|
| `#VALUE!` from `OR` | Condition is bare text, as in `OR(D2="Gold","Platinum")` | Write the full comparison for each value |
| Always returns the false result | `AND` used where `OR` was meant | Reread the rule: does any one condition suffice? |
| Always returns the true result | `OR` used where `AND` was meant, or a text value is not matching and `NOT` flips it | Test each condition in a helper cell |
| `Excel found a problem with this formula` | Missing parenthesis or quote | Count the parentheses and check each opening quote has a closing quote |
| Passes a value that should fail | Range written as `60<=B2<=100` | Use `AND(B2>=60,B2<=100)` |
| Text value not matching | Extra spaces or different spelling | Use `TRIM` and check spelling |
Exercises
Exercise
Write a formula for row 2 that returns "Eligible" when the age in B2 is at least 18 and the country in C2 is "UK", and "Not eligible" otherwise.
Show answer
=IF(AND(B2>=18,C2="UK"),"Eligible","Not eligible")
Exercise
A formula =IF(OR(A2="Red","Blue"),"Warm or cool","Other") returns #VALUE! for some rows. Explain why and write the corrected formula.
Show answer
The second argument of OR is only the text "Blue", not a comparison, so OR cannot read it as TRUE or FALSE. Each argument needs its own comparison: =IF(OR(A2="Red",A2="Blue"),"Warm or cool","Other")
Exercise
Write a formula that returns the value in B2 when the status in D2 is neither "Cancelled" nor "Returned", and 0 otherwise. Use NOT.
Show answer
=IF(NOT(OR(D2="Cancelled",D2="Returned")),B2,0)
Exercise
Sales are in B2 and region is in D2. A bonus of 10% of sales applies when sales are at least 1000 and the region is "East" or "West". Otherwise the bonus is 0. Write the formula.
Show answer
=IF(AND(B2>=1000,OR(D2="East",D2="West")),B2*0.1,0)
Exercise
The formula =IF(AND(C2<>0,B2/C2>1),"Above","Not above") shows #DIV/0! when C2 is 0. Why does AND not protect the division, and how do you fix it?
Show answer
AND evaluates all of its arguments, so B2/C2 is still calculated when C2 is 0. Use nested IF so the division runs only when it is safe: =IF(C2=0,"Not above",IF(B2/C2>1,"Above","Not above"))