How To Use The IF Function In Excel With Examples (2026)

The IF function in Excel checks a condition and returns one result if it is true and another if it is false.

The pattern is =IF(test, result if true, result if false), for example =IF(A2>B2,”Over Budget”,”OK”).

This guide shows the syntax, working examples from Microsoft, how to combine IF with AND and OR, and what to do when a formula returns 0 or #NAME?.

Table of Contents

Quick answer: the IF formula

PartRequired?What it does
logical_testYesThe condition you want to check
value_if_trueYesWhat to return when the condition is TRUE
value_if_falseNoWhat to return when the condition is FALSE

The full syntax is IF(logical_test, value_if_true, [value_if_false]), as shown on Microsoft’s IF function page.

How to write an IF formula

  1. Click the cell where you want the answer.
  2. Type =IF( to start the formula.
  3. Type the condition, such as A2>B2, followed by a comma.
  4. Type the result for a true condition, followed by a comma.
  5. Type the result for a false condition and close the bracket.
  6. Press Enter.

Text inside a formula must sit in quotation marks, though TRUE and FALSE are recognised without them.

IF can test both text and numbers, and it can run a calculation instead of returning a fixed word.

IF examples you can copy

FormulaWhat it returns
=IF(A2>B2,"Over Budget","OK")“Over Budget” when A2 is bigger than B2, otherwise “OK”
=IF(C2="Yes",1,2)1 when C2 is the word Yes, otherwise 2
=IF(C2=1,"Yes","No")“Yes” when C2 is 1, otherwise “No”
=IF(C2>B2,C2-B2,0)The overspend when actual exceeds budget, otherwise 0
=IF(E7="Yes",F5*0.0825,0)8.25% of F5 (a sales tax) when E7 is Yes, otherwise 0
=IF(A2=B2,B4-A4,"")The result of B4-A4 when A2 equals B2, otherwise a blank

All six formulas are from Microsoft’s IF page, which also notes that IF can check for errors.

Combine IF with AND, OR and NOT

Microsoft’s Create conditional formulas article uses sample cells where A2 is 15, A3 is 9, A4 is 8, A5 is Sprockets and A6 is Widgets.

FormulaPlain meaningResult
=IF(AND(A2>A3,A2<A4),"OK","Not OK")A2 must be above A3 and below A4Not OK
=IF(OR(A2>A3,A2<A4),"OK","Not OK")A2 must beat A3 or be under A4OK
=IF(NOT(A2<=15),"OK","Not OK")A2 must not be 15 or lessNot OK
=IF(A2<>15,"OK","Not OK")A2 must not equal 15Not OK

Use AND when every condition has to pass, OR when any one is enough, and NOT to flip a test.

Nested IF: several outcomes in one formula

You can place one IF inside another to handle more than two outcomes.

Microsoft’s article on nested IF formulas gives this letter-grade example.

=IF(D2>89,"A",IF(D2>79,"B",IF(D2>69,"C",IF(D2>59,"D","F"))))

Excel allows up to 64 nested IFs, but Microsoft advises against going beyond about three because long ones are hard to build and maintain.

Order matters in a nested IF

Excel stops at the first test that is true, so the order of the tests changes the answer.

Microsoft shows a commission formula written from the largest tier down, which works.

=IF(C9>15000,20%,IF(C9>12500,17.5%,IF(C9>10000,15%,IF(C9>7500,12.5%,IF(C9>5000,10%,0)))))

The same tiers written smallest first give wrong answers, because a value above 5,000 is caught by the first test, so 12,500 would return 10% instead of 17.5%.

Alternatives to long nested IFs

OptionExampleWhy use it
VLOOKUP with a table=VLOOKUP(C2,C5:D17,2,TRUE)The thresholds sit in a visible, sorted table, so you edit the table, not the formula
IFS=IFS(D2>89,"A",D2>79,"B",D2>69,"C",D2>59,"D",TRUE,"F")Removes most of the nesting; needs a Microsoft 365 subscription

Microsoft says the lookup table can sit on a separate worksheet to keep other users from changing it.

For a walkthrough of lookups, see our guide to XLOOKUP and VLOOKUP in Excel.

Make long formulas easier to read

  • Press Alt+Enter in the formula bar to insert a line break inside the formula.
  • Move the cursor through the formula, and Excel colours matching brackets so you can check the nesting.

Why your IF returns 0 or #NAME?

SymptomCause and fix
0 in the cellNeither result argument has a value, so add text to both, or TRUE or FALSE
#NAME?Usually the formula is misspelled

For other error values, see fixing #N/A, #VALUE! and #REF! errors.

Try the examples safely

  1. Copy the sample formulas and data, without row or column headers.
  2. Open a blank worksheet and paste into cell A1, because Microsoft says the example must start there.
  3. Press Ctrl+` (grave accent) to switch between results and formulas, or use Show Formulas on the Formulas tab.

If the formulas show as text instead of results, read why Excel formulas are not calculating.

To count or add only cells that meet a rule, SUMIF and COUNTIF are the simpler tools.

Leave a Reply

Your email address will not be published. Required fields are marked *