Conditional Logic

How to Use the IF Function in Excel

Learn how the IF function works in Excel, from its three arguments to comparisons, text results, copying formulas and the common mistakes that break them.

On this page
  1. The three parts of an IF formula
  2. Comparison operators
  3. Write your first IF formula
  4. Leave out the false result carefully
  5. Copy the formula down a column
  6. Combine IF with other tests and functions
  7. Hide errors only when you understand them
  8. Common reasons an IF formula returns the wrong result
  9. Keep the pattern in mind

The IF function checks a condition and returns one result when the condition is true and a different result when it is false. It is the starting point for most conditional formulas in Excel, from simple pass or fail labels to larger rules that decide what a sheet shows.

The basic form is:

=IF(logical_test, value_if_true, value_if_false)

For example, =IF(B2>=50, "Pass", "Fail") shows Pass when the number in B2 is 50 or more, and Fail when it is lower. The same syntax works in Excel for Windows, Excel for Mac and Excel for the web. In some regional settings, Excel uses semicolons instead of commas between arguments.

The three parts of an IF formula

  • logical_test is the condition Excel checks. It must evaluate to TRUE or FALSE, such as B2>=50 or C2="Paid".
  • value_if_true is what the cell shows when the test is TRUE.
  • value_if_false is optional. It is what the cell shows when the test is FALSE.

Text results go in double quotes, like "Pass". Numbers and cell references do not need quotes. A result can also be a calculation, such as B2*0.2, instead of a fixed value.

Comparison operators

These symbols make up most logical tests:

Operator Meaning Example
= equal to A2="Yes"
<> not equal to A2<>"Yes"
> greater than B2>100
< less than B2<100
>= greater than or equal to B2>=50
<= less than or equal to B2<=50

Comparisons between text are not case-sensitive. "yes", "Yes" and "YES" all match A2="Yes". Spaces do count, though, so "Yes " with a trailing space does not match.

Write your first IF formula

Suppose column B holds order totals, and you want column C to say “Free shipping” for orders of 100 or more and “Standard” for everything else. Click C2 and type:

=IF(B2>=100, "Free shipping", "Standard")

Press Enter. Excel checks B2 first. If the total is 100 or more, C2 shows “Free shipping”. If not, it shows “Standard”.

Check a few values at the edges, such as 99, 100 and a blank cell, before you copy the formula further.

Leave out the false result carefully

If you leave out the last argument, the formula still works, but a false test shows FALSE:

=IF(B2>=100, "Free shipping")

For orders under 100, that formula displays FALSE. If you would rather show nothing, use empty text as the last argument:

=IF(B2>=100, "Free shipping", "")

Copy the formula down a column

Once C2 works, you can fill it down. Double-click the small square at the bottom-right corner of the selected cell, or drag it down. Excel changes the row number in each copy, so C3 checks B3, C4 checks B4, and so on.

Sometimes a reference should stay fixed. Suppose the free shipping threshold sits in F1. Use $F$1 so every copy points to the same cell:

=IF(B2>=$F$1, "Free shipping", "Standard")

Now you can change the threshold in F1 and every row updates. The dollar signs are covered in more detail in how to lock cell reference in excel.

Combine IF with other tests and functions

Real sheets often need more than one test. A few common extensions:

  • Two or more conditions in one test. Wrap the tests in AND or OR. See how to use and or with if in excel for examples.
  • More than two results. Nest IF functions, or use IFS or SWITCH in newer versions. For nested IF, see how to write nested if formulas in excel. For matching one value to several options, see excel switch function example.
  • Text checks. SEARCH combined with ISNUMBER tests whether a cell contains a word. That is covered in excel if cell contains specific text.
  • Blank checks. A test like A2="" is covered in if cell is blank formula in excel.
  • Adding up instead of labeling. If you want a total for rows that match a rule, SUMIF is often the better tool. See how to use sumif in excel.

Each of these builds on the same three parts you have just learned.

Hide errors only when you understand them

If the value a formula checks is an error, or text where a number was expected, the result can be an error too. Wrapping the formula in IFERROR can show a friendlier result, but it can also hide a real problem. Fixing the cause is usually the better first step. How to hide errors in excel formulas explains when wrapping makes sense.

Common reasons an IF formula returns the wrong result

Most problems come from a few sources:

  • Text not in quotes. =IF(B2=Paid, ...) makes Excel look for a name called Paid, which produces a #NAME? error. Use "Paid".
  • Numbers stored as text. A number that Excel reads as text can fail a comparison.
  • Extra spaces. Text with a stray space does not match an exact test.
  • References that shifted. A reference that changed when the formula was copied, when it should have stayed fixed.
  • An unexpected false result. The false branch may not be what you assumed it would be.

If a formula still doesn’t behave, the step-by-step checks in why is my if formula not working in excel are a good next read.

Keep the pattern in mind

Once the three parts are clear, most conditional formulas in Excel are variations on the same pattern. Write the test first, then the result when true, then the result when false. Check the first row with a few values, and copy the formula down only after it works there.

Keep exploring

Guides from other topics

All topics →