Sub topic 3
Writing Your First IF Formula
What this chapter covers and what it assumes
This chapter introduces the structure of the IF function. You will write IF formulas that return one result when a test is true and another when it is false. You will also learn how Excel evaluates them and which errors to expect.
It 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.
- Write a logical test with a comparison operator (=, <>, >, <, >=, <=). The previous chapter covered these, and the chapter recaps them briefly below.
Later chapters cover text comparison, threshold cells, AND/OR/NOT, nesting, IFS, blanks, and error guarding in depth. This chapter touches on some of them only as previews.
The working model: a question with two answers
IF asks a yes-or-no question about your data. Excel then returns one of two results that you supplied in advance. The syntax is:
=IF(logical_test, value_if_true, value_if_false)The function has three arguments, separated by commas:
| Argument | What it holds | Example |
|---|---|---|
| `logical_test` | Any expression that evaluates to TRUE or FALSE | `B2>=60` |
| `value_if_true` | The result when the test is TRUE | `"Pass"` |
| `value_if_false` | The result when the test is FALSE | `"Fail"` |
Excel evaluates the test first. If the test is TRUE, Excel returns the second argument and ignores the third. If the test is FALSE, it returns the third argument and ignores the second. Only one branch is ever used for a given cell.
A quick recap of logical tests
A logical test compares two things and produces TRUE or FALSE. The comparison operators are:
| Operator | Meaning | Example | Result if B2 is 75 |
|---|---|---|---|
| `=` | equal to | `B2=75` | TRUE |
| `<>` | not equal to | `B2<>75` | FALSE |
| `>` | greater than | `B2>60` | TRUE |
| `<` | less than | `B2<60` | FALSE |
| `>=` | greater than or equal to | `B2>=75` | TRUE |
| `<=` | less than or equal to | `B2<=70` | FALSE |
You can type =B2>=60 in any empty cell and press Enter. Excel shows TRUE or FALSE. That is exactly the value IF uses to choose a branch. When a formula misbehaves, testing the logical test alone in a spare cell is a quick way to find out whether the test or the branches are at fault.
Your first IF formula
Suppose column B holds exam scores and 60 is the passing mark. In C2, enter:
=IF(B2>=60,"Pass","Fail")This formula returns "Pass" when the score in B2 is 60 or higher, and "Fail" otherwise.
Work through it in order:
- Excel reads the score in B2.
- It evaluates
B2>=60. For a score of 72 the result is TRUE. For a score of 45 it is FALSE. - For TRUE, it displays the second argument,
"Pass". For FALSE, it displays the third argument,"Fail".
Here is how the formula behaves when you copy it down column C:
| Row | B (score) | Test `B2>=60` | C (result) |
|---|---|---|---|
| 2 | 72 | TRUE | Pass |
| 3 | 45 | FALSE | Fail |
| 4 | 60 | TRUE | Pass |
| 5 | 59 | FALSE | Fail |
Row 4 matters. The test uses >=, so a score of exactly 60 passes. If you had written B2>60, row 4 would return "Fail". Boundary values are the most common place for IF logic to go wrong, so check them whenever you write a test.
When you drag the fill handle down, the reference B2 adjusts to B3, B4, and so on. This is a relative reference. It is what you want here, because each row should test its own score.
Why text results need quotation marks
In the formula above, "Pass" and "Fail" are inside double quotation marks. The quotes tell Excel that the content is literal text. Without them, Excel treats the word as a name, tries to find a named range or function with that name, and returns #NAME?.
Numbers and cell references do not take quotes. Compare:
=IF(B2>=60,"Pass","Fail")
=IF(B2>=60,1,0)
=IF(B2>=60,B2,"")The first returns text. The second returns numbers. The third returns the score itself when it passes and an empty string otherwise. The empty string "" is two quotation marks with nothing between them. It displays as a blank cell.
A number in quotes becomes text. "1" is a text character, not the number 1, so a later formula such as SUM will ignore it. Quote only what you mean as text.
Returning calculations
The result branches do not have to be fixed values. Either branch can be a calculation:
=IF(B2>=1000,B2*0.1,0)This returns a 10% bonus when the sales in B2 reach 1000, and 0 otherwise. Excel calculates B2*0.1 only when the test is TRUE. The next chapter explores return values in more detail. For now, remember that anything Excel can evaluate to a single value can go in either branch.
Threshold cells: a preview
The formulas so far have the number 60 or 1000 typed into the formula itself. That is called hard-coding. It works, but when the passing mark changes you must edit every formula. A better habit is to store the threshold in a cell, for example F1, and refer to it:
=IF(B2>=$F$1,"Target met","Below target")The dollar signs make $F$1 an absolute reference. It stays fixed when you copy the formula down, while B2 still changes row by row. A later chapter covers absolute references fully. If you see the wrong results after copying a formula down, forgetting the dollar signs is a likely cause.
Comparing text: a preview
IF can also test text. The comparison must put the text in quotes:
=IF(A2="Yes","Approved","Pending")The comparison with = ignores upper and lower case, so "yes", "YES", and "Yes" all match. However, a cell containing "Yes " with a trailing space does not match, because the strings are different. Stray spaces from pasted data are a frequent cause of IF formulas that appear to be correct but return the wrong branch.
What happens when you leave out an argument
IF tolerates missing arguments in some ways that surprise people. The behavior follows from how Excel reads the commas.
| Formula | Result when the test is FALSE | Why |
|---|---|---|
| `=IF(B2>=60,"Pass")` | `FALSE` | No third argument, so Excel returns the logical value FALSE |
| `=IF(B2>=60,"Pass",)` | `0` | The third argument is present but empty, which counts as 0 |
| `=IF(B2>=60,"Pass","")` | empty-looking cell | The third argument is an empty string |
The first form is the most common accident. A column fills with the word FALSE next to the passing rows, which looks like an error but is actually the correct result of an incomplete formula. If you want a blank, write "" explicitly.
The result type matters for later formulas
IF returns whatever type you give it. Mixing types in one formula can cause problems later:
=IF(B2>=60,"Pass",0)This formula returns text for some rows and a number for others. A formula such as COUNTIF or SUM that reads the column later will treat the two kinds of result differently. Keep the type consistent where you can. Use text for both branches, or numbers for both, unless you have a reason to do otherwise.
The empty string needs special care. "" looks blank, but it is text. A calculation such as =C2+1 on a cell that holds "" returns #VALUE!. If a later formula needs numbers, return 0 instead of "", or handle the blank case in that later formula.
Step by step: build and check an IF formula
A reliable routine prevents most errors:
- Say the rule in plain words: "If the score is at least 60, show Pass. Otherwise show Fail."
- Write the test alone in a spare cell, such as
=B2>=60, and confirm it returns TRUE and FALSE where you expect. - Wrap the test in IF and add the two results.
- Check the closing parenthesis and every pair of quotes.
- Copy the formula down and check the boundary rows, for example scores of 59, 60, and 61.
- Use Formulas > Evaluate Formula to step through the calculation if a result surprises you.
Excel also helps while you type. When you enter =IF(, a tooltip shows the argument names and highlights the one you are currently typing. Selecting a function name in the formula bar and pressing Ctrl+A (Windows) opens the Function Arguments dialog, which shows each argument in its own box and previews the result.
Common first-formula errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `#NAME?` | Text result or text comparison without quotes | Put the text in double quotes |
| Result shows `FALSE` instead of a word | Third argument omitted | Add the third argument, or `""` for blank |
| Excel reports a problem with the formula when you press Enter | Unbalanced parentheses or a missing comma | Count the opening and closing parentheses and check the separators |
| Boundary values land in the wrong branch | Used `>` where `>=` was needed, or the reverse | Recheck the rule at the exact boundary |
| Formula appears as text in the cell | The cell was formatted as Text before you typed the formula, or a leading apostrophe is present | Change the format to General, then re-enter the formula |
| Separator error on some computers | Regional settings use semicolons between arguments | Use the separator your Excel version expects, often `;` |
The last row is worth knowing. In some regional settings the list separator is a semicolon, so the formula reads =IF(B2>=60;"Pass";"Fail"). The structure is the same.
Exercises
Exercise
Column B holds sales figures. Write a formula for C2 that returns "Met" when B2 is at least 5000 and "Not met" otherwise. What does it return for a value of exactly 5000?
Show answer
=IF(B2>=5000,"Met","Not met"). For exactly 5000 it returns "Met", because the operator >= includes the boundary value.
Exercise
A learner enters =IF(B2>=60,"Pass") and sees FALSE in the rows that failed. Explain why this happens and fix the formula so those rows show "Fail".
Show answer
The third argument (valueiffalse) is missing, so Excel returns the logical value FALSE when the test is false. The fix is =IF(B2>=60,"Pass","Fail").
Exercise
A formula =IF(B2>=60,Pass,Fail) returns #NAME?. What is wrong and how do you correct it?
Show answer
The words Pass and Fail are not in quotation marks, so Excel treats them as names it cannot find. Correct it with =IF(B2>=60,"Pass","Fail").
Exercise
Write a formula that returns 10% of B2 as a bonus when B2 is at least 1000, and 0 otherwise. Why might 0 be a better choice than "" if the column will be summed or added to later?
Show answer
=IF(B2>=1000,B2*0.1,0). The value 0 is a number, so later calculations treat it as a value. The empty string "" is text, and adding it in a formula such as =C2+1 returns #VALUE!.