Sub topic 3
Writing your first IF formula
This chapter assumes you can enter values and formulas in cells, use references such as B2, and copy a formula down a column with the fill handle. It also assumes you know the comparison operators from the previous chapter (=, <>, >, <, >=, <=).
What IF does
The IF function checks a condition and returns one result when the condition is true and another when it is false. It is the basic building block for decisions in a worksheet, such as labeling a score as a pass or a fail.
The three arguments
IF takes three arguments, separated by commas:
=IF(logical_test, value_if_true, value_if_false)| Argument | What it holds |
|---|---|
| `logical_test` | A comparison that is either TRUE or FALSE, such as `B2>=60` |
| `value_if_true` | The result returned when the test is TRUE |
| `value_if_false` | The result returned when the test is FALSE |
Excel evaluates the test first. It then returns only one of the other two arguments and ignores the other.
A worked example
Suppose column A holds student names and column B holds scores. A score of 60 or higher passes. In cell C2, enter:
=IF(B2>=60,"Pass","Fail")Follow these steps:
- Click cell C2 and type the formula, starting with the equals sign.
- Press Enter. If B2 contains 72, C2 shows Pass. If B2 contains 45, C2 shows Fail.
- Drag the fill handle down the column. Each row's formula points to its own score, because the reference
B2is relative and shifts toB3,B4, and so on.
Read the formula aloud as a sentence: "If B2 is greater than or equal to 60, show Pass, otherwise show Fail." If you can say it that way, the formula is probably built correctly.
Rules for the syntax
- Start with
=. Without it, Excel treats the entry as plain text. - Put text results in double quotes.
"Pass"is text. Pass without quotes is read as a name, and Excel returns a#NAME?error. - Do not quote numbers or cell references.
B2refers to a cell."B2"is just the letters B and 2. - Match your parentheses. Every opening parenthesis needs a closing one. Excel colors matching pairs as you type, which helps you spot a missing one.
- Use commas as separators. Some regional settings use semicolons instead. If a formula from this chapter is rejected, check your list separator.
Checking the test on its own
When a result looks wrong, test the condition alone in a spare cell. Typing =B2>=60 returns TRUE or FALSE directly. If that cell shows the wrong answer, the problem is in the comparison and not in the rest of the IF.
Common first-formula errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `#NAME?` | Text result without quotes | Wrap the text in double quotes |
| The formula appears as text in the cell | Missing `=`, or the cell is formatted as Text | Add `=` and set the format to General |
| Every row shows the same answer | A reference was typed as `"B2"` or fixed by mistake | Use a plain relative reference such as `B2` |
| Excel reports a problem with the formula | Unbalanced parentheses or wrong separator | Count the parentheses and check the list separator |
Exercise
Column B holds sales figures. Write a formula for C2 that shows "Yes" when B2 is greater than 500 and "No" otherwise. Then say what the formula returns when B2 contains exactly 500.
Show answer
=IF(B2>500,"Yes","No"). When B2 is exactly 500, the test B2>500 is FALSE because 500 is not greater than 500, so the formula returns "No". To include 500, use >= instead.
Exercise
A colleague wrote =IF(B2>=60,Pass,"Fail") and gets #NAME? in the cell. What is wrong, and what is the corrected formula?
Show answer
The word Pass is not in quotes, so Excel treats it as an unknown name. The corrected formula is =IF(B2>=60,"Pass","Fail").