Sub topic 2
Asking a yes-or-no question with comparison operators
This chapter covers the logical test, the part of an IF formula that decides which result you get. It assumes you can enter values and formulas in cells, read references such as A1 and B2, and copy a formula down a column with the fill handle. You do not need to know the IF function yet. Chapter 3 introduces it. Here you learn to write the question that IF will later ask.
What a logical test is
A logical test is a formula that asks a yes-or-no question about your data. Excel answers with one of two logical values: TRUE or FALSE. These are not text and not numbers. They are a separate data type called Boolean values, and Excel treats them as its own kind of result.
Type this into any cell:
=5>3The cell shows TRUE, because 5 is greater than 3. Now try:
=5<3The cell shows FALSE. In both cases the formula contains no function. The > and < symbols are comparison operators, and a formula built from them returns TRUE or FALSE.
Every IF formula starts with a test like this. IF reads the TRUE or FALSE and picks a result. If you can write a test that returns the right answer for each row, you have done the hard part of writing IF.
The six comparison operators
Excel has six comparison operators.
| Operator | Meaning | Example | Result when A1 is 10 |
|---|---|---|---|
| `=` | Equal to | `=A1=10` | TRUE |
| `<>` | Not equal to | `=A1<>10` | FALSE |
| `>` | Greater than | `=A1>10` | FALSE |
| `<` | Less than | `=A1<10` | FALSE |
| `>=` | Greater than or equal to | `=A1>=10` | TRUE |
| `<=` | Less than or equal to | `=A1<=10` | TRUE |
Two details trip people up.
First, the = in =A1=10 plays two roles. The first = tells Excel that the cell holds a formula. The second = is the comparison operator. Excel reads =A1=10 as "compute whether A1 equals 10".
Second, the operators that combine two symbols must be typed in a fixed order. Write >=, not =>. Write <=, not =<. Write <>, not ><. Excel rejects the reversed forms with a formula error or a prompt to correct the formula.
Comparing a cell with a value
Most tests compare a cell with a fixed value. Suppose column B holds exam scores and 60 is the pass mark. Enter these scores in B2:B6:
| Row | B (Score) |
|---|---|
| 2 | 85 |
| 3 | 60 |
| 4 | 59 |
| 5 | 0 |
| 6 | 72 |
In C2, enter:
=B2>=60Copy it down to C6. The results are TRUE, TRUE, FALSE, FALSE, TRUE.
Look at row 3. The score is exactly 60, and the test uses >=, so the result is TRUE. Had you used >, row 3 would return FALSE. Choosing between > and >= is the most common source of off-by-one mistakes at a boundary. Before you write a test, decide what should happen at the boundary value itself. Then pick the operator.
Ask these questions about the wording you were given:
- "60 or more" means
>=60. - "More than 60" means
>60. - "Under 60" means
<60. - "60 or less" means
<=60.
Comparing two cells
The right side of a test can also be a cell reference. Suppose B holds actual sales and C holds the target for each row:
=B2>=C2This returns TRUE when sales reach the target in that row. Because both references are relative, copying the formula down changes them to B3>=C3, B4>=C4, and so on. Each row compares its own pair of values, which is what you want here.
Sometimes you want every row compared with one shared cell. That calls for an absolute reference, which a later chapter covers.
Comparing the result of a calculation
You can put a calculation on either side of the operator. Excel evaluates the arithmetic first and compares the result afterward. This is because arithmetic operators have higher precedence than comparison operators.
=B2*1.1>C2This multiplies B2 by 1.1, then asks whether the product is greater than C2. You do not need parentheses for this, though they can make a long test easier to read:
=(B2*1.1)>C2Comparing text
The = and <> operators also work on text. Text in a formula must be wrapped in double quotation marks.
=A2="Yes"This returns TRUE when A2 contains the word Yes. Without the quotes, Excel would look for a named range called Yes, and the formula would fail with a #NAME? error.
Excel's text comparison ignores upper and lower case. If A2 holds yes, YES, or Yes, the test above returns TRUE for all three. The comparison does not ignore extra spaces. A cell holding Yes with a trailing space does not equal Yes, and the test returns FALSE. This is a frequent cause of tests that "should" work. Chapter 5 covers text comparison in more detail.
Text can also be compared with > and <. Excel compares alphabetically, so ="apple"<"banana" returns TRUE. You will rarely need this for simple decisions.
Numbers stored as text
A test can also fail because the cell looks like a number but is stored as text. Imported data often has this problem. A cell showing 85 may hold the text "85". It usually appears left-aligned, and Excel may show a green triangle in the corner.
Excel orders data types in comparisons. Any text is treated as greater than any number. So with text "85" in B2, the test =B2>=60 returns TRUE, and so would =B2>=1000000. The result looks plausible but is wrong.
You can check what a cell holds with the ISNUMBER function:
=ISNUMBER(B2)This returns TRUE for a real number and FALSE for text. If you find text-numbers, convert them with Data > Text to Columns, or multiply the column by 1 using Paste Special.
Testing for blank cells
Two ways of testing for a blank cell appear often.
=A2=""This returns TRUE when A2 is empty, and also when A2 holds an empty string, such as the result of a formula that returns "". The second form is:
=ISBLANK(A2)ISBLANK returns TRUE only when the cell is truly empty. A cell holding a formula that returns "" is not blank to ISBLANK. For most simple decisions, =A2="" is the safer choice, because it treats both cases the same way.
An empty cell also behaves like zero in a numeric comparison. If A2 is empty, =A2=0 returns TRUE and =A2<60 returns TRUE. This matters when you grade scores. A student with no score entered looks like a student who scored zero. A later chapter shows how to handle blanks deliberately.
Seeing how Excel evaluates a test
When a test gives an unexpected answer, break it into parts. Excel has a tool for this. Select the cell, go to the Formulas tab, and choose Evaluate Formula. Each click on Evaluate shows the next step of the calculation. You can watch B2>=60 turn into 85>=60 and then into TRUE.
A quicker habit is to test in a spare column. Before wrapping a test in IF, enter the bare test in its own column and check that the TRUE and FALSE values match your expectation for every row, especially at the boundaries.
The flow below shows how Excel resolves a simple test.
Common errors in logical tests
The following table lists the problems you are most likely to hit at this stage.
| Symptom | Likely cause | Fix |
|---|---|---|
| `#NAME?` in a text test | Missing quotation marks around the text | Write `="Yes"`, not `=Yes` |
| `FALSE` for text that looks equal | Extra spaces in the cell | Use `TRIM`, or clean the data |
| `TRUE` for a score that should fail | Number stored as text | Convert to a real number |
| Wrong result only at the boundary | `>` used where `>=` was meant | Re-read the wording and choose again |
| Formula is rejected | Operator typed as `=>`, `=<`, or `!=` | Use `>=`, `<=`, and `<>` |
| Blank cell treated as zero | Empty cells compare as 0 | Test for `""` first |
Putting it together
Here is a short worked example. A sheet lists employees with a department in column A and years of service in column B.
| Row | A (Department) | B (Years) |
|---|---|---|
| 2 | Sales | 7 |
| 3 | Support | 2 |
| 4 | Sales | 5 |
| 5 | support | 5 |
You want a column of tests for "five or more years of service". In C2, enter:
=B2>=5Copy it down. The results are TRUE, FALSE, TRUE, TRUE.
Now you want a test for "works in Support". In D2, enter:
=A2="Support"Copy it down. The results are FALSE, TRUE, FALSE, TRUE. Row 5 returns TRUE even though the cell holds lowercase support, because the comparison ignores case.
Each of these formulas is a complete logical test. In the next chapter you will place such a test inside IF and tell Excel what to return for TRUE and for FALSE.
Exercise
Column B holds scores. A teacher says, "Students who score more than 70 get a merit." The score 70 itself should not earn a merit. Write the logical test for B2, then state what it returns when B2 is 70, and what a test using >=70 would return for the same score.
Show answer
The test is =B2>70. When B2 is 70 it returns FALSE, because 70 is not greater than 70. A test using >=70 returns TRUE for 70, which would wrongly give the merit to a score of exactly 70.
Exercise
Cell A2 holds the text "Approved" followed by one trailing space. The formula =A2="Approved" returns FALSE. Explain why, and name one way to find the cause.
Show answer
Excel's text comparison ignores case but not spaces, so "Approved " with a trailing space does not equal "Approved". You can find the cause with =LEN(A2), which returns 9 instead of 8, or by using Evaluate Formula. Wrapping the cell in TRIM, as in =TRIM(A2)="Approved", fixes it.
Exercise
B2 holds the text "85", stored as text rather than as a number. What does =B2>=60 return, and why is that misleading?
Show answer
It returns TRUE, because Excel treats any text as greater than any number in a comparison. It is misleading because the result would be TRUE for any threshold, including 1000000. Use =ISNUMBER(B2) to detect the problem and convert the cell to a real number.