The tutorial explains the essence of Excel logical functions AND, OR, XOR and NOT and provides formula examples that demonstrate their common and inventive uses.
Last week we tapped into the insight of Excel logical operators that are used to compare data in different cells. Today, you will see how to extend the use of logical operators and construct more elaborate tests to perform more complex calculations. Excel logical functions such as AND, OR, XOR and NOT will help you in doing this.
Excel logical functions - overview
Microsoft Excel provides 4 logical functions to work with the logical values. The functions are AND, OR, XOR and NOT. You use these functions when you want to carry out more than one comparison in your formula or test multiple conditions instead of just one. As well as logical operators, Excel logical functions return either TRUE or FALSE when their arguments are evaluated.
The following table provides a short summary of what each logical function does to help you choose the right formula for a specific task.
Function | Description | Formula Example | Formula Description |
AND | Returns TRUE if all of the arguments evaluate to TRUE. | =AND(A2>=10, B2<5) |
The formula returns TRUE if a value in cell A2 is greater than or equal to 10, and a value in B2 is less than 5, FALSE otherwise. |
OR | Returns TRUE if any argument evaluates to TRUE. | =OR(A2>=10, B2<5) |
The formula returns TRUE if A2 is greater than or equal to 10 or B2 is less than 5, or both conditions are met. If neither of the conditions it met, the formula returns FALSE. |
XOR | Returns a logical Exclusive Or of all arguments. | =XOR(A2>=10, B2<5) |
The formula returns TRUE if either A2 is greater than or equal to 10 or B2 is less than 5. If neither of the conditions is met or both conditions are met, the formula returns FALSE. |
NOT | Returns the reversed logical value of its argument. I.e. If the argument is FALSE, then TRUE is returned and vice versa. | =NOT(A2>=10) |
The formula returns FALSE if a value in cell A1 is greater than or equal to 10; TRUE otherwise. |
In additions to the four logical functions outlined above, Microsoft Excel provides 3 "conditional" functions - IF, IFERROR and IFNA.
Excel logical functions - facts and figures
- In arguments of the logical functions, you can use cell references, numeric and text values, Boolean values, comparison operators, and other Excel functions. However, all arguments must evaluate to the Boolean values of TRUE or FALSE, or references or arrays containing logical values.
- If an argument of a logical function contains any empty cells, such values are ignored. If all of the arguments are empty cells, the formula returns #VALUE! error.
- If an argument of a logical function contains numbers, then zero evaluates to FALSE, and all other numbers including negative numbers evaluate to TRUE. For example, if cells A1:A5 contain numbers, the formula =AND(A1:A5) will return TRUE if none of the cells contains 0, FALSE otherwise.
- A logical function returns the #VALUE! error if none of the arguments evaluate to logical values.
- A logical function returns the #NAME? error if you've misspell the function's name or attempted to use the function in an earlier Excel version that does not support it. For example, the XOR function can be used in Excel 2016 and 2013 only.
- In Excel 2007 and higher, you can include up to 255 arguments in a logical function, provided that the total length of the formula does not exceed 8,192 characters. In Excel 2003 and lower, you can supply up to 30 arguments and the total length of your formula shall not exceed 1,024 characters.
Using the AND function in Excel
The AND function is the most popular member of the logic functions family. It comes in handy when you have to test several conditions and make sure that all of them are met. Technically, the AND function tests the conditions you specify and returns TRUE if all of the conditions evaluate to TRUE, FALSE otherwise.
The syntax for the Excel AND function is as follows:
Where logical is the condition you want to test that can evaluate to either TRUE or FALSE. The first condition (logical1) is required, subsequent conditions are optional.
And now, let's look at some formula examples that demonstrate how to use the AND functions in Excel formulas.
Formula | Description |
=AND(A2="Bananas", B2>C2) |
Returns TRUE if A2 contains "Bananas" and B2 is greater than C2, FALSE otherwise. |
=AND(B2>20, B2=C2) |
Returns TRUE if B2 is greater than 20 and B2 is equal to C2, FALSE otherwise. |
=AND(A2="Bananas", B2>=30, B2>C2) |
Returns TRUE if A2 contains "Bananas", B2 is greater than or equal to 30 and B2 is greater than C2, FALSE otherwise. |
Excel AND function - common uses
By itself, the Excel AND function is not very exciting and has narrow usefulness. But in combination with other Excel functions, AND can significantly extend the capabilities of your worksheets.
One of the most common uses of the Excel AND function is found in the logical_test argument of the IF function to test several conditions instead of just one. For example, you can nest any of the AND functions above inside the IF function and get a result similar to this:
=IF(AND(A2="Bananas", B2>C2), "Good", "Bad")
For more IF / AND formula examples, please check out his tutorial: Excel IF function with multiple AND conditions.
An Excel formula for the BETWEEN condition
If you need to create a between formula in Excel that picks all values between the given two values, a common approach is to use the IF function with AND in the logical test.
For example, you have 3 values in columns A, B and C and you want to know if a value in column A falls between B and C values. To make such a formula, all it takes is the IF function with nested AND and a couple of comparison operators:
Formula to check if X is between Y and Z, inclusive:
=IF(AND(A2>=B2,A2<=C2),"Yes", "No")
Formula to check if X is between Y and Z, not inclusive:
=IF(AND(A2>B2, A2<C2),"Yes", "No")
As demonstrated in the screenshot above, the formula works perfectly for all data types - numbers, dates and text values. When comparing text values, the formula checks them character-by-character in the alphabetic order. For example, it states that Apples in not between Apricot and Bananas because the second "p" in Apples comes before "r" in Apricot. Please see Using Excel comparison operators with text values for more details.
As you see, the IF /AND formula is simple, fast and almost universal. I say "almost" because it does not cover one scenario. The above formula implies that a value in column B is smaller than in column C, i.e. column B always contains the lower bound value and C - the upper bound value. This is the reason why the formula returns "No" for row 6, where A6 has 12, B6 - 15 and C6 - 3 as well as for row 8 where A8 is 24-Nov, B8 is 26-Dec and C8 is 21-Oct.
But what if you want your between formula to work correctly regardless of where the lower-bound and upper-bound values reside? In this case, use the Excel MEDIAN function that returns the median of the given numbers (i.e. the number in the middle of a set of numbers).
So, if you replace AND in the logical test of the IF function with MEDIAN, the formula will go like:
=IF(A2=MEDIAN(A2:C2),"Yes","No")
And you will get the following results:
As you see, the MEDIAN function works perfectly for numbers and dates, but returns the #NUM! error for text values. Alas, no one is perfect : )
If you want a perfect Between formula that works for text values as well as for numbers and dates, then you will have to construct a more complex logical text using the AND / OR functions, like this:
=IF(OR(AND(A2>B2, A2<C2), AND(A2<B2, A2>C2)), "Yes", "No")
Using the OR function in Excel
As well as AND, the Excel OR function is a basic logical function that is used to compare two values or statements. The difference is that the OR function returns TRUE if at least one if the arguments evaluates to TRUE, and returns FALSE if all arguments are FALSE. The OR function is available in all versions of Excel 2016 - 2000.
The syntax of the Excel OR function is very similar to AND:
Where logical is something you want to test that can be either TRUE or FALSE. The first logical is required, additional conditions (up to 255 in modern Excel versions) are optional.
And now, let's write down a few formulas for you to get a feel how the OR function in Excel works.
Formula | Description |
=OR(A2="Bananas", A2="Oranges") |
Returns TRUE if A2 contains "Bananas" or "Oranges", FALSE otherwise. |
=OR(B2>=40, C2>=20) |
Returns TRUE if B2 is greater than or equal to 40 or C2 is greater than or equal to 20, FALSE otherwise. |
=OR(B2=" ", C2="") |
Returns TRUE if either B2 or C2 is blank or both, FALSE otherwise. |
As well as Excel AND function, OR is widely used to expand the usefulness of other Excel functions that perform logical tests, e.g. the IF function. Here are just a couple of examples:
IF function with nested OR
=IF(OR(B2>30, C2>20), "Good", "Bad")
The formula returns "Good" if a number in cell B3 is greater than 30 or the number in C2 is greater than 20, "Bad" otherwise.
Excel AND / OR functions in one formula
Naturally, nothing prevents you from using both functions, AND & OR, in a single formula if your business logic requires this. There can be infinite variations of such formulas that boil down to the following basic patterns:
=AND(OR(Cond1, Cond2), Cond3)
=AND(OR(Cond1, Cond2), OR(Cond3, Cond4)
=OR(AND(Cond1, Cond2), Cond3)
=OR(AND(Cond1,Cond2), AND(Cond3,Cond4))
For example, if you wanted to know what consignments of bananas and oranges are sold out, i.e. "In stock" number (column B) is equal to the "Sold" number (column C), the following OR/AND formula could quickly show this to you:
=OR(AND(A2="bananas", B2=C2), AND(A2="oranges", B2=C2))
OR function in Excel conditional formatting
=OR($B2="", $C2="")
The rule with the above OR formula highlights rows that contain an empty cell either in column B or C, or in both.
For more information about conditional formatting formulas, please see the following articles:
Using the XOR function in Excel
In Excel 2013, Microsoft introduced the XOR function, which is a logical Exclusive OR function. This term is definitely familiar to those of you who have some knowledge of any programming language or computer science in general. For those who don't, the concept of 'Exclusive Or' may be a bit difficult to grasp at first, but hopefully the below explanation illustrated with formula examples will help.
The syntax of the XOR function is identical to OR's :
The first logical statement (Logical 1) is required, additional logical values are optional. You can test up to 254 conditions in one formula, and these can be logical values, arrays, or references that evaluate to either TRUE or FALSE.
In the simplest version, an XOR formula contains just 2 logical statements and returns:
- TRUE if either argument evaluates to TRUE.
- FALSE if both arguments are TRUE or neither is TRUE.
This might be easier to understand from the formula examples:
Formula | Result | Description |
=XOR(1>0, 2<1) |
TRUE | Returns TRUE because the 1st argument is TRUE and the 2nd argument is FALSE. |
=XOR(1<0, 2<1) |
FALSE | Returns FALSE because both arguments are FALSE. |
=XOR(1>0, 2>1) |
FALSE | Returns FALSE because both arguments are TRUE. |
When more logical statements are added, the XOR function in Excel results in:
- TRUE if an odd number of the arguments evaluate to TRUE;
- FALSE if is the total number of TRUE statements is even, or if all statements are FALSE.
The screenshot below illustrates the point:
If you are not sure how the Excel XOR function can be applied to a real-life scenario, consider the following example. Suppose you have a table of contestants and their results for the first 2 games. You want to know which of the payers shall play the 3rd game based on the following conditions:
- Contestants who won Game 1 and Game 2 advance to the next round automatically and don't have to play Game 3.
- Contestants who lost both first games are knocked out and don't play Game 3 either.
- Contestants who won either Game 1 or Game 2 shall play Game 3 to determine who goes into the next round and who doesn't.
A simple XOR formula works exactly as we want:
=XOR(B2="Won", C2="Won")
And if you nest this XOR function into the logical test of the IF formula, you will get even more sensible results:
=IF(XOR(B2="Won", C2="Won"), "Yes", "No")
Using the NOT function in Excel
The NOT function is one of the simplest Excel functions in terms of syntax:
You use the NOT function in Excel to reverse a value of its argument. In other words, if logical evaluates to FALSE, the NOT function returns TRUE and vice versa. For example, both of the below formulas return FALSE:
=NOT(TRUE)
=NOT(2*2=4)
Why would one want to get such ridiculous results? In some cases, you might be more interested to know when a certain condition isn't met than when it is. For example, when reviewing a list of attire, you may want to exclude some color that does not suit you. I'm not particularly fond of black, so I go ahead with this formula:
=NOT(C2="black")
As usual, in Microsoft Excel there is more than one way to do something, and you can achieve the same result by using the Not equal to operator: =C2<>"black".
If you want to test several conditions in a single formula, you can use NOT in conjunctions with the AND or OR function. For example, if you wanted to exclude black and white colors, the formula would go like:
=NOT(OR(C2="black", C2="white"))
And if you'd rather not have a black coat, while a black jacket or a back fur coat may be considered, you should use NOT in combination with the Excel AND function:
=NOT(AND(C2="black", B2="coat"))
Another common use of the NOT function in Excel is to reverse the behavior of some other function. For instance, you can combine NOT and ISBLANK functions to create the ISNOTBLANK formula that Microsoft Excel lacks.
As you know, the formula =ISBLANK(A2) returns TRUE of if the cell A2 is blank. The NOT function can reverse this result to FALSE: =NOT(ISBLANK(A2))
And then, you can take a step further and create a nested IF statement with the NOT / ISBLANK functions for a real-life task:
=IF(NOT(ISBLANK(C2)), C2*0.15, "No bonus :(")
Translated into plain English, the formula tells Excel to do the following. If the cell C2 is not empty, multiply the number in C2 by 0.15, which gives the 15% bonus to each salesman who has made any extra sales. If C2 is blank, the text "No bonus :(" appears.
In essence, this is how you use the logical functions in Excel. Of course, these examples have only scratched the surface of AND, OR, XOR and NOT capabilities. Knowing the basics, you can now extend your knowledge by tackling your real tasks and writing smart elaborate formulas for your worksheets.
567 comments
Hey Svetlana,
I might be going past what Excel is capable of doing, but I'm trying to find a way for a function in excel to filter information from a cell(ie 1.2.3.4 in A1, to 1 in A2, 2 in A3, 3 in A4, and 4 in A5), and I've figured that one out using a LEFT, MID, RIGHT combination.
The problem comes when I put it into practice, because I have some that I can use a simple =LEFT(A1,1) =Mid(A1,3,1) =Right(A1,5,1) etc. and then I have some that I can't use that for(ie 12.34.56.78 or 123.456.789.123)
Essentially I'm trying to find a way to have excel differentiate, or at the very least stop when it gets to the "." I've been looking for a few hours, and I'm pretty good at figuring out how to do things in unorthodox ways, but I've hit the limit of my knowledge on this one.
I filter through large sums of data a day, so I'm trying to find a way to simplify this instead of typing these things out hundreds of time. Any advice you can give would be appreciated
i want to determine date taxaton
A1=date(21/10/2015
B1=date(19/10/2015
I want to show in C1
I A1 date later then B1 date C1date=B1date
Hi Svetlana
When all the cells are blank. As the cells are filled "pass"/"fail", then filled appropriately.
Thanks
Hi Phil ,
Then you can add one more AND function that checks for blank cells:
=IF(AND(D45="", D46="", D47="", D48=""), "", IF(AND(D45="Pass", D46="Pass", D47="Pass", D48="Pass"), "Pass", "Fail"))
Alternatively, you can use COUNTIF to make the formula more compact:
=IF(COUNTIF(D45:D48, "")=4, "", IF(COUNTIF(D45:D48, "pass")=4, "Pass", "Fail"))
Hi
I have a column of cells in which "pass" or "fail" is entered. I have a formula in the last cell (D49) set to auto fill, as an overall "fail" if only one of the cells has "fail" in it.
=IF((AND(D45="Pass", D46="Pass", D47="Pass", D48="Pass")), "Pass", "Fail")
This is what I want. However, when the other cells are blank, it auto fills to "fail", as no "pass" is present. I would like D49 to be blank if there is no text in the Cells D45 to D46. What can I add to the formula to keep it blank if cells D45 to D48 are blank?
Thanks
Hi Phil,
Do you want D49 to be blank when all the cells (D45 to D48) are blank or when at least one cell is blank?
Sir,
Thanks for Ablebits.
It helped me lot about how to make combined use of IF, OR, AND logical functions.
Regarads,
P.G. Kerkar
Goa.
My question is related to And function.
I have the below Duration in Dates
Phase 1 Start : Phase 1 End | Phase 2 Start : Phase 2 End.
October 1 : October 5 | October 4 : October 10
I am comparing each October date if it falls in Phase 1 then return "X" Value and if falls in Phase 2 return "Y" Value.. However if you see OCT 3 and 4th falls in both Phases how to generate "Z" Value for those dates.
what logic should i use for developing a duty roster of 40 people with the conditions as:
No repetition of duty till each out of 40 personnel performs duty.
No two personnel from same section within a week.
HI i need help. im trying to ask excel to evaluate a less than but greater than scenario. for example: IF(A3 is greater than 1 but less than 100) , "...." ... Anybody?
Hi Francisco,
You can use an AND statement, like this:
=IF(AND(A3>1, A3<100), value_if_true, value_if_false)
Hi,
I need a Function that is yes and no column are multiple as below, how use function.. i have tried =if(and(
FUNCTION, But not able, plz help
South9601 NO South9601 NO 0 N/A
South9567 Yes South9566 Yes SOUTH7065 Yes
SOUTH5965 NO SOUTH5965 NO SOUTH5965 NO
SOUTH7146 NO SOUTH7146 NO SOUTH7146 NO
SOUTH5067 NO SOUTH5067 NO SOUTH5067 NO
South9734 Yes South9574 Yes 0 N/A
SOUTH8400 NO SOUTH8400 NO SOUTH8400 NO
SOUTH8288 NO SOUTH8288 NO SOUTH8288 NO
I appreciate your skill & devotion to make known Excel Technology to the world. By the help your free Excel tutorials, I have developed my Excel skill & used it to solve many practical problems. Eg. I have created a smart Excel format to identify the least bidder with a short period & with 100% accuracy.
Dear Sir: I have one question to be answered.
I'm a teacher in a high school and a home room teacher in particular for 50 students. They are learning 10 subjects in a year. Thanks for your smart Excel Tutorials that enables me to manage the students mark using Excel like their total mark, average, Rank and their mark analyses. But at the end the year I'm still unable to decide (using Excel formula) whether each student is passed/Failed under the following conditions.
A student failed/Detained if.....
1. His total average mark is < 51% & scored <50% by one subject OR
2. His total average mark is <53% & scored <50% by two subjects OR
3. His total average mark is <55% & scored <50% by three subjects OR
4. Failed by any four or more subjects.
Thanks in advance
Maru from Ethiopia
hi,
I have a column with 500 numbers from A1 to A500 , ranging from 1 to 999,999 and numbers ending in M ( million ). I want a IF function to convert them all to numbers without letter M ( million ).For example i want 1.7M to be converted to 1,700,000. Please help
Hi,
I have a spreadsheet that has three columns of 200 lines
Column A has values of I,II,III and IV
Column B has values of A,B,C,D,E,F
Column C would be either High, Medium or Low based on the IA,IB,IC, IIA, IIB etc
What is the calculation for this.
To further complicate things I also need High to fill the cell Red background, Medium to fill the cell Orange background and Low to fill the cell Green background.
Its driving me mad.
=IF(AND(O13="",I13="",S13="",W13=""),"",IF(J13="NO","",IF(O13="",TODAY()-I13,O13-I13)))
How can I make this formula work, IF Cell "O13 is Blank and the Data is available with Cell S13 or W13.
I have a column with two forced choices (i.e., Group or individual). There are ten rows in the column. I am trying to create a formula that returns a "Yes" or "No" if the text "individual" is entered in any one of the ten rows in the column. The formula for one row in the column is =IF(F3="Individual", "Yes", "No"
How do I get the formula to recognize all ten cells (i.e., F3:F12) to determine if the value is present in just one of the ten cells?
svetlena !!!!!!
please send me few excel sheet for working & practise please it my request for you
I can't figure out which formula to use for the following scenario. Really hoping you can help me.
-sheet 2 will have a football roster, column a - school, column b - # on jersey, and columns c-e - will have name, grade level, stats
-I need to be able to type school code and jersey number on sheet 1 in columns a & b and have the players info display in columns c-e
So during the football game, the announcer can type in a school code, like R and a jersey number, like 2 and the name of the player will pop up.
formula in O4 cell D4-(C4+F4+I4+L4) and second formula IF(O4>=0,0,IF(O4<=0,O4)). how to used both formula in same cell.
Hello PANKAJ,
If you want to perform all the calculations with a single formula, here you go:
=IF(D4-(C4+F4+I4+L4)>=0, 0, D4-(C4+F4+I4+L4))
If you are looking for something different, please clarify.
Please send me few excel work sheet for office usage, & practise please it my request for you....
please
Hello
I am trying to create a bonus tool where 3 cells are T (Target), A (Actual) and B (Bonus)
The bonus will be calculated by achievement over target but due to the variable nature I need to input the numbers on a spreadsheet.
The logic is essentially as follows...
if A=T(1 to 1.29) THEN B=A(0.01)
in other words, if the actual is 100% to 129% of target then Bonus is 1% of Actual
Then, if possible incorporate another rule for when 130% to 149% is achieved setting the bonus at 2% of Actual
Finally incorporate one more rule for when 150% or more is achieved setting the bonus at 3% of Actual.
Many Thanks
Hi,
I am trying to determine how i can take the value of two different cells and return the difference in another cell. I realize that if I subtract one cell from the other it will give me the difference. However this will not work correctly if the first cell's value is less then the 2nd cells value. I need the third cell to show either a positive or negative number based on the values entered into the first two cells.
Thank You
Hi Ron,
I am not sure I understand the problem. For example, if A1 is 1 and B1 is 3, and you put the formula A1-B1 in C1, the answer will be -2.
If you want the formula to do something different, please clarify.
Hi,
I'm looking for a formula that will count. EG if both cells a1 and b1 are negative #s, then enter cell b1 in cell c1.
Hi Crystal,
If #s means negative numbers, you can enter the following formula in C1:
=IF(AND(A1<0, B1<0), B1, "")