Sub topic 2
Asking a yes-or-no question with comparison operators
This chapter assumes you can enter values and formulas in cells, use references such as A1 and B2, and copy a formula down a column with the fill handle. It covers the logical test, which is the part of an IF formula that decides which result you get. You will write logical tests on their own first and put them inside IF in the next chapter.
What a logical test is
A logical test is a formula that asks a question with only two possible answers. Excel replies with one of two logical values, TRUE or FALSE. Suppose B2 holds a score of 72. Type this in another cell:
=B2>=60Excel shows TRUE, because 72 is at least 60. If B2 held 45, the cell would show FALSE.
An IF formula reads this answer and picks its result. Every IF formula you write depends on a logical test, so it helps to practice them alone first.
The six comparison operators
A comparison operator sits between two values and states how they should relate. Excel has six.
| Operator | Meaning | Example | Result when B2 is 72 |
|---|---|---|---|
| `=` | equal to | `=B2=72` | TRUE |
| `<>` | not equal to | `=B2<>72` | FALSE |
| `>` | greater than | `=B2>72` | FALSE |
| `<` | less than | `=B2<72` | FALSE |
| `>=` | greater than or equal to | `=B2>=72` | TRUE |
| `<=` | less than or equal to | `=B2<=72` | TRUE |
In the first example, =B2=72, the first = starts the formula. The second = is the comparison. Excel tells them apart by position.
What you can compare
Each side of an operator can be a cell reference, a typed value, or a calculation. All of these are valid logical tests:
=B2>=60
=B2>C2
=B2*C2>=1000
=A2<>"Closed"The second test compares two cells with each other. The third calculates a value first, and Excel evaluates the arithmetic before it compares. Text values go in double quotes, as in the fourth test. A later chapter covers text comparison in detail.
Testing a whole column
Enter a test in the first row of your data and copy it down with the fill handle. Each row's reference shifts to match, so row 3 tests B3, row 4 tests B4, and so on. A column of TRUE and FALSE values gives you a quick check that your question matches your data before you wrap it in IF.
=B2>=60Enter that in C2 and fill it down. You can scan the results and see whether the cutoff behaves the way you expect.
Common errors with operators
Most logical-test problems come from a few habits.
- Reversed symbols. Excel accepts
>=and<=. It does not accept=>or=<, and it shows an error message when you try to enter them. - Programming-style operators.
==and!=are not Excel operators. Use=and<>. - Wrong boundary.
B2>60isFALSEfor a score of exactly 60, whileB2>=60isTRUE. When a rule says "at least" or "60 or higher", use>=. - Numbers stored as text. A value that looks like 72 but is stored as text, often left-aligned, does not behave like a number. Excel ranks all text above all numbers, so
="72">=60style comparisons can returnTRUEeven for a text value like "5". If a test gives surprising results, check that the cell holds a true number.
Summary of the working model
A logical test has three parts: a left value, one of six operators, and a right value. Excel evaluates it to TRUE or FALSE, and nothing else. Once you can predict that answer by reading the test, the IF function is a small step: it takes the answer and returns one result for TRUE and another for FALSE.
Exercise
Cell B2 contains 1000. For each test, say whether Excel returns TRUE or FALSE: (a) =B2>1000, (b) =B2>=1000, (c) =B2<>1000, (d) =B2*2<=2000. Then name the mistake in =B2=>1000.
Show answer
(a) FALSE, because 1000 is not greater than 1000. (b) TRUE, because 1000 equals the boundary. (c) FALSE, because B2 is equal to 1000. (d) TRUE, because 1000*2 is 2000, which is less than or equal to 2000. The formula =B2=>1000 has the symbols in the wrong order. The operator must be written >=, so the correct formula is =B2>=1000.