How to Write an IF Formula and Nested IFs Without the Headaches

An IF formula asks one question and returns one answer if the question is true and another if it is false. A nested IF places a second IF inside one of those answers, so a sheet can sort a value into three, four or more groups. Most of the trouble with nested IFs comes from the order of the tests and the bracket count, and both are easy to check once you know what to look for.

The short rule: write the simplest IF that works, test it on boundary values, and move to IFS or a lookup table once you reach a third branch that someone else will need to maintain.

Terms used in this guide

Before writing a formula, agree on the vocabulary. The terms below appear throughout this guide and in most spreadsheet help pages.

  • Logical test: the condition that returns TRUE or FALSE, such as B2>=5000.
  • Value if true: the result returned when the logical test is TRUE.
  • Value if false: the result returned when the logical test is FALSE. It is optional in IF, but leaving it out returns FALSE, which often surprises people.
  • Nesting: placing one function inside another. A nested IF is an IF inside the value-if-false (or value-if-true) argument of another IF.
  • Boundary value: an input that sits exactly on a threshold, such as 5,000 when the rule says “5,000 and above”.
  • Lookup table: a small range of thresholds and results, kept in its own cells, that a formula reads instead of hard-coding the numbers.
Bellmark Teacher Planner and Gradebook

Skip the setup

Bellmark Teacher Planner and Gradebook

Weighted gradebook, attendance and lesson plans, with grades that calculate as you enter marks.

See it on Taskdale →

Writing a single IF

SOP 2.1: The three arguments

Every IF takes three arguments separated by commas:

=IF(logical_test, value_if_true, value_if_false)

A worked case: a pass mark of 40 in cell B2.

=IF(B2>=40, "Pass", "Fail")

If B2 holds 52, the cell shows Pass. If B2 holds 38, the cell shows Fail. The formula checks the test first, then returns whichever result matches.

Some European locales use semicolons instead of commas as the separator. If a formula pastes in with errors about unexpected characters, check the separator setting in your spreadsheet’s regional options before you change the formula itself.

SOP 2.2: Text, numbers and references

Four rules cover most single-IF problems:

  1. Text results go in double quotes. "Pass" is text. Pass without quotes is treated as a name and returns an error.
  2. Numbers and cell references do not go in quotes. B2*0.1 is correct. "B2*0.1" returns the text string, not the calculation.
  3. Logical test operators are =, <> (not equal to), >, <, >= and <=. Use <> for “not equal”, not !=, which belongs to programming languages.
  4. Anchor references you will copy. If a formula refers to a fixed cell such as a threshold in $D$2, the dollar signs stop the reference shifting when the formula is filled down.

Building a nested IF

SOP 3.1: How the nest is read

A nested IF is read from the outside in. The first logical test runs. If it is TRUE, its value is returned and the rest of the formula is ignored. If it is FALSE, the formula moves into the value-if-false argument and runs the next test. The first TRUE result wins.

Here is a three-band commission rule. Sales of 10,000 or more earn 6%. Sales from 5,000 up to 9,999 earn 4%. Anything below 5,000 earns 2%. Sales sit in B2.

=IF(B2>=10000, B2*6%, IF(B2>=5000, B2*4%, B2*2%))

Read the outer IF first. If B2 is 10,000 or more, the first branch applies and the second IF is never evaluated. Only values that fail the first test reach the inner IF.

SOP 3.2: Order the tests from the top threshold down

The most common nested IF error is testing the thresholds in the wrong order. Compare these two versions, both using >=:

Wrong:  =IF(B2>=5000, B2*4%, IF(B2>=10000, B2*6%, B2*2%))
Right:  =IF(B2>=10000, B2*6%, IF(B2>=5000, B2*4%, B2*2%))

In the wrong version, a sale of 12,000 passes the first test (it is 5,000 or more) and receives 4% rather than 6%. The formula returns a result, so nothing flags the problem. Someone has to check the output against the rule.

With >= tests, order from the highest threshold down. With tests written as < (for example, B2<5000), order from the lowest threshold up. Either way, write the order down before typing.

SOP 3.3: Count the brackets before you press Enter

Every IF opens with a bracket and must close with one. A three-level nest needs three closing brackets at the end. Forgetting one returns an error, but adding one in the wrong place can produce a formula that is valid and wrong.

Two habits prevent most bracket errors. First, write the nest on paper or in a text editor, with each IF on its own line, then paste it in as one line. Second, when editing, click next to a bracket in the formula bar. Excel and Google Sheets highlight the matching bracket, so you can see where each IF ends.

Choosing the right tool

A nested IF is not always the right answer. The table below compares the four approaches most people use for multi-branch logic.

ApproachAvailable inBest forMain weakness
Nested IFEvery version of Excel and Google SheetsTwo or three branchesHard to read and edit beyond three levels
IFSExcel 2019 and Microsoft 365, Google SheetsOrdered conditions without repeated IF functionsReturns an error if no condition is TRUE, unless the last test is TRUE
SWITCHExcel 2019 and Microsoft 365, Google SheetsExact matches against one value, such as product codesDoes not handle ranges of numbers
Lookup tableAny version with VLOOKUP, XLOOKUP in newer versionsTiers that may change, such as rates or gradesNeeds a correctly ordered table and a one-off setup

SOP 4.1: Use IFS for ordered conditions

The commission example becomes:

=IFS(B2>=10000, B2*6%, B2>=5000, B2*4%, TRUE, B2*2%)

The final TRUE acts as the default. Without it, a value that matches no test (for example, a negative number if the rule has no lower band) returns an error. The IFS version is easier to read because each condition sits beside its result, but it needs a version of the software that supports it. Anyone opening the file in an older version sees an error.

SOP 4.2: Use a lookup table when the rule will change

Put the thresholds and rates in their own cells, then let a lookup read them:

Sales from (£)Rate
02%
5,0004%
10,0006%

With the thresholds in D2:D4 and the rates in E2:E4, the formula is:

=B2*XLOOKUP(B2, $D$2:$D$4, $E$2:$E$4, 0, -1)

The -1 tells the lookup to find an exact match or, failing that, the next smaller value. The table must be sorted in ascending order. An unsorted table produces wrong results without any warning. In versions without XLOOKUP, use =B2*VLOOKUP(B2, $D$2:$E$4, 2, TRUE), where the TRUE requests approximate matching.

SOP 4.3: Understand the trade-offs

The cost of a nested IF is paid later. Building a three-band formula takes a few minutes. Changing the 5,000 threshold to 6,000 means editing the formula in every cell that holds it, and missing one cell is easy. A lookup table costs time to set up and needs its sort order protected, but a rate change is one cell edit.

For a sheet that will be used once, a nested IF is fine. For a model that other people will update, or a rule that changes each quarter, the lookup table is the more maintainable choice. A rough guide: two branches, use IF. Three branches with stable thresholds, use nested IF or IFS. Four or more branches, or any rule with changing numbers, use a lookup table.

QuotaBolt Commission Tracker

Ready-made template

QuotaBolt Commission Tracker

Log deals and let the sheet calculate commission, tiers and payouts per rep.

See it on Taskdale →

Common mistakes and how to fix them

These are the errors that appear most often in shared spreadsheets, with the fix for each.

  • Wrong threshold order. Test from the highest value down when using >=. Check with a boundary value at each threshold.
  • Missing or extra brackets. Count the opening and closing brackets. Use the bracket highlighting in the formula bar.
  • Numbers stored as text. A value imported from a system can look like 5000 but be stored as text. Comparisons against text behave unexpectedly. Convert with =VALUE(A2), or use Data then Text to Columns to convert the whole column.
  • Blank cells passing a test. In a comparison, a blank cell counts as zero. So =IF(B2<5000, ...) returns the “below 5,000” result for an empty cell. Guard against this with =IF(B2="", "", ...) as the outer test.
  • Decimal comparisons that miss. Percentages and currency calculations can produce values such as 4999.9999999 rather than 5000. Wrap the calculated value in ROUND, for example =IF(ROUND(B2,2)>=5000, ...).
  • Unanchored references. A threshold that shifts when the formula is copied down produces a different rule in each row. Use $D$2 rather than D2 for fixed cells.
  • IFERROR hiding the real problem. Wrapping the whole nest in IFERROR(..., 0) turns every error into a zero, including the errors you need to see. Apply IFERROR only to the specific calculation that may legitimately fail.
  • Typing TRUE or FALSE in quotes. "TRUE" is text and does not match the logical value TRUE. Type the word without quotes.

Worked example, sales commission from start to finish

This module builds the commission sheet from the start, so you can see each choice in context.

SOP 7.1: Set up the inputs

Put sales figures in column B, starting at B2. Put the rule table in D1:E4, with a header row in row 1 (Sales from, Rate) and the three bands in rows 2 to 4. Keep the rule table on the same sheet so the thresholds are visible to anyone reviewing the file.

SOP 7.2: Write the nested IF

In C2, enter:

=B2*IF(B2>=10000, 6%, IF(B2>=5000, 4%, 2%))

This version multiplies sales by a rate chosen by the nest. It is shorter than repeating the multiplication in each branch, and it keeps the calculation in one place.

SOP 7.3: Test the boundaries

Enter the test values in column B and check column C against the expected commission.

Sales (£)Expected rateExpected commission (£)
4,999.992%100.00
5,0004%200.00
9,9994%399.96
10,0006%600.00

The row for 5,000 is the most useful check. If that row shows 2%, the test is written as > instead of >=. The row for 4,999.99 confirms that the lower band is not reached early.

SOP 7.4: Compare the three versions

The same rule works three ways. The nested IF version above suits a small sheet. The IFS version swaps the nest for a flat list of conditions, which reads more clearly but needs a recent version of the software. The lookup version reads the rates from D2:E4.

Now the rule changes. The business decides the top band starts at 12,000 rather than 10,000. In the nested version, the formula must be edited in every row. In the lookup version, D4 changes from 10,000 to 12,000, and every row updates. Re-run the boundary tests after any change, because the new threshold is the one most likely to be typed incorrectly.

Decision checklist for choosing and checking an IF

Use this checklist before a formula goes into a shared file.

  1. Count the branches. Two branches: use IF. Three with stable thresholds: nested IF or IFS. Four or more, or changing rates: lookup table.
  2. Confirm every recipient’s software supports the function. IFS and SWITCH need Excel 2019 or Microsoft 365, or Google Sheets.
  3. If nesting, write the tests in order from the top threshold down (for >=) or the bottom up (for <).
  4. Count the brackets, and check that each IF closes.
  5. Test each threshold with the exact value, one unit below and one unit above.
  6. Test a blank cell, a zero and a negative value, and decide what each should return.
  7. Anchor every fixed reference with dollar signs before copying the formula.
  8. Check that numbers and dates are stored as numbers and dates, not text.
  9. Keep IFERROR away from the rule itself, and apply it only where an error is expected.
  10. Write the rule and the test results in a note on the sheet, so the next person can confirm the formula still matches the rule.
GradeMint Gradebook for Google Sheets

Get the finished file

GradeMint Gradebook for Google Sheets

A digital gradebook with weighted categories and averages that update on their own.

See it on Taskdale →

Scroll to Top