Excel nested if and not

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)

Things will not always be the way we want them to be. The unexpected can happen. For example, let’s say you have to divide numbers. Trying to divide any number by zero (0) gives an error. Logical functions come in handy such cases. In this tutorial, we are going to cover the following topics.

In this tutorial, we are going to cover the following topics.

  • What is a Logical Function?
  • IF function example
  • Excel Logic functions explained
  • Nested IF functions

What is a Logical Function?

It is a feature that allows us to introduce decision-making when executing formulas and functions. Functions are used to;

  • Check if a condition is true or false
  • Combine multiple conditions together

What is a condition and why does it matter?

A condition is an expression that either evaluates to true or false. The expression could be a function that determines if the value entered in a cell is of numeric or text data type, if a value is greater than, equal to or less than a specified value, etc.

IF Function example

We will work with the home supplies budget from this tutorial. We will use the IF function to determine if an item is expensive or not. We will assume that items with a value greater than 6,000 are expensive. Those that are less than 6,000 are less expensive. The following image shows us the dataset that we will work with.

Logical functions (operators) and conditions in Excel

  • Put the cursor focus in cell F4
  • Enter the following formula that uses the IF function

=IF(E4<6000,”Yes”,”No”)

HERE,

  • “=IF(…)” calls the IF functions
  • “E4<6000” is the condition that the IF function evaluates. It checks the value of cell address E4 (subtotal) is less than 6,000
  • “Yes” this is the value that the function will display if the value of E4 is less than 6,000
  • “No” this is the value that the function will display if the value of E4 is greater than 6,000

When you are done press the enter key

You will get the following results

Logical functions (operators) and conditions in Excel

Excel Logic functions explained

The following table shows all of the logical functions in Excel

S/N FUNCTION CATEGORY DESCRIPTION USAGE
01 AND Logical Checks multiple conditions and returns true if they all the conditions evaluate to true. =AND(1 > 0,ISNUMBER(1)) The above function returns TRUE because both Condition is True.
02 FALSE Logical Returns the logical value FALSE. It is used to compare the results of a condition or function that either returns true or false FALSE()
03 IF Logical Verifies whether a condition is met or not. If the condition is met, it returns true. If the condition is not met, it returns false.
=IF(logical_test,[value_if_true],[value_if_false])
=IF(ISNUMBER(22),”Yes”, “No”)
22 is Number so that it return Yes.
04 IFERROR Logical Returns the expression value if no error occurs. If an error occurs, it returns the error value =IFERROR(5/0,”Divide by zero error”)
05 IFNA Logical Returns value if #N/A error does not occur. If #N/A error occurs, it returns NA value. #N/A error means a value if not available to a formula or function. =IFNA(D6*E6,0)
N.B the above formula returns zero if both or either D6 or E6 is/are empty
06 NOT Logical Returns true if the condition is false and returns false if condition is true =NOT(ISTEXT(0))
N.B. the above function returns true. This is because ISTEXT(0) returns false and NOT function converts false to TRUE
07 OR Logical Used when evaluating multiple conditions. Returns true if any or all of the conditions are true. Returns false if all of the conditions are false =OR(D8=”admin”,E8=”cashier”)
N.B. the above function returns true if either or both D8 and E8 admin or cashier
08 TRUE Logical Returns the logical value TRUE. It is used to compare the results of a condition or function that either returns true or false TRUE()

A nested IF function is an IF function within another IF function. Nested if statements come in handy when we have to work with more than two conditions. Let’s say we want to develop a simple program that checks the day of the week. If the day is Saturday we want to display “party well”, if it’s Sunday we want to display “time to rest”, and if it’s any day from Monday to Friday we want to display, remember to complete your to do list.

A nested if function can help us to implement the above example. The following flowchart shows how the nested IF function will be implemented.

Logical functions (operators) and conditions in Excel

The formula for the above flowchart is as follows

=IF(B1=”Sunday”,”time to rest”,IF(B1=”Saturday”,”party well”,”to do list”))

HERE,

  • “=IF(….)” is the main if function
  • “=IF(…,IF(….))” the second IF function is the nested one. It provides further evaluation if the main IF function returned false.

Practical example

Logical functions (operators) and conditions in Excel

Create a new workbook and enter the data as shown below

Logical functions (operators) and conditions in Excel

  • Enter the following formula

=IF(B1=”Sunday”,”time to rest”,IF(B1=”Saturday”,”party well”,”to do list”))

  • Enter Saturday in cell address B1
  • You will get the following results

Logical functions (operators) and conditions in Excel

Download the Excel file used in Tutorial

Summary

Logical functions are used to introduce decision-making when evaluating formulas and functions in Excel.

We use the IF statement in Excel to test one condition and return one value if the condition is met and another if the condition is not met.

However, we use multiple or nested IF statements when evaluating numerous conditions in a specific order to return different results.

This tutorial shows four examples of using nested IF statements in Excel and gives five alternatives to using multiple IF statements in Excel.

General Syntax of Nested IF Statements (Multiple IF Statements)

The general syntax for nested IF statements is as follows:

=IF(Condition1, Value_if_true1, IF(Condition2, Value_if_true2, IF(Condition3, Value_if_true3, Value_if_false)))

This formula tests the first condition; if true, it returns the first value.

If the first condition is false, the formula moves to the second condition and returns the second value if it’s true.

Each subsequent IF function is incorporated into the value_if_false argument of the previous IF function.

This process continues until all conditions have been evaluated, and the formula returns the final value if none of the conditions is true.

The maximum number of nested IF statements allowed in Excel is 64.

Now, look at the following four examples of how to use nested IF statements in Excel.

Example #1: Use Multiple IF Statements to Assign Letter Grades Based on Numeric Scores

Let’s consider the following dataset showing some students’ scores on a Math test. 

Data set of students scores

We want to use nested IF statements to assign student letter grades based on their scores. 

We use the following steps:

  1. Select cell C2 and type in the below formula:
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C",IF(B2>=60,"D","F"))))
multiple if functions to calculate the letter grade of students
  1. Click Enter in the cell to get the result of the formula in the cell.
  1. Copy the formula for the rest of the cells in the column

The assigned letter grades appear in column C.

multiple functions used to calculate the letter grade

Explanation of the formula

=IF(B2>=90,”A”,IF(B2>=80,”B”,IF(B2>=70,”C”,IF(B2>=60,”D”,”F”))))

This formula evaluates the value in cell B2 and assigns an “A” if the value is 90 or greater, a “B” if the value is between 80 and 89, a “C” if the value is between 70 and 79, a “D” if the value is between 60 and 69, and an “F” if the value is less than 60.

Notice that it can be challenging to keep track of which parentheses go with which arguments in nested IF functions.

Therefore, as we enter the formula, Excel uses different colors for the parentheses at each level of the nested IF functions to make it easier to see which parts of the formula belong together. 

multiple functions have different color parentheses
Also read: How to use Excel If Statement with Multiple Conditions Range

Example #2: Use Multiple IF Statements to Calculate Commission Based on Sales Volume

Here’s the dataset showing the sales of specific salespeople in a particular month.

data set to calculate commission using multiple if statements

We want to use multiple IF statements to calculate the tiered commission for the salespeople based on their sales volume. 

We proceed as follows:

  1. Select cell C2 and enter the following formula:
=IF(B2>=40000, B2*0.14,IF(B2>=20000,B2*0.12,IF(B2>=10000,B2*0.105,IF(B2>0,B2*0.08,0))))
multiple if functions to calculate the commission
  1. Press the Enter key to get the result of the formula.
  2. Double-click or drag the Fill Handle to copy the formula down the column.

The commission for each salesperson is displayed in column D.

commission calculated using multiple IF functions

Explanation of the formula

=IF(B2>=40000, B2*0.14,IF(B2>=20000,B2*0.12,IF(B2>=10000,B2*0.105,IF(B2>0,B2*0.08,0))))

This formula evaluates the value in cell B2 and then does the following:

  • If the value in cell B2 is greater than or equal to 40,000, the figure is multiplied by 14% (0.14).
  • If the figure in cell B2 is less than 40,000 but greater than or equal to 20,000, the value is multiplied by 12% (0.12).
  •  If the number in cell B2 is less than 20,000 but greater than or equal to 10,000, the figure is multiplied by 10.5% (0.105).
  • If the value in cell B2 is less than 10,000 but greater than 0 (zero), the number is multiplied by 8% (0.08).
  • If the value in cell B2 is 0 (zero), 0 (zero) is returned.

Example #3: Use Multiple IF Statements to Assign Sales Performance Rating Based On Sales Target Achievement

The following is a dataset showing regional sales data of a specific technology company in a particular year.

sales data set where we need to calculate the performance rating

We want to use multiple IF statements to assign a sales performance rating to each region based on their sales target achievement.

We use the following steps:

  1. Select cell C2 and type in the below formula:
=IF(B2>500000, "Excellent", IF(B2>400000, "Good", IF(B2>275000, "Average", "Poor")))
multiple IF functions to calculate the performance rating
  1. Click Enter on the Formula bar.
  2. Drag or double-click the Fill Handle to copy the formula down the column.

The performance ratings of the regions are shown in column C.

result of the multiple if functions

Explanation of the formula

=IF(B2>500000, “Excellent”, IF(B2>400000, “Good”, IF(B2>275000, “Average”, “Poor”)))

In this formula, if the sales target in cell B2 is greater than 500,000, the formula returns “Excellent.”

If it’s between 400,000 and 500,000, the formula returns “Good.”

If it’s between 275,000 and 400,000, the formula returns “Average.” And if it’s below 275,000, the formula returns “Poor.”

Example #4: Use Multiple IF Statements in Excel to Check For Errors and Return Error Messages

Suppose we have the following dataset of students’ English test scores. Some scores are less than 0 or greater than 100, and there are no scores in some cases.

data set where some of the scores are missing

We want to use nested IF statements to check for scores in column B and display error messages in column C if there are no scores or the scores are less than 0 or greater than 100.

If the score in column B is valid, we want the formula to return an empty string in column C.

Here are the steps to follow:

  1. Select cell C2 and enter the following formula:
=IF(OR(B2<0,B2>100),"Score out of range",IF(ISBLANK(B2),"Invalid score",""))
nested if function to get the error message
  1. Click Enter on the Formula bar.
  2. Drag the Fill Handle to copy the formula down the column.

The error messages are shown in column C.

result of the multiple if functions

Explanation of the formula 

=IF(OR(B2<0,B2>100),”Score out of range”,IF(ISBLANK(B2),”Invalid score”,””))

This formula uses the OR function to check if the score in cell B2 is less than 0 or greater than 100, and if it is, it returns the error message “Score out of range.”

The formula also uses the ISBLANK function to check if cell B2 is blank, and if it is, it returns the error message “Invalid score.”

If there is no error, the formula returns an empty string, meaning no message is displayed in column B.

Alternatives to Using Multiple IF Statements in Excel

Formulas using nested IF statements can become difficult to read and manage if we have more than a few conditions to test.

In addition, if we exceed the maximum allowed limit of 64 nested IF statements, we will get an error message.

Fortunately, Excel offers alternative ways to use instead of nested IF functions, especially when we need to test more than a few conditions. 

We present the alternative ways in this tutorial.

Alternative #1: Use the IFS Function

The IFS function tests whether one or more conditions are met and returns a value corresponding to the first TRUE condition.

Before the release of the IFS function in 2018 as part of the Excel 365 update, the only way to test multiple conditions and return a corresponding value in Excel was to use nested IF statements.

However, multiple IF statements have the downside of resulting in unwieldy formulas that are difficult to read and maintain.

In some situations, the IFS function is designed to replace the need for multiple IF functions.

The syntax of the IFS function is more straightforward and easier to read than nested IF statements, and it can handle up to 127 conditions.

Here’s an example:

Let’s consider the following dataset showing some students’ scores on a Math test. 

data set to calculate the grade of the students

We want to use the IFS function to assign letter grades to the students based on their scores. 

We use the following steps:

  1. Select cell C2 and type in the below formula:
=IFS(B2>=90, "A", B2>=80, "B", B2>=70, "C", B2>=60, "D", B2<60, "F")
multiple if functions to calculate the grid of the students
  1. Click Enter on the Formula bar.
  2. Drag or double-click the Fill Handle to copy the formula down the column.

The student’s letter grades are shown in column C.

result of the nested if statements

Explanation of the formula

=IFS(B2>=90, “A”, B2>=80, “B”, B2>=70, “C”, B2>=60, “D”, B2<60, “F”)

This formula tests the score in cell B2 against each condition and returns the corresponding grade letter when the condition is true. 

Limitation of IFS Function

The IFS function in Excel is designed to simplify complex nested IF statements.

However, there are situations where the IFS function may not be able to replace nested IF functions completely.

One such situation is when you must calculate or operate based on a condition or set of conditions.

While the IFS function can return a value or text string based on a condition, it cannot perform calculations or operations on that value like nested IF statements.

Another situation where the IFS function may be less useful is when you need to test for a range of conditions rather than just a specific set.

This is because the IFS function requires you to specify each condition and corresponding result separately, which can become cumbersome if you have many conditions to test—in contrast, nested IF statements allow you to test for a range of conditions using logical operators like AND and OR.

The IFS function is a powerful tool for simplifying complex logical tests in Excel.

However, there may be situations where nested IF statements are more appropriate for your needs.

We recommend that you consider both options and choose the one that best fits the specific requirements of your task.

Alternative #2: Use Nested IF Functions

We can use multiple IFS functions in a formula if we have more than one condition to test.

For example, let’s say we have the following dataset of student names and scores on a Physics test in columns A and B.

data set to calculate pass or fail for students

We want to assign a letter grade to each score and include a pass or fail designation based on whether the score is above or below 75.

Here are the steps to use:

  1. Select cell C2 and enter the following formula
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",B2>=60,"D",B2<60,"F")&"  "&IFS(B2>=75,"Pass",B2<75,"Fail")
IFS function to check for multiple conditions
  1. Click Enter on the Formula bar.
  2. Drag or double-click the Fill Handle to copy the formula down the column.

The letter grade and designation of the student scores are displayed in column C.

a result that shows the letter grid and the pass field status

Explanation of the formula

=IFS(B2>=90,”A”,B2>=80,”B”,B2>=70,”C”,B2>=60,”D”,B2<60,”F”)&”  “&IFS(B2>=75,”Pass”,B2<75,”Fail”)

This formula uses the first IFS function to assign a letter grade based on the score in column A and the second IFS function to give a pass/fail designation based on the score in column A.

The two IFS functions are combined using the ampersand (&) operator to create a single text string that displays each score’s letter grade and pass/fail designation.

Alternative #3: Use the Combination of CHOOSE and XMATCH Functions

The CHOOSE function selects a value or action from a value list based on an index number.

The XMATCH function locates and returns the relative position of an item in an array. We can combine these functions in a formula instead of nested IF functions.

Here’s an example:

Suppose we have the following dataset showing some students’ scores and letter grades on a Biology test. 

data set to calculate the grade point of students

We want to use a formula combining the CHOOSE and XMATCH functions to assign corresponding grade points in column D to each letter grade.

We use the following steps:

  1. Select cell D2 and type in the below formula:
=CHOOSE(XMATCH(C2,{"F","E","D","C","B","A"},0),0,1,2,3,4,5)
choose and match function to calculate grade point
  1. Click Enter on the Formula bar.
  2. Drag or double-click the Fill Handle to copy the formula down the column.

The grade points for each student are displayed in column D.

result of the choose and match function

Explanation of the formula

=CHOOSE(XMATCH(C2,{“F”,”E”,”D”,”C”,”B”,”A”},0),0,1,2,3,4,5)

This formula applies the XMATCH function to find the position of the letter grade in the array {“F”,”E”,”D”,”C”,”B”,”A”}, and then uses the CHOOSE function to return the corresponding grade points.

Alternative #4: Use the VLOOKUP Function

The VLOOKUP function looks for a value in the leftmost column of a table and then returns a value in the same row from a specified column.

We can use the VLOOKUP function instead of nested IF functions in Excel.

The following is an example of using the VLOOKUP function instead of nested IF functions in Excel:

Suppose we have the following dataset showing some students’ scores and letter grades on a Biology test. 

data set to calculate the grade point

We want to use the VLOOKUP function to assign grade points to each student’s letter grade in column D.

We use the steps below:

  1. Create a table that lists the grades and their corresponding grade points in cell range F1:G7.
table that shows the grade based on the grade points
  1. In cell D2, type the following formula:
=VLOOKUP(C2,$F$2:$G$7,2,FALSE)

Note: Use the dollar signs to lock down the cell range F2:G7.

vlookup function to check for multiple conditions
  1. Click Enter on the Formula bar.
  2. Drag or double-click the Fill Handle to copy the formula down the column.

The grade points for each student appear in column D.

result where the grade points are calculated

Explanation of the formula

=VLOOKUP(C2,$F$2:$G$7,2,FALSE)

This formula uses the VLOOKUP function to look up the grade in cell C2 in the table in F2:G7 and return the corresponding grade point in the second column (i.e., column G).

The “FALSE” argument ensures that an exact match is required.

Alternative #5: Use a User-Defined Function

If you need to test more than a few conditions, consider creating a User Defined Function in VBA that can handle many conditions. 

Here’s an example of using VBA code to replace nested IF functions in Excel:

Suppose we have the following dataset showing the sales of specific salespeople in a particular month.

data set to calculate tiered commission

We want to use a User Defined Function to calculate the commission for each salesperson based on the following rates:

  • If the total sales are less than $10,000, the commission rate is 8%.
  • If the total sales are equal to or greater than $10,000 but less than $20,000, the commission rate is 10.5%.
  • If the total sales are equal to or greater than $20,000 but less than $40,000, the commission rate is 12%.
  • If the sales are equal to or greater than $40,000, the commission rate is 14%

We use the following steps:

  1. Open the worksheet containing the sales dataset.
  2. Press Alt + F11 to launch the Visual Basic Editor.
  3. Click Insert on the menu bar and choose Module to insert a new module.
insert a new module
  1. Enter the following VBA code.
'Code developed by Steve Scott from https://spreadsheetplanet.com
Function COMMISSION(Sales As Double) As Double
    Const Rate1 = 0.08
    Const Rate2 = 0.105
    Const Rate3 = 0.12
    Const Rate4 = 0.14

'Calculate sales commissions

    Select Case Sales
        Case 0 To 9999.99: COMMISSION = Sales * Rate1
        Case 10000 To 19999.99: COMMISSION = Sales * Rate2
        Case 20000 To 39999.99: COMMISSION = Sales * Rate3
        Case Is >= 40000: COMMISSION = Sales * Rate4
    End Select

End Function
  1. Save the function procedure and the workbook as a Macro-Enabled Workbook.
  2. Press Alt + F11 to switch to the active worksheet with the sales dataset.
  3. Select cell C2 and enter the following formula:
=COMMISSION(B2)
enter the user defined function in the cell in worksheet
  1. Click Enter on the Formula bar.
  2. Drag or double-click the Fill Handle to copy the formula down the column.

The commission for each salesperson is displayed in column C.

result where the commission is calculated using the UDF

This VBA function takes the sales amount as an argument and returns the corresponding commission.

The User-Defined Function is a much simpler and easier-to-read solution than using nested IF functions.

This tutorial showed four examples of using nested IF statements in Excel and gave five alternatives to using multiple IF statements in Excel. We hope you found the tutorial helpful.

Other Excel tutorials you may find useful:

  • Excel Logical Test Using Multiple If Statements in Excel [AND/OR]
  • How to Compare Two Columns in Excel (using VLOOKUP & IF)
  • Using IF Function with Dates in Excel (Easy Examples)
  • COUNTIF Greater Than Zero in Excel
  • BETWEEN Formula in Excel (Using IF Function) – Examples
  • Count Cells Less than a Value in Excel (COUNTIF Less)

Following up on last week’s introductory post on using the IF function, here’s one on how to use Excel’s IF statement when you have multiple conditions to meet.

The original question I received from a reader, which prompted this series, was about how to use the IF statement in Excel when you had two conditions that had to be met. For example, when sales fell between a minimum and maximum number. Let’s take a look at our example again. Note that I’ve added the maximum amount of $200K into cell B3:

Before we go further, if you’d like to work through the examples yourself, here’s the raw data you can copy into an Excel worksheet. First, open up a blank excel worksheet. Next, highlight the table below. Copy it, and then go back to your excel worksheet. Go to cell A1 (or another empty cell, if you want to put the data elsewhere), and then select “Edit” from the menu bar. Select “Paste Special” and then “Text” from the popup box. Click “OK”. The data should appear in your Excel worksheet just as it does above.

Bonus 12%
Minimum amount 100000
Maximum amount 200000
Sales Bonus
Salesman A 87925
Salesman B 100000
Salesman C 145000
Salesman D 200750
Salesman E 178650
Salesman F 99555
Salesman G 147000
Salesman H 213450
Salesman I 122680
Salesman J 92500

Ok, now back to the tutorial.

Now, let’s suppose sales have to be greater than or equal to $100K and less than $200K for a salesman to receive a 12% commission rather than just be greater than $100K, as in our introductory example. How would you write that in “Excel-speak”?

It turns out that you can use Excel’s AND function, which Excel calls a logical operator (just like it calls the IF function). And, as usual, unlike how most other programming languages work, the syntax required in Excel is a bit different. To use it correctly, you have to write it like the following:

=AND(first condition, second condition, …, etc.)

(In other programming languages, AND would fall in between each condition, just like how we normally talk, but not in Excel!)

Let’s go back to the concrete example. To write the condition that sales have to fall betwen $100K and $200K for the salesman to receive a 12% commission, we’d write the following in cell C6:

=IF(AND(B6>=$B$2, B6<$B$3),B6*$B$1,»No bonus»)

Like this:

Translated into plain English, our IF statement now reads, “If B6 is greater than or equal to B2 and B6 is less than B3, then multiply B6 by B1. If not, then put ‘No bonus’ into the cell.” In the first case, our salesman didn’t meet the $100K requirement, so the AND function returned a false, so the IF statement put “No bonus” into the cell. By the way, in our case, we only had two conditions to meet, but if we had more, we could just keep adding them into the list of conditions in the parenthese after the AND function.

Here’s how the results look if we do this for all the salesmen in our example:

There are two other logical functions, OR and NOT, which work the exact same way:

=OR(first condition, second condition, …, etc.)
=NOT(first condition, second condition, …, etc.)

For example, we could actually rewrite our IF statement above using the OR condition if we’re careful. Here’s how it would look:

=IF(OR(B6<$B$2,B6>=$B$3),”No bonus”, B6*$B$1)

Like this:

Again, in plain English, what we’re saying is, “If B6 is less than B2 or B6 is greater than or equal to B3, put ‘No bonus’ into the cell. Otherwise, multiply B6 by B1.” We’re basically testing for the opposite conditions in our OR statement as we did in our AND statement. And you’ll get the same results as above with the AND statement, as seen below:

You might be able to picture now how you could start using multiple AND, OR, and IF statements nested altogether to test all sorts of complicated conditions in Excel. But keep in mind that cells in Excel are really meant more for doing calculations than programming, which is really what you’re getting into once you start putting in complex IF statements. (I’ve read somewhere you can nest up to 7 IF statements in Excel, though I’m not sure to which version(s) of Excel that limitation applies.)

In particular, coming up with a correct list of conditions to test for, in the right order, that don’t result in a false positive test (e.g. a condition accidentally being met that you didn’t mean to) is a big concern in Excel. The other problem is usability. Even if you were able to come up with a complex IF statement that did what you wanted it to, imagine you put the spreadsheet away and revisited it again in a year’s time when you had to update annual sales figures and calculate bonuses again. Here’s an example of a complicated nested IF statement with no other logical operators:

=IF(A1<20, A1*1, IF(A1<50, A1*2, IF(A1<100, A1*3, A1*4)))

Think about it: would you really want to spend an extra hour or two decoding that and figure out what you needed to change or update a year later? Similarly, if you had to use a worksheet that someone else wrote that contained a bunch of complicated IF statements, wouldn’t you go nuts? I would!

The statement above is actually even in “reverse” order of what you might normally expect. (In case you’re wondering, I pulled this example from the bottom of a webpage here. It provides the original reader’s question, though why he or she wanted to calculate that, I’m not sure!)

I’ll follow up this post with one on conditional formatting, and I’ve had a request to explain VLOOKUP, HLOOKUP, and pivot tables. Feel free to suggest other tutorials you’d like to see, and I’ll try my best to get to them!

***************************************************

Look Good at Work and Become Indispensable Become an Excel Pro and Impress Your Boss


***************************************************

Explanation 

This article describes the Excel nested IF construction. Usually, nested IFs are used when you need to test more than one condition and return different results depending on those tests.

Testing more than one condition

If you need to test for more than one condition, then take one of several actions, depending on the result of the tests, one option is to nest multiple IF statements together in one formula. You’ll often hear this referred to as «nested IFs».

The idea of nesting comes from embedding or «nesting» one IF function inside another. In the example shown, we are using nested IF functions to assign grades based on a score. The logic for assigning a grade goes like this:

Score Grade
0-63 F
64-72 D
73-84 C
85-94 B
95-100 A

To build up a nested IF formula that reflects this logic, we start by testing to see if the score is below 64. If TRUE, we return «F». If FALSE, we move into the next IF function. This time, we test to see if the score is less than 73. If TRUE, we return «D». If FALSE, we move into yet another IF function. And so on.

Eventually, the formula we have in cell D5 looks like this:

=IF(C5<64,"F",IF(C5<73,"D",IF(C5<85,"C",IF(C5<95,"B","A"))))

You can see that it’s important in this case to move in one direction, either low to high, or high to low. This allows us to return a result whenever a test returns TRUE, because we know that the previous tests have returned FALSE.

Making nested IFs easier to read

By their nature, nested IF formulas can be hard to read. If this bothers you, you can add line breaks inside the formula to «line up» the tests and results. This video explains how to add line breaks to a nested if.

Notes

  1. The newer IFS function can handle multiple conditions in a single function.
  2. VLOOKUP can sometimes be used to replace complicated nested ifs.
  3. This article has many more examples of nested ifs.

Nested IF allows you to create complex IF functions. But also, Microsoft has built new functions to avoid creating very long IF formulae.

Methods for making nested IF

There are 5 methods to perform nested IF in Excel

  • Several IF in the same formula
  • The VLOOKUP function (option appromiate match)
  • The XLOOKUP function
  • The IFS function (Excel 2016 and more)
  • The SWITCH function (Excel 2016 and more)

Let’s see when it’s better for you to use one of these functions

Search between values

If you want to return a value inside a range of numbers, the functions VLOOKUP or better XLOOKUP do the job. In the following example, you return the percentage according to the number of cars sold.

VLOOKUP returns the result for any values

Write Several IF

But if you have to build a test that doesn’t follow a range of values, you must use another technique.

How to build a nested IF?

The IF function returns 2 results according to the result of a logic test. But what if we are looking to return more than 2 results?

The method is to write another IF formula for the FALSE argument.

Nested IF in Excel

Example with 3 results

Let’s say you want to give a gift to your employees according to gender and also seniority.

  • For the men, you give them the Gift 1
  • For the women with less than 5 years of seniority, it’s the Gift 2
  • And for the women with 5 years of seniority and more, it’s the Gift 3

So, first, we write a test for the men

=IF(C2=»male»,»Gift 1″)

First part of the nested IF

Now, for the FALSE argument, we write the second IF function for the test for the women and the seniority

=IF(C2=»male»,»Gift 1″,IF(AND(C2=»female»,D2<5),»Gift 2″,»Gift 3″))

Nested IF with 3 situations

Example with 4 results

And now, if we also apply the seniority for the men, the formula is more complex

=IF(AND(C2=»male»,D2<5),»Gift 1″, IF(AND(C2=»male»,D2>5),»Gift 4″,
IF(AND(C2=»female»,D2<5),»Gift 2″,»Gift 3″)))

Nested IF with 4 Result

Remark: To split your formula, place the cursor on your formula and press the shortcut Alt + Enter

IFS Function

As you can see, the more you have tested, the longest the formula is. This is why Microsoft has released a new function to avoid nested IF. This function is IFS and it’s very simple to write

  • Logical test 1 => result when test is TRUE
  • Logical test 2 => result when test is TRUE
  • Logical test 3 => result when test is TRUE
  • ….

In our case, the writing of the IFS function is

=IFS(AND(C2=»male»,D2<5),»Gift 1″, AND(C2=»male»,D2>5),»Gift 4″,
AND(C2=»female»,D2<5),»Gift 2″,TRUE,»Gift 3″)

IFS function with 4 tests

With an IFS function, if the first logical tests are FALSE, the only way to always perform the latest test is to write TRUE as the last logical test.

Понравилась статья? Поделить с друзьями:
  • Excel nan что это
  • Excel naming cells formulas
  • Excel named range worksheet
  • Excel named range not listed
  • Excel named range in formulas