Logic and errors function
IF function in Google Sheets and Excel
IF checks a condition and returns one value when it's true and another when it's false.
Checked against Microsoft's and Google's documentation on · Example calculated on a live sheet
- Excel 2016
- Excel 2019
- Excel 2021
- Excel 2024
- Microsoft 365
- Google Sheets
Syntax
IF(logical_test, value_if_true, [value_if_false])Arguments in [brackets] are optional. Google Sheets writes it IF(logical_expression, value_if_true, value_if_false).
Example
=IF(D2>C2,D2-C2,0) reads: If the actual spend D2 is more than the budget C2, show the overspend D2−C2; otherwise show 0.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Category | Type | Budget | Actual | Overspend |
| 2 | Rent | Fixed | $1,200.00 | $1,200.00 | $0.00 |
| 3 | Groceries | Variable | $450.00 | $512.40 | $62.40 |
| 4 | Utilities | Fixed | $160.00 | $148.75 | $0.00 |
| 5 | Transport | Variable | $120.00 | $96.50 | $0.00 |
| 6 | Phone & internet | Fixed | $65.00 | $65.00 | $0.00 |
| 7 | Eating out | Variable | $150.00 | $203.10 | $53.10 |
| 8 | Subscriptions | Fixed | $38.00 | $44.97 | $6.97 |
| 9 | Gym | Fixed | $40.00 | $40.00 | $0.00 |
| 10 | Gifts | Variable | $60.00 | $25.00 | $0.00 |
| 11 | Savings | Fixed | $300.00 | $300.00 | $0.00 |
E3 shows $62.40 for Groceries (512.40 − 450). Categories within budget, like Rent in E2, show $0.00.
How the spreadsheet calculates it
Checks whether D2 is greater than C2. If it is, the result is D2 minus C2; if not, it's 0.
- 1. IF: Checks whether
D2is greater thanC2. If it is, the result isD2minusC2; if not, it's0.condition=`D2>C2`: a comparison that's TRUE or FALSEvalue_if_true=`D2-C2`: what to show when it's TRUEvalue_if_false=`0`: what to show when it's FALSE. Leave it out and a failed test shows FALSE
More IF examples
=IF(C2>=50,"Pass","Fail")Pass for 50 or more, otherwise Fail.=IF(D2>C2,D2-C2,0)The overspend, or 0 when within budget.=IF(AND(B2="Variable",D2>C2),"Cut back","")A flag only when both conditions are true.=IF(B2="","",C2/B2)Leave the cell empty when there's nothing to divide by.
Paste any of these into the formula explainer to see it step by step.
Tips
- Test the highest bar first when you nest IFs, or the lower bands catch everything.
- Keep thresholds in cells (
IF(C2>=$K$1, …)) so changing the rule doesn't mean editing every formula. - Return "" rather than 0 when “nothing to show” isn't the same as zero.
- When IFs nest more than three deep, switch to IFS or a lookup table: both are easier to read and change.
What IF is used for
Flagging overspends, late invoices or missing data, turning scores into pass and fail, and choosing between two calculations (a discount above a threshold, a fee below it).
Arguments
| Argument | Required | What it is |
|---|---|---|
| logical_test | Yes | A comparison that is TRUE or FALSE, such as D2>C2. |
| value_if_true | Yes | What to return when the test is TRUE: a number, text in quotes, a cell or a calculation. |
| value_if_false | No | What to return when it's FALSE. Left out, a false test shows FALSE.Watch out: Use "" if you want the cell to look empty instead. |
Common mistakes
Swapping the results: the value for TRUE always comes first.
Leaving out value_if_false when you wanted a number: the cell shows FALSE instead.
Text results without quotes, like Yes instead of "Yes", which give #NAME?.
Writing
A1>5 AND A1<10: useAND(A1>5, A1<10)as the test.
In Google Sheets
Google Sheets' IF has the same three arguments, and the same behaviour when the third is left out.
Errors you may see with IF
Questions
How do I use IF with several conditions?
Put AND or OR inside the test, or nest IFs for more than two results. See IF with AND and OR and nested IF vs IFS vs SWITCH.
How do I check if a cell is blank?
IF(A2="", "Missing", A2) treats empty text from a formula as blank too; ISBLANK(A2) is TRUE only for a truly empty cell.
Can IF return a formula?
Yes: either result can be any calculation, such as IF(F2>=100, F2*0.9, F2) for a 10% discount on orders of 100 or more.
Sources
Official documentation the facts on this page were checked against.