formulastreak

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

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.

March household budget · edit E2 and watch it recalculate
Click a cell (or use the arrow keys) to insert its address · Enter shows the resultE2 = $0.00
ABCDE
1CategoryTypeBudgetActualOverspend
2RentFixed$1,200.00$1,200.00$0.00
3GroceriesVariable$450.00$512.40$62.40
4UtilitiesFixed$160.00$148.75$0.00
5TransportVariable$120.00$96.50$0.00
6Phone & internetFixed$65.00$65.00$0.00
7Eating outVariable$150.00$203.10$53.10
8SubscriptionsFixed$38.00$44.97$6.97
9GymFixed$40.00$40.00$0.00
10GiftsVariable$60.00$25.00$0.00
11SavingsFixed$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. 1. IF: Checks whether D2 is greater than C2. If it is, the result is D2 minus C2; if not, it's 0.
    • condition = `D2>C2`: a comparison that's TRUE or FALSE
    • value_if_true = `D2-C2`: what to show when it's TRUE
    • value_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

ArgumentRequiredWhat it is
logical_testYesA comparison that is TRUE or FALSE, such as D2>C2.
value_if_trueYesWhat to return when the test is TRUE: a number, text in quotes, a cell or a calculation.
value_if_falseNoWhat 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: use AND(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

  • #NAME?: A text result without quotes is read as an unknown name.
  • #VALUE!: The test or a result does maths on text.

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.

Practise IF on a live sheet

Lesson 10, TRUE, FALSE and IF (Unit 3, Decisions), has 8 graded exercises, checked instantly, with steps and hints when you're stuck. Pro