The IF function
IF lets a formula make a decision. It checks a test, such as "is the amount spent more than the budget?", and returns one result if the answer is yes and another if it's no.
The results can be words, numbers or calculations, so IF can label a row as well as work out a figure only when it applies.
=IF(logical_test, value_if_true, [value_if_false])
Arguments
- logical_test
- A comparison that is either TRUE or FALSE, such as
C2>B2. - value_if_true
- What to show when the test is TRUE: text in quotes, a number, a cell or another formula.
- value_if_falseoptional
- What to show when the test is FALSE. Leave it out and IF shows FALSE.
Try it
=IF(C2>B2,"Over","OK") in D2. Groceries cost £324.60 against a £300 budget, so the test C2>B2 is TRUE and IF shows Over.
D2
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Item | Budget | Spent | Status | |
| 2 | Groceries | £300.00 | £324.60 | Over | |
| 3 | Fuel | £120.00 | £96.00 | ||
| 4 | Phone | £25.00 | £25.00 | ||
| 5 | Eating out | £80.00 | £112.40 | ||
| 6 | |||||
| 7 |
Edit the formula in D2, or change the data, and press Enter.
More examples
| Formula | Result | What it does |
|---|---|---|
| =IF(C3>B3,"Over","OK") | OK | Fuel came in under budget, so the test is FALSE and the second answer is used. |
| =IF(C4>B4,"Over","OK") | OK | Spending exactly the budget isn't more than it. Use >= if hitting the limit should count. |
| =IF(C5>B5,C5-B5,0) | 32.4 | The results can be calculations: how much eating out went over by. |
| =IF(C3<B3,"Spare £"&B3-C3,"None") | Spare £24 | Join text and a sum with & to build a message. |
Tips
- Put text results in double quotes,
"Over", but not numbers or cell references. - For two conditions at once, put AND or OR inside the test:
=IF(AND(C2>B2,B2>100),"Check",""). - If you find yourself nesting several IFs, IFS or SWITCH is usually easier to read.
- Use
""as a result to leave the cell looking blank.
Common mistakes
- #NAME? appears.Text results are missing their quote marks:
"Over", not Over. - The result is always the same, whatever the data.Check the comparison direction (
>vs<) and that the cells being compared hold numbers, not numbers stored as text. - A cell shows FALSE instead of a blank.You left out value_if_false. Add
""as the third argument.
Practise IF with instant feedback
Type real formulas into a working spreadsheet and get told straight away whether they work.