- Purpose
- Test a condition and return one value when it is true and another when it is false.
- Returns
- The second argument if the test is TRUE, the third argument if it is FALSE.
- Syntax
- Works in
- Every version of Excel · Microsoft 365 · Google Sheets · LibreOffice Calc
Arguments
| Argument | Required | What it is |
|---|---|---|
| logical_test | Yes | A comparison or any expression that comes out as TRUE or FALSE, such as B2>=50. |
| value_if_true | Yes | What to return when the test is TRUE. Text goes in double quotes. It can also be a number, a cell or another formula. |
| value_if_false | No | What to return when the test is FALSE. Left out, the formula shows FALSE. |
Example: mark a score as pass or fail
The score in B2 is compared with 50. It is 72, so the test is TRUE and the formula returns the second argument, Pass. The same formula is copied down column C.
| A | B | C | |
|---|---|---|---|
| 1 | Student | Score | Result |
| 2 | Noor | 72 | Pass |
| 3 | Eli | 48 | Fail |
| 4 | Sofia | 50 | Pass |
| 5 | Marcus | 35 | Fail |
| 6 | Yuki | 91 | Pass |
Build your IF formula
Fill in the fields with your own cells. The formula updates as you type.
Checks whether B2 >= 50. Shows "Pass" when that is true and "Fail" when it is not.
Paste the formula into cell C2 or any empty cell. The coloured parts match the coloured fields. Using a different list separator? Replace the commas with semicolons.
How IF works
IF asks one question and gives one of two answers. The first argument is the question, written as a test that is either true or false. If it is true, Excel returns the second argument. If it is false, Excel returns the third.
The test is usually a comparison between a cell and a value.
| Operator | Meaning | Example test |
|---|---|---|
= |
Equal to | A2="Paid" |
<> |
Not equal to | A2<>"Paid" |
> |
Greater than | B2>100 |
>= |
Greater than or equal to | B2>=50 |
< |
Less than | B2<0 |
<= |
Less than or equal to | B2<=10 |
The two results can be text in double quotes, numbers, cell references or other formulas. =IF(B2>100, B2*0.1, 0) returns ten percent of B2 when B2 is over 100, and 0 otherwise.
Text comparisons ignore case. Numbers are written without quotes: B2>=50 compares with the number 50, and B2>="50" compares with a piece of text.
Nested IF and IFS
One IF gives two outcomes. For three or more, put a second IF in place of the false result. This is a nested IF.
=IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", "F")))
Excel checks the tests from left to right and stops at the first true one. For a score of 72 the first two tests fail, the third passes, and the result is C. Order matters. Start with the highest band when you test with >=.
IFS does the same without nesting. It takes pairs of test and result:
=IFS(B2>=90, "A", B2>=80, "B", B2>=70, "C", TRUE, "F")
The final pair, TRUE, "F", is the catch-all. Without it, IFS returns #N/A when no test is true.
| Nested IF | IFS | |
|---|---|---|
| Works in | Every version of Excel | Excel 2019 and later, Microsoft 365 |
| Structure | One IF inside another, with a closing bracket for each | A flat list of test and result pairs |
| When nothing is true | Returns the last value_if_false | Returns #N/A unless you add a TRUE pair |
| Limit | 64 levels | 127 pairs |
More examples
Two conditions that must both be true
=IF(AND(B2>=50, C2="Yes"), "Pass", "Fail")
returns Pass only when the score in B2 is at least 50 and C2 says Yes.
Either of two conditions
=IF(OR(A2="Sat", A2="Sun"), "Weekend", "Weekday")
returns Weekend when A2 is Sat or Sun, and Weekday for anything else.
Leave the result empty when the input is empty
=IF(B2="", "", B2*1.2)
returns empty text while B2 is blank, and B2 multiplied by 1.2 once a number is entered.
Test for part of a text value
=IF(ISNUMBER(SEARCH("east", A2)), "East region", "Other")
returns East region when A2 contains “east” anywhere, in any case.
Tips
- Put text in double quotes and leave numbers bare.
"Pass"is text,50is a number, and"50"is text that will not behave like a number. - Always give the third argument. Without it a false test displays the word FALSE.
- A result can be a calculation. You do not need a second cell to hold it.
- To count or add rows that meet a condition, you do not need IF. COUNTIF and SUMIF do it in one step.
- To replace an error with a value, use IFERROR. It is shorter than testing with IF and ISERROR.
- When a nested IF maps a value to a band, a small lookup table is easier to maintain. Changing a boundary means editing a cell and leaving the formula alone.
IF or IFS
Use a single IF for a yes-or-no decision. It works everywhere and anyone who opens the file can read it.
Use IFS when you have three or more outcomes and every reader has Excel 2019 or later. The flat list is easier to check than a chain of brackets. Remember the TRUE pair at the end, since IFS has no value_if_false argument.
Use nested IF when the file may be opened in Excel 2016 or earlier, where IFS shows #NAME?.
If you would like the formula written for you from a description such as “A if 90 or more, B if 80 or more”, try the Excel formula generator.
Errors and fixes
| Error | Why it happens | Fix |
|---|---|---|
| #NAME? | Text in the formula has no double quotes, so Excel reads it as a name, as in =IF(B2>=50, Pass, Fail). | Put text values in double quotes: "Pass". |
| 0 result | A value argument is empty after its comma, as in =IF(B2>=50, "Pass", ). | Type the value you want. For an empty-looking cell use "". |
| FALSE shown | The third argument is left out and the test is false. | Add a value_if_false argument. |
| Wrong result | The cell being tested holds a number stored as text. Excel treats any text as larger than any number, so B2>=50 is always TRUE. | Convert the column to real numbers, or test VALUE(B2)>=50. |
Questions
How many IF functions can I nest?
Up to 64 levels. In practice a formula becomes hard to read after three or four. For a long list of conditions use IFS, or put the bands in a table and use a lookup such as XLOOKUP.
How do I use IF with multiple conditions?
Put AND or OR in the test. =IF(AND(B2>=50, C2="Yes"), "Pass", "Fail") needs both conditions. =IF(OR(B2>=50, C2="Yes"), "Pass", "Fail") needs at least one.
Is the IF function case-sensitive?
No. =IF(A2="yes", 1, 0) returns 1 for "yes", "Yes" and "YES". For a case-sensitive test use EXACT: =IF(EXACT(A2, "Yes"), 1, 0).
How do I check whether a cell contains certain text?
IF does not accept wildcards in a comparison. Use SEARCH inside the test: =IF(ISNUMBER(SEARCH("east", A2)), "Yes", "No"). COUNTIF also works: =IF(COUNTIF(A2, "*east*"), "Yes", "No").
How do I make IF return a blank cell?
Return empty text, written as two double quotes: =IF(B2="", "", B2*1.2). The cell looks empty, although it still holds a formula.
What is the difference between IF and IFS?
IF tests one condition and has a result for true and a result for false. IFS tests several conditions in order and returns the result for the first one that is true, with no nesting. IFS needs Excel 2019 or later.