If than formulas in excel

The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if that condition is True or False.

  • =IF(Something is True, then do something, otherwise do something else)

But what if you need to test multiple conditions, where let’s say all conditions need to be True or False (AND), or only one condition needs to be True or False (OR), or if you want to check if a condition does NOT meet your criteria? All 3 functions can be used on their own, but it’s much more common to see them paired with IF functions.

Use the IF function along with AND, OR and NOT to perform multiple evaluations if conditions are True or False.

Syntax

  • IF(AND()) — IF(AND(logical1, [logical2], …), value_if_true, [value_if_false]))

  • IF(OR()) — IF(OR(logical1, [logical2], …), value_if_true, [value_if_false]))

  • IF(NOT()) — IF(NOT(logical1), value_if_true, [value_if_false]))

Argument name

Description

logical_test (required)

The condition you want to test.

value_if_true (required)

The value that you want returned if the result of logical_test is TRUE.

value_if_false (optional)

The value that you want returned if the result of logical_test is FALSE.

Here are overviews of how to structure AND, OR and NOT functions individually. When you combine each one of them with an IF statement, they read like this:

  • AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False)

  • OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False)

  • NOT – =IF(NOT(Something is True), Value if True, Value if False)

Examples

Following are examples of some common nested IF(AND()), IF(OR()) and IF(NOT()) statements. The AND and OR functions can support up to 255 individual conditions, but it’s not good practice to use more than a few because complex, nested formulas can get very difficult to build, test and maintain. The NOT function only takes one condition.

Examples of using IF with AND, OR and NOT to evaluate numeric values and text

Here are the formulas spelled out according to their logic:

Formula

Description

=IF(AND(A2>0,B2<100),TRUE, FALSE)

IF A2 (25) is greater than 0, AND B2 (75) is less than 100, then return TRUE, otherwise return FALSE. In this case both conditions are true, so TRUE is returned.

=IF(AND(A3=»Red»,B3=»Green»),TRUE,FALSE)

If A3 (“Blue”) = “Red”, AND B3 (“Green”) equals “Green” then return TRUE, otherwise return FALSE. In this case only the first condition is true, so FALSE is returned.

=IF(OR(A4>0,B4<50),TRUE, FALSE)

IF A4 (25) is greater than 0, OR B4 (75) is less than 50, then return TRUE, otherwise return FALSE. In this case, only the first condition is TRUE, but since OR only requires one argument to be true the formula returns TRUE.

=IF(OR(A5=»Red»,B5=»Green»),TRUE,FALSE)

IF A5 (“Blue”) equals “Red”, OR B5 (“Green”) equals “Green” then return TRUE, otherwise return FALSE. In this case, the second argument is True, so the formula returns TRUE.

=IF(NOT(A6>50),TRUE,FALSE)

IF A6 (25) is NOT greater than 50, then return TRUE, otherwise return FALSE. In this case 25 is not greater than 50, so the formula returns TRUE.

=IF(NOT(A7=»Red»),TRUE,FALSE)

IF A7 (“Blue”) is NOT equal to “Red”, then return TRUE, otherwise return FALSE.

Note that all of the examples have a closing parenthesis after their respective conditions are entered. The remaining True/False arguments are then left as part of the outer IF statement. You can also substitute Text or Numeric values for the TRUE/FALSE values to be returned in the examples.

Here are some examples of using AND, OR and NOT to evaluate dates.

Examples of using IF with AND, OR and NOT to evaluate dates

Here are the formulas spelled out according to their logic:

Formula

Description

=IF(A2>B2,TRUE,FALSE)

IF A2 is greater than B2, return TRUE, otherwise return FALSE. 03/12/14 is greater than 01/01/14, so the formula returns TRUE.

=IF(AND(A3>B2,A3<C2),TRUE,FALSE)

IF A3 is greater than B2 AND A3 is less than C2, return TRUE, otherwise return FALSE. In this case both arguments are true, so the formula returns TRUE.

=IF(OR(A4>B2,A4<B2+60),TRUE,FALSE)

IF A4 is greater than B2 OR A4 is less than B2 + 60, return TRUE, otherwise return FALSE. In this case the first argument is true, but the second is false. Since OR only needs one of the arguments to be true, the formula returns TRUE. If you use the Evaluate Formula Wizard from the Formula tab you’ll see how Excel evaluates the formula.

=IF(NOT(A5>B2),TRUE,FALSE)

IF A5 is not greater than B2, then return TRUE, otherwise return FALSE. In this case, A5 is greater than B2, so the formula returns FALSE.

Example of the Evaluate Formula Wizard

Using AND, OR and NOT with Conditional Formatting

You can also use AND, OR and NOT to set Conditional Formatting criteria with the formula option. When you do this you can omit the IF function and use AND, OR and NOT on their own.

From the Home tab, click Conditional Formatting > New Rule. Next, select the “Use a formula to determine which cells to format” option, enter your formula and apply the format of your choice.

Conditional Formatting > Edit Rule dialog showing the Formula method

Using the earlier Dates example, here is what the formulas would be.

Example of using AND, OR and NOT as Conditional Formatting tests

Formula

Description

=A2>B2

If A2 is greater than B2, format the cell, otherwise do nothing.

=AND(A3>B2,A3<C2)

If A3 is greater than B2 AND A3 is less than C2, format the cell, otherwise do nothing.

=OR(A4>B2,A4<B2+60)

If A4 is greater than B2 OR A4 is less than B2 plus 60 (days), then format the cell, otherwise do nothing.

=NOT(A5>B2)

If A5 is NOT greater than B2, format the cell, otherwise do nothing. In this case A5 is greater than B2, so the result will return FALSE. If you were to change the formula to =NOT(B2>A5) it would return TRUE and the cell would be formatted.

Note: A common error is to enter your formula into Conditional Formatting without the equals sign (=). If you do this you’ll see that the Conditional Formatting dialog will add the equals sign and quotes to the formula — =»OR(A4>B2,A4<B2+60)», so you’ll need to remove the quotes before the formula will respond properly.

Need more help?

Connect with an expert. Learn from live instructors.​​​​​​​

See also

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

Learn how to use nested functions in a formula

IF function

AND function

OR function

NOT function

Overview of formulas in Excel

How to avoid broken formulas

Detect errors in formulas

Keyboard shortcuts in Excel

Logical functions (reference)

Excel functions (alphabetical)

Excel functions (by category)

What to Know

  • The syntax of IF-THEN is =IF(logic test,value if true,value if false).
  • The first argument tells the function what to do if the comparison is true.
  • The second argument tells the function what to do if the comparison is false.

This article explains how to use the IF-THEN function in Excel for Microsoft 365, Excel 2019, 2016, 2013, 2010; Excel for Mac, and Excel Online, as well as a few examples.

Inputting IF-THEN in Excel

The IF-THEN function in Excel is a powerful way to add decision making to your spreadsheets. It tests a condition to see if it’s true or false and then carries out a specific set of instructions based on the results.

For example, by inputting an IF-THEN in Excel, you can test if a specific cell is greater than 900. If it is, you can make the formula return the text «PERFECT.» If it isn’t, you can make the formula return «TOO SMALL.»

There are many conditions you can enter into the IF-THEN formula.

The IF-THEN function’s syntax includes the name of the function and the function arguments inside of the parenthesis.

This is the proper syntax of the IF-THEN function:

=IF(logic test,value if true,value if false)

The IF part of the function is the logic test. This is where you use comparison operators to compare two values.

The THEN part of the function comes after the first comma and includes two arguments separated by a comma.

  • The first argument tells the function what to do if the comparison is true.
  • The second argument tells the function what to do if the comparison is false.

A Simple IF-THEN Function Example

Before moving on to more complex calculations, let’s look at a straightforward example of an IF-THEN statement. 

Our spreadsheet is set up with cell B2 as $100. We can input the following formula into C2 to indicate whether the value is larger than $1000. 

=IF(B2>1000,"PERFECT","TOO SMALL")

This function has the following arguments:

  • B2>1000 tests whether the value in cell B2 is larger than 1000.
  • «PERFECT» returns the word PERFECT in cell C2 if B2 is larger than 1000.
  • «TOO SMALL» returns the phrase TOO SMALL in cell C2 if B2 is not larger than 1000.

The comparison part of the function can compare only two values. Either of those two values can be:

  • Fixed number
  • A string of characters (text value)
  • Date or time
  • Functions that return any of the values above
  • A reference to any other cell in the spreadsheet containing any of the above values

The TRUE or FALSE part of the function can also return any of the above. This means that you can make the IF-THEN function very advanced by embedding additional calculations or functions inside of it (see below).

When inputting true or false conditions of an IF-THEN statement in Excel, you need to use quotation marks around any text you want to return, unless you’re using TRUE and FALSE, which Excel automatically recognizes. Other values and formulas don’t require quotation marks.

Inputting Calculations Into the IF-THEN Function

You can embed different calculations for the IF-THEN function to perform, depending on the comparison results.

In this example, one calculation is used to calculate the tax owed, depending on the total income in B2.

The logic test compares total income in B2 to see if it’s greater than $50,000.00.

=IF(B2>50000,B2*0.15,B2*0.10)

In this example, B2 is not larger than 50,000, so the «value_if_false» condition will calculate and return that result.

In this case, that’s B2*0.10, which is 4000.

The result is placed into cell C2, where the IF-THEN function is inserted, will be 4000.

You can also embed calculations into the comparison side of the function.

For example, if you want to estimate that taxable income will only be 80% of total income, you could change the above IF-THEN function to the following.

=IF(B2*0.8>50000,B2*0.15,B2*0.10)

This will perform the calculation on B2 before comparing it to 50,000.

Never enter a comma when entering numbers in the thousands. This is because Excel interprets a comma as the end of an argument inside of a function.

Nesting Functions Inside of an IF-THEN Function

You can also embed (or «nest») a function inside of an IF-THEN function.

This lets you perform advanced calculations and then compare the actual results to the expected results.

In this example, let’s say you have a spreadsheet with five students’ grades in column B. You could average those grades using the AVERAGE function. Depending on the class average results, you could have cell C2 return either «Excellent!» or «Needs Work.»

This is how you would input that IF-THEN function:

=IF(AVERAGE(B2:B6)>85,"Excellent!","Needs Work")

This function returns the text «Excellent!» in cell C2 if the class average is over 85. Otherwise, it returns «Needs Work.»

As you can see, inputting the IF-THEN function in Excel with embedded calculations or functions allows you to create dynamic and highly functional spreadsheets.

FAQ

  • How do I create multiple IF-THEN statements in Excel?

  • How many IF statements can you nest in Excel?

    You can nest up to 7 IF statements within a single IF-THEN statement.

  • How does conditional formatting work in Excel?

    With conditional formatting in Excel, you can apply more than one rule to the same data to test for different conditions. Excel first determines if the various rules conflict, and, if so, the program determines which conditional formatting rule to apply to the data.

Thanks for letting us know!

Get the Latest Tech News Delivered Every Day

Subscribe

The IF function runs a logical test and returns one value for a TRUE result, and another value for a FALSE result. The result from IF can be a value, a cell reference, or even another formula. By combining the IF function with other logical functions like AND and OR, you can test more than one condition at a time.

Syntax

The generic syntax for the IF function looks like this:

=IF(logical_test,[value_if_true],[value_if_false])

The first argument, logical_test, is typically an expression that returns either TRUE or FALSE. The second argument, value_if_true, is the value to return when logical_test is TRUE. The last argument, value_if_false, is the value to return when logical_test is FALSE. Both value_if_true and value_if_false are optional, but you must provide one or the other. For example, if cell A1 contains 80, then:

=IF(A1>75,TRUE) // returns TRUE
=IF(A1>75,"OK") // returns "OK"
=IF(A1>85,"OK") // returns FALSE
=IF(A1>75,10,0) // returns 10
=IF(A1>85,10,0) // returns 0
=IF(A1>75,"Yes","No") // returns "Yes"
=IF(A1>85,"Yes","No") // returns "No"

Notice that text values like «OK», «Yes», «No», etc. must be enclosed in double quotes («»). However, numeric values should not be enclosed in quotes.

Logical tests

The IF function supports logical operators (>,<,<>,=) when creating logical tests. Most commonly, the logical_test in IF is a complete logical expression that will evaluate to TRUE or FALSE. The table below shows some common examples:

Goal Logical test
If A1 is greater than 75 A1>75
If A1 equals 100 A1=100
If A1 is less than or equal to 100 A1<=100
If A1 equals «Red» A1=»red»
If A1 is not equal to «Red» A1<>»red»
If A1 is less than B1 A1<B1
If A1 is empty A1=»»
If A1 is not empty A1<>»»
If A1 is less than current date A1<TODAY()

Notice text values must be enclosed in double quotes («»), but numbers do not. The IF function does not support wildcards, but you can combine IF with COUNTIF to get basic wildcard functionality. To test for substrings in a cell, you can use the IF function with the SEARCH function.

Pass or Fail example

In the worksheet shown above, we want to assign either «Pass» or «Fail» based on a test score. A passing score is 70 or higher. The formula in D6, copied down, is:

=IF(C5>=70,"Pass","Fail")

Translation: If the value in C5 is greater than or equal to 70, return «Pass». Otherwise, return «Fail».

Note that the logical flow of this formula can be reversed. This formula returns the same result:

=IF(C5<70,"Fail","Pass")

Translation: If the value in C5 is less than 70, return «Fail». Otherwise, return «Pass».

Both formulas above, when copied down, will return correct results.

Note: If you are new to the idea of formula criteria, this article explains many examples.

Assign points based on color

In the worksheet below, we want to assign points based on the color in column B. If the color is «red», the result should be 100. If the color is «blue», the result should be 125. This requires that we use a formula based on two IF functions, one nested inside the other. The formula in C5, copied down, is:

=IF(B5="red",100,IF(B5="blue",125))

Translation: IF the value in B5 is «red», return 100. Else, if the value in B5 is «blue», return 125.

Assign points based on color with the IF function

There are three things to notice in this example:

  1. The formula will return FALSE if the value in B5 is anything except «red» or «blue»
  2. The text values «red» and «blue» must be enclosed in double quotes («»)
  3. The IF function is not case-sensitive and will match «red», «Red», «RED», or «rEd».

This is a simple example of a nested IFs formula. See below for a more complex example.

Return another formula

The IF function can return another formula as a result. For example, the formula below will return A1*5% when A1 is less than 100, and A1*7% when A1 is greater than or equal to 100:

=IF(A1<100,A1*5%,A1*7%)

Nested IF statements

The IF function can be «nested». A «nested IF» refers to a formula where at least one IF function is nested inside another in order to test for more conditions and return more possible results. Each IF statement needs to be carefully «nested» inside another so that the logic is correct. For example, the following formula can be used to assign a grade rather than a pass / fail result:

=IF(C6<70,"F",IF(C6<75,"D",IF(C6<85,"C",IF(C6<95,"B","A"))))

Up to 64 IF functions can be nested. However, in general, you should consider other functions, like VLOOKUP or XLOOKUP for more complex scenarios, because they can handle more conditions in a more streamlined fashion. For a more details see this article on nested IFs.

Note: the newer IFS function is designed to handle multiple conditions without nesting. However, a lookup function like VLOOKUP or XLOOKUP is usually a better approach unless the logic for each condition is custom.

IF with AND, OR, NOT

The IF function can be combined with the AND function and the OR function. For example, to return «OK» when A1 is between 7 and 10, you can use a formula like this:

=IF(AND(A1>7,A1<10),"OK","")

Translation: if A1 is greater than 7 and less than 10, return «OK». Otherwise, return nothing («»).

To return B1+10 when A1 is «red» or «blue» you can use the OR function like this:

=IF(OR(A1="red",A1="blue"),B1+10,B1)

Translation: if A1 is red or blue, return B1+10, otherwise return B1.

=IF(NOT(A1="red"),B1+10,B1)

Translation: if A1 is NOT red, return B1+10, otherwise return B1.

IF cell contains specific text

Because the IF function does not support wildcards, it is not obvious how to configure IF to check for a specific substring in a cell. A common approach is to combine the ISNUMBER function and the SEARCH function to create a logical test like this:

=ISNUMBER(SEARCH(substring,A1)) // returns TRUE or FALSE

For example, to check for the substring «xyz» in cell A1, you can use a formula like this:

=IF(ISNUMBER(SEARCH("xyz",A1)),"Yes","No")

Read a detailed explanation here.

More information

  • Read more about nested IFs
  • Learn how to use VLOOKUP instead of nested IFs (video)
  • 50 Examples of formula criteria

Notes

  • The IF function is not case-sensitive.
  • To count values conditionally, use the COUNTIF or the COUNTIFS functions.
  • To sum values conditionally, use the SUMIF or the SUMIFS functions.
  • If any of the arguments to IF are supplied as arrays, the IF function will evaluate every element of the array.


Download Article


Download Article

One of the most useful features of Microsoft Excel is the IF-THEN statement, which is used to perform calculations or display text based on whether a condition is true. If you’re confused about the IF function, this handy wikiHow tutorial will guide you through writing useful IF-THEN formulas, from making simple comparisons to testing multiple conditions. Read on for examples, troubleshooting tips, and more!

  1. Image titled How Do You Write an if then Formula in Excel Step 1

    =IF(logical_test, value_if_true, value_if_false). An IF statement makes a logical comparison based on whether a condition is true or false.

    • logical_test: This is the condition that you want to test (required).
    • value_if_true: The value you want returned if logical_test is true (required).
    • value_if_false: The value you want returned if logical_test is not true (optional).
  2. Advertisement

  1. Image titled How Do You Write an if then Formula in Excel Step 2

    1

    =IF(B3>C3, «Goal Met», «Goal Not Met»).

    • In this example, the condition is B3>C3, meaning «If the value of B3 is greater than C3.»
    • If the value of B3 is greater than C3, then Goal Met will appear in the cell.
    • If the value of B3 is less than the value of C3, then Goal Not Met will appear in the cell.
  2. Image titled How Do You Write an if then Formula in Excel Step 3

    2

    =IF(B3=C3, «Goal Met»).

    • In this example, the condition is B3=C3, meaning, «If the value of B3 is equal to the value of C3.»
    • If the values are equal, «Goal Met» will appear in the cell.
    • If the values are not equal, a zero will appear in the field because we did not define a third (ELSE) value.[1]
  3. Image titled How Do You Write an if then Formula in Excel Step 4

    3

    =IF(B3*2>C3, C3*400, «Good»).

    • In this example the condition is B3*2>C3, meaning «If the value of B3 multiplied by 2 is greater than the value of C3.»
    • If the value of B3*2 is greater than the value of C3, the value of C3 multiplied by 400 will appear in this cell.
    • If the value of B3*2 is not greater than the value of C3, the word «Good» will appear in the cell.
  4. Image titled How Do You Write an if then Formula in Excel Step 5

    4

    =IF(B3=»Sold», «1», «»)

    • In this example, the condition is B3="Sold", meaning «If the value of cell B3 contains the word Sold
    • If B3 contains the word «Sold,» a «1» will appear in the cell.
    • If B3 says anything other than the word «Sold,» the condition is false. Since our ELSE value is " ", which is two double quote marks with nothing in between, the cell value will be blank.
  5. Image titled How Do You Write an if then Formula in Excel Step 6

    5

    =IF(D3=»Taxed», F3*.07, «0»).

    • In this example, the condition is D3="Taxed", meaning «If the value of D3 is the word Taxed
    • If D3 contains the word «Taxed,» the result will be the value of F3 multiplied by .07.
    • If D3 contains anything other than the word «Taxed,» the result will be 0.
  6. Image titled How Do You Write an if then Formula in Excel Step 7

    6

    Things to remember.

    • An IF statement can have two different results—one result if the condition is true, and another result if the condition is false.
    • The ELSE value, which is what will happen if the result is false, is optional—if you don’t specify what to place in the cell if the condition is false, a «0» will appear in the cell. If you’d rather the cell be blank, make your ELSE value "".
    • When referring to specific text in a condition or THEN/ELSE value, always surround that text in quotation marks.
  7. Advertisement

  1. Image titled How Do You Write an if then Formula in Excel Step 8

    1

    You can nest up to 64 IF/THEN statements in a single formula. For example, let’s say you want to create a formula that calculates sales tax for purchases made from Alaska, California, Oregon, and Washington. Since each of these four states has its own tax rate, we’ll need to create a formula that contains four IF/THEN statements to test four conditions. Assuming the sale amount is in G1 and the purchaser’s state is in F1, our formula would look like this, using the 2022 tax rates for these 4 states:

    • =IF(F1=»Alaska», G1*0,IF(F1=»California»,G1*0.0725,IF(F1=»Oregon»,G1*0,IF(F1=»Washington», G1*0.065))))
  2. Image titled How Do You Write an if then Formula in Excel Step 9

    2

    Nesting many IF/THEN statements can be challenging. Aside from the very long formulas, creating very long IF/THEN statements may become cumbersome if you have to frequently change them. In our previous example, we’d need to update our IF/THEN statements any time a state’s tax rate changed.

    • A good alternative to IF/THEN statements in this situation would be to add the values to a single table and use a VLOOKUP formula to query the table for the proper rates.
    • For example, if we add a list of our states to column A and their corresponding tax rates in column B, we could write a VLOOKUP formula that does the math for us without specifying the amounts in the formula itself. The shortened version of the above nested IF/THEN formula would be =G1*VLOOKUP(F1,A:B,2,TRUE).
    • If you had to update a tax rate, you’d now only have to update it in the referenced table, not all of your formulas.
  3. Advertisement

  1. Image titled How Do You Write an if then Formula in Excel Step 10

    1

    The result is zero (0). This means that you didn’t specify a value for either the value_if_true or value_if_false arguments.[2]

    • If you don’t specify a value_if_false value, the result will always be 0 if the condition is false.
  2. Image titled How Do You Write an if then Formula in Excel Step 11

    2

    The result is the #NAME? error. This indicates that a syntax error—make sure you’ve surrounded the formula after = with parentheses, your arguments are separated by commas, and that any plain text is surrounded by quotation marks.

    • For example, this IF/THEN statement will result in the #NAME? error because there are no quotation marks around the value OK: =IF(A2>1,OK).
    • The correct syntax would be =IF(A2>1,"OK")
  3. Advertisement

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

Thanks for submitting a tip for review!

References

About This Article

Article SummaryX

1. The syntax for an IF-THEN statement is =IF(logical_test, value_if_true, value_if_false).
2. «logical_test» is the condition you want to test, such as whether a cell value is greater than or equal to another cell.
3. «value_if_true» is what the formula should do if the test condition is true.
4. «value_if_false» is what the formula should do if the test condition is «not» true.

Did this summary help you?

Thanks to all authors for creating a page that has been read 48,324 times.

Is this article up to date?

Понравилась статья? Поделить с друзьями:
  • If syntax in excel formula
  • If string contains word java
  • If statements in word documents
  • If statements in excel with dates
  • If statements in excel with and or not