Sub topic 7
Testing several conditions with AND, OR, and NOT
This chapter assumes you can already write a basic IF(test, value_if_true, value_if_false) formula. It also assumes you know the comparison operators (=, <>, >, <, >=, <=) and the difference between relative references like B2 and absolute references like $F$1. Here you will learn to make IF decide using more than one test.
Why one test is not always enough
A single comparison such as B2>=50 answers one question. Real rules usually combine several:
- A student passes only if both the math score and the English score reach 50.
- An order ships free if the total is over 100 or the customer is a member.
- A task needs attention if it is not marked "Done".
IF accepts only one test in its first argument. To combine questions, you put a helper function inside that argument. The helper reduces several comparisons to a single TRUE or FALSE, and IF then acts on that result.
The three helper functions
| Function | Syntax | Returns TRUE when |
|---|---|---|
| AND | `AND(test1, test2, ...)` | Every test is TRUE |
| OR | `OR(test1, test2, ...)` | At least one test is TRUE |
| NOT | `NOT(test)` | The test is FALSE |
AND and OR take two or more tests. NOT takes exactly one. All three return TRUE or FALSE and nothing else, so on their own they work in a cell, but they are most useful inside IF.
Truth tables show every combination for two tests, A and B:
| A | B | AND(A, B) | OR(A, B) | NOT(A) |
|---|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE | FALSE |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE | FALSE | TRUE |
AND: every test must pass
Suppose column B holds a math score and column C holds an English score. A student passes only if both are at least 50:
=IF(AND(B2>=50, C2>=50), "Pass", "Fail")If B2 is 72 and C2 is 48, the first test is TRUE and the second is FALSE. AND returns FALSE, so IF returns "Fail". One failed test is enough to make AND fail.
You can add more tests. This version also requires an attendance percentage in D2 of at least 80:
=IF(AND(B2>=50, C2>=50, D2>=80), "Pass", "Fail")OR: at least one test must pass
Replacing AND with OR changes the rule. Now one passing score is enough:
=IF(OR(B2>=50, C2>=50), "Pass", "Fail")With B2 at 72 and C2 at 48, OR returns TRUE and the result is "Pass". The result is "Fail" only when every test is FALSE.
OR suits text matching well. To flag rows whose status is any of several values:
=IF(OR(A2="Late", A2="Overdue", A2="Disputed"), "Follow up", "OK")Text comparison with = ignores case, so "late" and "LATE" both match. It does not ignore extra spaces, so "Late " with a trailing space does not match "Late".
NOT: flip a result
NOT turns TRUE into FALSE and FALSE into TRUE. This formula flags every status except "Paid":
=IF(NOT(A2="Paid"), "Follow up", "Done")For a single comparison, NOT(A2="Paid") is the same as A2<>"Paid", and the <> form is shorter. NOT earns its place when you want to reverse a whole group of tests. This formula returns "Needs review" unless both scores reach 50:
=IF(NOT(AND(B2>=50, C2>=50)), "Needs review", "OK")The same logic can be written without NOT by reversing each comparison and switching AND to OR:
=IF(OR(B2<50, C2<50), "Needs review", "OK")"Not (both pass)" means "at least one fails". This equivalence, known as De Morgan's law, is useful whenever a negated formula becomes hard to read. Note that the reverse of >= is <, not <=. Using <= would wrongly treat a score of exactly 50 as failing.
Combining AND, OR, and NOT
You can nest these functions inside each other. Each function needs its own parentheses, and the order of nesting sets the meaning. Consider a discount rule: the customer must be a member, and they must either spend at least 100 or hold a "Gold" tier.
=IF(AND(A2="Member", OR(B2>=100, C2="Gold")), "Discount", "No discount")Read it from the inside out. OR(B2>=100, C2="Gold") is TRUE if either is true. AND then requires that result and A2="Member" to both be TRUE. IF finally chooses the text.
Getting the grouping wrong changes the answer. These two formulas differ:
=AND(A2="Member", OR(B2>=100, C2="Gold"))=OR(AND(A2="Member", B2>=100), C2="Gold")The first one never gives a discount to a non-member. In the second, any Gold customer passes whether or not they are a member. Always check which reading matches the rule you were given.
Testing a range of values
To check that a value lies between two limits, use AND with two comparisons:
=IF(AND(B2>=10, B2<=20), "In range", "Out of range")Writing 10<=B2<=20 as in a math textbook does not work. Sheets evaluates 10<=B2 first, gets TRUE or FALSE, and then compares that to 20, which does not test your range.
To catch values outside a range, use OR. This is a common data-validation check:
=IF(OR(B2<0, B2>100), "Invalid score", "OK")Blank cells inside AND and OR
A blank cell compared to a number behaves like 0, so B2>=50 is FALSE for an empty B2. Often that is fine, but it can hide missing data behind a "Fail". To separate "missing" from "failed", add a blank test to an AND:
=IF(AND(B2<>"", C2<>""), IF(AND(B2>=50, C2>=50), "Pass", "Fail"), "Incomplete")The outer IF checks that both cells have values. Only then does the inner IF grade the row. If a row may be entirely empty, return an empty string for it, as in IF(A2="", "", ...).
Errors and evaluation
IF evaluates only the branch it needs. AND and OR do not work that way: Sheets evaluates all their arguments. If any argument is an error, the whole AND or OR is an error, even when another argument already settles the answer. For example, this formula returns #DIV/0! when C2 is 0, even though the first test is FALSE:
=IF(AND(C2<>0, B2/C2>0.5), "High", "Low")The usual fix is to nest IF so the risky division runs only after the guard passes:
=IF(C2=0, "Check data", IF(B2/C2>0.5, "High", "Low"))Do not rely on the order of arguments inside AND as a guard. It is not one.
Multiplication and addition as shortcuts
You may see formulas that use * for AND and + for OR:
=IF((B2>=50)*(C2>=50), "Pass", "Fail")TRUE counts as 1 and FALSE as 0 in arithmetic, so the product is 1 only if both tests are TRUE. IF treats any nonzero number as TRUE. This style matters in one place: AND and OR collapse everything into a single answer, so they do not work row by row inside ARRAYFORMULA. The multiplication form does work there. A later chapter returns to this. For ordinary formulas, prefer AND and OR because they are easier to read.
Common mistakes
- Quotes around text.
AND(A2=Paid, B2>0)fails becausePaidis read as a name. Write"Paid". - Quotes around numbers.
B2>="50"compares to the text "50". WriteB2>=50without quotes. - Wrong closing parentheses. In
IF(AND(B2>=50, C2>=50), "Pass", "Fail"), theANDcloses before the comma that starts theIFvalue. If you place the comma inside theAND, then "Pass" becomes a third test. - Mixing up AND and OR. "Neither is blank" needs
AND. "Either is blank" needsOR. Say the rule aloud in words before choosing. - Using NOT with several arguments.
NOT(A2="x", B2="y")is an error. Wrap the tests inANDorORfirst. - Relative references when copying. If a threshold lives in a cell, anchor it, as in
AND(B2>=$F$1, C2>=$F$2), so it stays put when you fill the formula down.
Exercises
Exercise
A sheet has an order total in B2 and a customer type in C2. Write a formula that returns "Free shipping" when the total is at least 100 or the customer type is "Member", and "Standard" otherwise.
Show answer
=IF(OR(B2>=100, C2="Member"), "Free shipping", "Standard")
Exercise
This formula should return "Review" when a score in B2 is below 0 or above 100, but it returns "OK" for a score of 150. What is wrong, and what is the fix? =IF(AND(B2<0, B2>100), "Review", "OK")
Show answer
AND requires both tests to be TRUE, and no number is both below 0 and above 100, so the result is always FALSE. The rule needs OR: =IF(OR(B2<0, B2>100), "Review", "OK")
Exercise
Rewrite =IF(NOT(AND(B2>=50, C2>=50)), "Needs review", "OK") without using NOT or AND.
Show answer
=IF(OR(B2<50, C2<50), "Needs review", "OK"). Reversing "both pass" gives "at least one fails", so AND becomes OR and each comparison is reversed (>= becomes <).