If this then that but if this then that excel

Содержание

  1. If this AND that
  2. Related functions
  3. Summary
  4. Generic formula
  5. Explanation
  6. Testing the same cell
  7. If cell is this OR that
  8. Related functions
  9. Summary
  10. Generic formula
  11. Explanation
  12. Increase price if color is red or green
  13. Using IF with AND, OR and NOT functions
  14. Examples
  15. Using AND, OR and NOT with Conditional Formatting
  16. Need more help?
  17. See also
  18. IF function
  19. Simple IF examples
  20. Common problems
  21. Need more help?
  22. IF function – nested formulas and avoiding pitfalls
  23. Remarks
  24. Examples
  25. Additional examples
  26. Did you know?
  27. Need more help?

If this AND that

Summary

To do something when two cells have specific values (i.e. this AND that) that you can use the IF function together with the AND function to run a test. In cell D6, the formula is:

which returns «x» when B6 is «red» AND C6 is «small», and returns an empty string («») if not.

Generic formula

Explanation

To do something specific when two or more conditions are TRUE, you can use the IF function in combination with the AND function to evaluate conditions with a test, then take one action if the r esult is TRUE, and (optionally) do take another if the result of the test is FALSE.

In the example shown, we simply want to «flag» records where the color is red AND the size is small. In other words, we want to check cells in column B for the color «red» AND check cells in column C to see if the size is «small». Then, if both conditions are TRUE, we mark the row with an «x». In D6, the formula is:

In this formula, the logical test is this bit:

This snippet will return TRUE only if the value in B6 is «red» AND the value in C6 is «small». If either condition isn’t true, the test will return FALSE.

Next, we need to take an action when the result of the test is TRUE. In this case, we do that by adding an «x» to column D. If the test is FALSE, we simply add an empty string («»). This causes an «x» to appear in column D when both conditions are true and nothing to display if not.

Note: if we didn’t add the empty string when FALSE, the formula would actually display FALSE whenever the color is not red.

Testing the same cell

In the example above, we are checking two different cells, but there nothing that prevents you from running two tests on the same cell. For example, let’s say you want to check values in column A and then do something when a value at least 100 but less than 200. In that case you could use this code for the logical test:

Источник

If cell is this OR that

Summary

To do something when a cell is this or that (i.e. a cell is equal to «x», «y», etc.) you can use the IF function together with the OR function to run a test. In cell D6, the formula is:

which returns «x» when B6 contains «red» or «green», and an empty string («») if not. Notice the OR function is not case-sensitive.

Generic formula

Explanation

In the example shown, we want to mark or «flag» records where the color is red OR green. In other words, we want to check the color in column B, and then leave a marker (x) if we find the word «red» or «green». In D6, the formula were using is:

This is an example of nesting – the OR function is nested inside the IF function. Working from the inside-out, the logical test is created with the OR function:

OR will return TRUE if the value in B6 is either «red» OR «green», and FALSE if not. This result is returned directly to the IF function as the logical_test argument. The color in B6 is «red» so OR returns TRUE:

With TRUE as the result of the logical test, the IF function returns a final result of «x».

When the color in column B is not red or green, the OR function will return FALSE, and IF will return an empty string («») which looks like a blank cell:

As the formula is copied down the column, the result is either «x» or «», depending on the colors in column B.

Note: if an empty string («») is not provided for value_if_false, the formula will return FALSE when the color is not red or green.

Increase price if color is red or green

You can extend this formula to run another calculation, instead of simply returning «x».

For example, let’s say you want to increase the price of red and green items only by 15%. In that case, you can use the formula in column E to calculate a new price:

The logical test is the same as before. However the value_if_true argument is now a formula:

When the result of the test is TRUE, we multiply the original price in column C by 1.15, to increase by 15%. If the result of the test is FALSE, we simply return the original price. As the formula is copied down, the result is either the increased price or original price, depending on the color.

Источник

Using IF with AND, OR and NOT functions

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.

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]))

The condition you want to test.

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

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.

Here are the formulas spelled out according to their logic:

=IF(AND(A2>0,B2 0,B4 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 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.

Here are the formulas spelled out according to their logic:

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 B2,A4 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.

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.

Edit Rule dialog showing the Formula method» loading=»lazy»>

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

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

=AND(A3>B2,A3 B2,A4 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

Need more help?

​​​​​​​

See also

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

Источник

IF function

The IF function is one of the most popular functions in Excel, and it allows you to make logical comparisons between a value and what you expect.

So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.

For example, =IF(C2=”Yes”,1,2) says IF(C2 = Yes, then return a 1, otherwise return a 2).

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it’s false.

IF(logical_test, value_if_true, [value_if_false])

The condition you want to test.

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

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

Simple IF examples

In the above example, cell D2 says: IF(C2 = Yes, then return a 1, otherwise return a 2)

In this example, the formula in cell D2 says: IF(C2 = 1, then return Yes, otherwise return No)As you see, the IF function can be used to evaluate both text and values. It can also be used to evaluate errors. You are not limited to only checking if one thing is equal to another and returning a single result, you can also use mathematical operators and perform additional calculations depending on your criteria. You can also nest multiple IF functions together in order to perform multiple comparisons.

B2,”Over Budget”,”Within Budget”)» loading=»lazy»>

=IF(C2>B2,”Over Budget”,”Within Budget”)

In the above example, the IF function in D2 is saying IF(C2 Is Greater Than B2, then return “Over Budget”, otherwise return “Within Budget”)

B2,C2-B2,»»)» loading=»lazy»>

In the above illustration, instead of returning a text result, we are going to return a mathematical calculation. So the formula in E2 is saying IF(Actual is Greater than Budgeted, then Subtract the Budgeted amount from the Actual amount, otherwise return nothing).

In this example, the formula in F7 is saying IF(E7 = “Yes”, then calculate the Total Amount in F5 * 8.25%, otherwise no Sales Tax is due so return 0)

Note: If you are going to use text in formulas, you need to wrap the text in quotes (e.g. “Text”). The only exception to that is using TRUE or FALSE, which Excel automatically understands.

Common problems

What went wrong

There was no argument for either value_if_true or value_if_False arguments. To see the right value returned, add argument text to the two arguments, or add TRUE or FALSE to the argument.

This usually means that the formula is misspelled.

Need more help?

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

Источник

IF function – nested formulas and avoiding pitfalls

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 True or False.

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

So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.

IF statements are incredibly robust, and form the basis of many spreadsheet models, but they are also the root cause of many spreadsheet issues. Ideally, an IF statement should apply to minimal conditions, such as Male/Female, Yes/No/Maybe, to name a few, but sometimes you might need to evaluate more complex scenarios that require nesting* more than 3 IF functions together.

* “Nesting” refers to the practice of joining multiple functions together in one formula.

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it’s false.

IF(logical_test, value_if_true, [value_if_false])

The condition you want to test.

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

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

While Excel will allow you to nest up to 64 different IF functions, it’s not at all advisable to do so. Why?

Multiple IF statements require a great deal of thought to build correctly and make sure that their logic can calculate correctly through each condition all the way to the end. If you don’t nest your formula 100% accurately, then it might work 75% of the time, but return unexpected results 25% of the time. Unfortunately, the odds of you catching the 25% are slim.

Multiple IF statements can become incredibly difficult to maintain, especially when you come back some time later and try to figure out what you, or worse someone else, was trying to do.

If you find yourself with an IF statement that just seems to keep growing with no end in sight, it’s time to put down the mouse and rethink your strategy.

Let’s look at how to properly create a complex nested IF statement using multiple IFs, and when to recognize that it’s time to use another tool in your Excel arsenal.

Examples

Following is an example of a relatively standard nested IF statement to convert student test scores to their letter grade equivalent.

97,»A+»,IF(B2>93,»A»,IF(B2>89,»A-«,IF(B2>87,»B+»,IF(B2>83,»B»,IF(B2>79,»B-«,IF(B2>77,»C+»,IF(B2>73,»C»,IF(B2>69,»C-«,IF(B2>57,»D+»,IF(B2>53,»D»,IF(B2>49,»D-«,»F»))))))))))))» loading=»lazy»>

This complex nested IF statement follows a straightforward logic:

If the Test Score (in cell D2) is greater than 89, then the student gets an A

If the Test Score is greater than 79, then the student gets a B

If the Test Score is greater than 69, then the student gets a C

If the Test Score is greater than 59, then the student gets a D

Otherwise the student gets an F

This particular example is relatively safe because it’s not likely that the correlation between test scores and letter grades will change, so it won’t require much maintenance. But here’s a thought – what if you need to segment the grades between A+, A and A- (and so on)? Now your four condition IF statement needs to be rewritten to have 12 conditions! Here’s what your formula would look like now:

It’s still functionally accurate and will work as expected, but it takes a long time to write and longer to test to make sure it does what you want. Another glaring issue is that you’ve had to enter the scores and equivalent letter grades by hand. What are the odds that you’ll accidentally have a typo? Now imagine trying to do this 64 times with more complex conditions! Sure, it’s possible, but do you really want to subject yourself to this kind of effort and probable errors that will be really hard to spot?

Tip: Every function in Excel requires an opening and closing parenthesis (). Excel will try to help you figure out what goes where by coloring different parts of your formula when you’re editing it. For instance, if you were to edit the above formula, as you move the cursor past each of the ending parentheses “)”, its corresponding opening parenthesis will turn the same color. This can be especially useful in complex nested formulas when you’re trying to figure out if you have enough matching parentheses.

Additional examples

Following is a very common example of calculating Sales Commission based on levels of Revenue achievement.

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

This formula says IF(C9 is Greater Than 15,000 then return 20%, IF(C9 is Greater Than 12,500 then return 17.5%, and so on.

While it’s remarkably similar to the earlier Grades example, this formula is a great example of how difficult it can be to maintain large IF statements – what would you need to do if your organization decided to add new compensation levels and possibly even change the existing dollar or percentage values? You’d have a lot of work on your hands!

Tip: You can insert line breaks in the formula bar to make long formulas easier to read. Just press ALT+ENTER before the text you want to wrap to a new line.

Here is an example of the commission scenario with the logic out of order:

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

Can you see what’s wrong? Compare the order of the Revenue comparisons to the previous example. Which way is this one going? That’s right, it’s going from bottom up ($5,000 to $15,000), not the other way around. But why should that be such a big deal? It’s a big deal because the formula can’t pass the first evaluation for any value over $5,000. Let’s say you’ve got $12,500 in revenue – the IF statement will return 10% because it is greater than $5,000, and it will stop there. This can be incredibly problematic because in a lot of situations these types of errors go unnoticed until they’ve had a negative impact. So knowing that there are some serious pitfalls with complex nested IF statements, what can you do? In most cases, you can use the VLOOKUP function instead of building a complex formula with the IF function. Using VLOOKUP, you first need to create a reference table:

This formula says to look for the value in C2 in the range C5:C17. If the value is found, then return the corresponding value from the same row in column D.

Similarly, this formula looks for the value in cell B9 in the range B2:B22. If the value is found, then return the corresponding value from the same row in column C.

Note: Both of these VLOOKUPs use the TRUE argument at the end of the formulas, meaning we want them to look for an approxiate match. In other words, it will match the exact values in the lookup table, as well as any values that fall between them. In this case the lookup tables need to be sorted in Ascending order, from smallest to largest.

VLOOKUP is covered in much more detail here, but this is sure a lot simpler than a 12-level, complex nested IF statement! There are other less obvious benefits as well:

VLOOKUP reference tables are right out in the open and easy to see.

Table values can be easily updated and you never have to touch the formula if your conditions change.

If you don’t want people to see or interfere with your reference table, just put it on another worksheet.

Did you know?

There is now an IFS function that can replace multiple, nested IF statements with a single function. So instead of our initial grades example, which has 4 nested IF functions:

It can be made much simpler with a single IFS function:

The IFS function is great because you don’t need to worry about all of those IF statements and parentheses.

Note: This feature is only available if you have a Microsoft 365 subscription. If you are a Microsoft 365subscriber, make sure you have the latest version of Office.

Need more help?

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

Источник

Explanation 

To do something specific when two or more conditions are TRUE, you can use the IF function in combination with the AND function to evaluate conditions with a test, then take one action if the result is TRUE, and (optionally) do take another if the result of the test is FALSE.

In the example shown, we simply want to «flag» records where the color is red AND the size is small. In other words, we want to check cells in column B for the color «red» AND check cells in column C to see if the size is «small». Then, if both conditions are TRUE, we mark the row with an «x». In D6, the formula is:

=IF(AND(B6="red",C6="small"),"x","")

In this formula, the logical test is this bit:

AND(B6="red",C6="small")

This snippet will return TRUE only if the value in B6 is «red» AND the value in C6 is «small». If either condition isn’t true, the test will return FALSE.

Next, we need to take an action when the result of the test is TRUE. In this case, we do that by adding an «x» to column D. If the test is FALSE,  we simply add an empty string («»). This causes an «x» to appear in column D when both conditions are true and nothing to display if not.

Note: if we didn’t add the empty string when FALSE, the formula would actually display FALSE whenever the color is not red.

Testing the same cell

In the example above, we are checking two different cells, but there nothing that prevents you from running two tests on the same cell. For example, let’s say you want to check values in column A and then do something when a value at least 100 but less than 200.  In that case you could use this code for the logical test:

=AND(A1>=100,A1<200)
  1. 06-22-2018, 09:02 AM


    #1

    Supersadie is offline


    Forum Contributor


    If this then that, but if this then that, but if this then that, otherwise this

    I’m in a proper tangle over a combination formula.

    If A1=1 or 4 or 7, then «1» but if a1=2 or 5 or 8, then «2», if A1=3 or 6 or 9, then «3», otherwise «x»

    I should be able to do it, but it’s eluding me with «too many arguments» coming up…

    Many thanks, people


  2. 06-22-2018, 09:06 AM


    #2

    Re: If this then that, but if this then that, but if this then that, otherwise this

    Since you have Office 365, you should have SWITCH function available.

    Try…

    Last edited by CK76; 06-22-2018 at 09:10 AM.

    ?Progress isn’t made by early risers. It’s made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein


  3. 06-22-2018, 09:10 AM


    #3

    Supersadie is offline


    Forum Contributor


    Re: If this then that, but if this then that, but if this then that, otherwise this

    Interesting. Not come across ‘switch’. Will see if it works. Thanks for opening another useful chapter in my learning curve.
    Best wishes.


  4. 06-22-2018, 09:18 AM


    #4

    Supersadie is offline


    Forum Contributor


    Re: If this then that, but if this then that, but if this then that, otherwise this

    Yup. Like a dream. Thanks, CK76!


  5. 06-22-2018, 09:24 AM


    #5

    Re: If this then that, but if this then that, but if this then that, otherwise this

    Bear in mind, however, that should you share the workbook with colleagues who are not running Office 365, it won’t work for them.

    Ali


    Enthusiastic self-taught user of MS Excel who’s always learning!
    Don’t forget to say «thank you» to anyone who has offered you help in your thread. You can reward them by clicking on * Add Reputation below theur user name on the left, if you wish.

    Forum Rules (updated September 2018): please read them here.
    How to use the Power Query code you’ve been given: help here. More about the Power suite here.


  6. 06-22-2018, 09:29 AM


    #6

    Re: If this then that, but if this then that, but if this then that, otherwise this

    If above case you can use…

    Edit: Can be shortened to…

    Last edited by CK76; 06-22-2018 at 09:32 AM.


  7. 06-22-2018, 09:31 AM


    #7

    Re: If this then that, but if this then that, but if this then that, otherwise this

    just a different version…
    =IF(OR(A1=1,A1=4,A1=7),1,IF(OR(A1=2,A1=5,A1=8),2,IF(OR(A1=3,A1=6,A1=9),3,»x»)))

    edit: a slightly shorter version too…
    =IF(OR(A1={1,4,7}),1,IF(OR(A1={2,5,8}),2,IF(OR(A1={3,6,9}),3,»x»)))
    just an FYI

    Last edited by Sam Capricci; 06-22-2018 at 10:00 AM.

    Make contributors happy, click on the «* Add Reputation» as a way to say thank you.
    Sam Capricci


  8. 06-22-2018, 09:59 AM


    #8

    Supersadie is offline


    Forum Contributor


    Re: If this then that, but if this then that, but if this then that, otherwise this

    That’s what I was struggling to do, Sambo Kid — I suspect it was my incorrect use of double quotes around numbers that might have upset the applecart.
    Many thanks.


  9. 06-22-2018, 10:02 AM


    #9

    Re: If this then that, but if this then that, but if this then that, otherwise this

    well, double quotes around a number will make it a text so if the original reference (A1 for example) is numeric but the formula you use to refer to it is «1» it will change it to a text then not find it.
    check out my second formula I just posted, it is a little shorter.


  10. 06-22-2018, 10:04 AM


    #10

    Supersadie is offline


    Forum Contributor


    Re: If this then that, but if this then that, but if this then that, otherwise this

    I like the curly brackets. I was sure I didn’t have to do a1=this, a2=that into infinity! Helpful.


  11. 06-22-2018, 10:08 AM


    #11

    Re: If this then that, but if this then that, but if this then that, otherwise this

    Last edited by Sam Capricci; 06-22-2018 at 10:29 AM.


IF function

The IF function is one of the most popular functions in Excel, and it allows you to make logical comparisons between a value and what you expect.

So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.

For example, =IF(C2=”Yes”,1,2) says IF(C2 = Yes, then return a 1, otherwise return a 2).

Your browser does not support video. Install Microsoft Silverlight, Adobe Flash Player, or Internet Explorer 9.

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it’s false.

IF(logical_test, value_if_true, [value_if_false])

For example:

  • =IF(A2>B2,»Over Budget»,»OK»)

  • =IF(A2=B2,B4-A4,»»)

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.

Simple IF examples

Cell D2 contains a formula =IF(C2="Yes",1,2)

  • =IF(C2=”Yes”,1,2)

In the above example, cell D2 says: IF(C2 = Yes, then return a 1, otherwise return a 2)

Cell D2 contains the formula =IF(C2=1,"YES","NO")

  • =IF(C2=1,”Yes”,”No”)

In this example, the formula in cell D2 says: IF(C2 = 1, then return Yes, otherwise return No)As you see, the IF function can be used to evaluate both text and values. It can also be used to evaluate errors. You are not limited to only checking if one thing is equal to another and returning a single result, you can also use mathematical operators and perform additional calculations depending on your criteria. You can also nest multiple IF functions together in order to perform multiple comparisons.

Formula in cell D2 is =IF(C2>B2,”Over Budget”,”Within Budget”)

  • =IF(C2>B2,”Over Budget”,”Within Budget”)

In the above example, the IF function in D2 is saying IF(C2 Is Greater Than B2, then return “Over Budget”, otherwise return “Within Budget”)

Formula in cell E2 is =IF(C2>B2,C2-B2,"")

  • =IF(C2>B2,C2-B2,0)

In the above illustration, instead of returning a text result, we are going to return a mathematical calculation. So the formula in E2 is saying IF(Actual is Greater than Budgeted, then Subtract the Budgeted amount from the Actual amount, otherwise return nothing).

Formula in Cell F7 is IF(E7=”Yes”,F5*0.0825,0)

  • =IF(E7=”Yes”,F5*0.0825,0)

In this example, the formula in F7 is saying IF(E7 = “Yes”, then calculate the Total Amount in F5 * 8.25%, otherwise no Sales Tax is due so return 0)

Note: If you are going to use text in formulas, you need to wrap the text in quotes (e.g. “Text”). The only exception to that is using TRUE or FALSE, which Excel automatically understands.

Common problems

Problem

What went wrong

0 (zero) in cell

There was no argument for either value_if_true or value_if_False arguments. To see the right value returned, add argument text to the two arguments, or add TRUE or FALSE to the argument.

#NAME? in cell

This usually means that the formula is misspelled.

Need more help?

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

Connect with an expert. Learn from live instructors.

See Also

IF function — nested formulas and avoiding pitfalls

IFS function

Using IF with AND, OR and NOT functions

COUNTIF function

How to avoid broken formulas

Overview of formulas in Excel

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

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

Like this post? Please share to your friends:
  • If this and that then this in excel
  • If there is a word better than love it would be you
  • If then with range in excel
  • If then goto in excel
  • If then else vba excel примеры