Sub topic 1
Quick refresher: formulas and cell references
Every IF formula is a normal Excel formula with a decision inside it. If you are unsure how Excel reads a formula, or what happens to references when you copy it, the IF formulas in later chapters will fail in confusing ways. This chapter reviews the parts of formulas and cell references that IF depends on. It stays on those parts and does not cover IF itself.
Prerequisites
This chapter assumes you can already do the following:
- Enter values and formulas in worksheet cells.
- Read basic cell references such as A1 and B2.
- Copy a formula down a column with the fill handle, the small square at the bottom-right corner of the selected cell.
If any of these is new to you, practice them in a blank workbook first. Then come back to this chapter.
How Excel reads a formula
A formula is any cell entry that starts with an equals sign (=). Excel evaluates what follows and displays the result in the cell. The formula itself stays visible in the formula bar when you select the cell.
A formula can contain four kinds of pieces:
- Constants: numbers such as
0.1or text in double quotes such as"Pass". - Cell references: addresses such as
B2that point to the value stored in another cell. - Operators: symbols such as
+and>=that combine or compare values. - Functions: named calculations such as
SUM(B2:B10), which take arguments in parentheses.
Here is a small worksheet. Columns A to C hold typed values, and column D holds a formula.
| Row | A | B | C | D |
|---|---|---|---|---|
| 1 | Item | Quantity | Unit price | Line total |
| 2 | Pens | 12 | 1.50 | `=B2*C2` |
| 3 | Notebooks | 5 | 4.00 | `=B3*C3` |
=B2*C2The formula in D2 multiplies the value in B2 by the value in C2 and shows 18. If you change B2 to 20, D2 updates to 30 automatically. This is the main advantage of references: the formula describes a relationship, so the result stays correct when the inputs change.
Operators and the order of evaluation
Excel does not always work from left to right. It follows a fixed order, and parentheses override it.
| Order | Operator | Meaning |
|---|---|---|
| 1 | `-` (before a value) | Negation |
| 2 | `%` | Percent |
| 3 | `^` | Exponent |
| 4 | `*` and `/` | Multiply and divide |
| 5 | `+` and `-` | Add and subtract |
| 6 | `&` | Join text |
| 7 | `=`, `<>`, `>`, `<`, `>=`, `<=` | Comparisons |
Two consequences matter for IF later:
- Comparisons run last. In
B2*2>=100, Excel first doubles B2 and then compares the result with 100. You do not need parentheses to get that behavior. - Negation runs before the exponent.
=-2^2returns 4, not -4, because Excel makes the 2 negative first. Write=-(2^2)when you want -4.
=(B2+C2)*0.5Without the parentheses, Excel would multiply C2 by 0.5 first and then add B2. When a formula mixes operators, add parentheses even if the default order would work. They make your intent clear to the next reader.
Comparison operators produce TRUE or FALSE
The comparison operators are the bridge to IF. A comparison does not return a number or text. It returns a logical value, which is either TRUE or FALSE.
=B2>=10If B2 contains 12, this formula returns TRUE. If B2 contains 8, it returns FALSE. You can type a comparison into any cell to see its result. This is a quick way to test a condition before you put it inside a larger formula.
Cell references
A reference tells Excel where to look. The behavior that confuses people is what happens to a reference when you copy the formula to another cell.
Relative references
A reference such as B2 is relative. Excel does not remember "column B, row 2". It remembers "the cell two columns to the left in the same row", measured from the cell holding the formula. When you copy the formula, Excel keeps that relative offset.
This is why filling =B2*C2 down from D2 works. In D3 the formula becomes =B3*C3, in D4 it becomes =B4*C4, and so on. Copying across changes columns instead. Copy =B2*C2 one cell to the right, from D2 to E2, and it becomes =C2*D2.
Absolute references
Sometimes every copy of a formula must point to the same cell. A tax rate typed once in F1 is a typical example. An absolute reference adds a dollar sign before the column letter and the row number: $F$1. The dollar signs lock the reference, so it does not change when you copy the formula.
=D2*$F$1Copy this formula down and the D part changes (D3, D4, and so on) while $F$1 stays fixed. Without the dollar signs, the reference would drift to F2, F3, and so on. Those cells are probably empty, so you would get zeros instead of a tax amount.
When you are editing a formula, click inside a reference and press F4 to add the dollar signs. Pressing F4 again cycles through the four forms. On some laptops you need to hold the Fn key as well.
Mixed references
A mixed reference locks only one part. $A1 locks the column and lets the row change. A$1 locks the row and lets the column change. Mixed references are useful in tables that fill both across and down, but you will rarely need them for the IF formulas in this tutorial.
| Form | Column when copied | Row when copied | Typical use |
|---|---|---|---|
| `B2` | Changes | Changes | Values in the same row |
| `$B$2` | Fixed | Fixed | A single threshold or rate |
| `$B2` | Fixed | Changes | Fill down from a fixed column |
| `B$2` | Changes | Fixed | Fill across from a fixed row |
Ranges and function arguments
A range is a block of cells written with a colon. B2:B10 means the nine cells from B2 down to B10. Functions often take ranges as arguments.
=SUM(B2:B10)A function call has a name, an opening parenthesis, arguments separated by commas, and a closing parenthesis. Each opening parenthesis needs a matching closing one. When you type a formula, Excel colors matching pairs, which helps you spot a missing one.
Your regional settings can change one detail. In some locales Excel uses semicolons instead of commas between arguments. This tutorial uses commas. If Excel rejects a formula with commas, check whether your version expects semicolons.
Functions can contain other functions. =SUM(B2:B10)/COUNT(B2:B10) uses two functions in one formula. IF relies on this ability, because its test and results can themselves be formulas.
What a cell can contain
A formula's result depends on the type of data in the cells it reads. The three types you will meet most often are:
- Numbers, which Excel right-aligns by default.
- Text, which Excel left-aligns by default. In a formula, typed text must be wrapped in double quotes, as in
"Yes". - Blanks, which are cells with nothing in them.
Two details here cause many IF problems later.
First, a number that is stored as text looks like a number but behaves differently. Imagine A1 contains the text 5, perhaps imported from another system. A green triangle often appears in the corner, and the value is left-aligned. The comparison =A1=5 returns FALSE, because the text "5" and the number 5 are different values. If a comparison fails on values that look equal, check how the cells are aligned and whether they show the green triangle.
Second, a blank cell is not always treated as blank. In a comparison, an empty cell equals both 0 and the empty string:
=A1=0=A1=""If A1 is truly empty, both formulas return TRUE. The chapter on blank cells covers this in detail. For now, remember that "empty" and "zero" can look the same on screen while being different things.
Checking what a formula is doing
You can rely on a few built-in tools instead of guessing why a formula returns an unexpected result:
- F2 puts a cell into edit mode and outlines each referenced cell in a matching color. You can see at a glance whether the references point where you intended. Press Esc to leave without changing anything.
- Show Formulas (Formulas tab, or Ctrl+` ) displays the formula text in every cell instead of the results. Use it to compare a row that works with a row that does not.
- Evaluate Formula (Formulas tab) steps through a formula one calculation at a time. It is most useful once formulas contain nested functions.
- Trace Precedents (Formulas tab) draws arrows from a formula to the cells it reads.
Common errors to recognize
When a formula cannot produce a result, Excel shows an error value that starts with #. These are the ones you will see most often:
| Error | Usual cause |
|---|---|
| `#DIV/0!` | The formula divides by zero or by an empty cell. |
| `#NAME?` | Excel does not recognize a function name or a piece of text outside quotes. |
| `#VALUE!` | The formula does arithmetic on something that is not a number, such as text that cannot be converted. |
| `#REF!` | A referenced cell was deleted, or a reference points outside the worksheet. |
Two other situations look like errors but are not:
- A cell full of
#####means the column is too narrow to display the result. Widen the column. - A circular reference warning means a formula refers to its own cell, directly or through other cells. Excel cannot finish the calculation. Edit the formula so it no longer reads the cell it sits in.
Why this matters for IF
The rest of this tutorial builds on four ideas from this chapter:
- A comparison returns
TRUEorFALSE. IF uses that result to choose between two outcomes. - Formulas can contain constants, references, and other functions. The outcomes of an IF can be text, numbers, or whole calculations.
- Relative references shift when you copy a formula, and absolute references do not. A threshold cell needs the dollar signs.
- Text, numbers, and blanks are different kinds of data. Mixing them up is the most common reason an IF formula gives a wrong answer.
Exercise
A formula in D2 is =B2*C2*F1. F1 holds a tax rate. You fill the formula down to D3. What formula appears in D3, and how should you change the original formula so the tax rate is read correctly in every row?
Show answer
D3 contains =B3*C3*F2, so the tax rate reference has drifted to F2, which is probably empty. Lock the rate cell in D2 with an absolute reference: =B2*C2*$F$1. After filling down, D3 reads =B3*C3*$F$1.
Exercise
Cell A1 contains the number 8. What does each formula return: =A1>=10, =A1<>8, and =A1*2>=10?
Show answer
=A1>=10 returns FALSE because 8 is below 10. =A1<>8 returns FALSE because A1 does equal 8, so "not equal" is false. =A1*2>=10 returns TRUE, because the multiplication runs first (8 * 2 = 16) and 16 is at least 10.
Exercise
A column of scores was imported from another system. The values are left-aligned and show green triangles. The formula =B2=60 returns FALSE for a cell that appears to show 60. What is the likely cause, and what should you check first?
Show answer
The cell most likely holds the text "60" instead of the number 60. A text value never equals a number in a comparison. Check the alignment and the green triangle, then convert the values to numbers. The warning icon next to the cell offers "Convert to Number".