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
"=IF(AND(A2="White",B21),15,IF(AND(A2="Not white",B21),12,0))))"
=IF(AND(A2="White",B21),15,IF(AND(A2="Not white",B21),12,0))))
=IF(AND(A2="White",B21),15,IF(AND(A2="Not white",B21),12,0))))
Hello Svetlana,
*Something went wrong in my previous question. Here's the correct question.
I have been trying to crack this for quite a while now. Your help will be much appreciated.
I am trying to achieve the following rate/pc:
If color is white and pcs is less than or equal to 1, rate/pc = 10
If color is white and pcs is greater than 1, rate/pc = 5
If color is not white and pcs is less than or equal to 1, rate/pc = 15
If color is not white and pcs is greater than 1, rate/pc = 12
I have been able to get the first 3 conditions correct using if(and) and if(not) however I can't get the 4th condition to work. Is there a formula for 2 not conditions?
I look forward to hearing from you soon. Thanks in advance.
=IF(AND(A2="White",B21),15,IF(AND(A2="Not white",B21),12,0))))
Hello Svetlana,
I have been trying to crack this for quite a while now. Your help will be much appreciated.
I am trying to achieve the following rate/pc:
If color is white and pcs is =1, rate/pc = 5
If color is not white and pcs is =1, rate/pc = 12
I have been able to get the first 3 conditions correct using if(and) and if(not) however I can't get the 4th condition to work. Is there a formula for 2 not conditions?
I look forward to hearing from you soon. Thanks in advance.
what formula can i use for example if cell a1:g1 is 60..i want to sum a1:g1 in one cell to be 40 and excess (20) in another cell?
Sir
How can import a particular Colum from PDF to Excel file how ever i have many of PDF files and I want Import a particular Colum (which contains some value) from PDF to Excel Please Help for this formulla or Micro Please and Please
hello, i am stucked while preparing a XL for my investments. Every investment has end date. so I want to put formula in such way that when it comes to the end date status should show "mature" or else show "active".
Please help.
Help in Excel formula that is If sheet 1 cell C2 are equal or greater than 21 then copy data sheet 1 cell A2 to C2 and paste in Sheet 2 A2 to C2 if not equal 21 then do not copy data
Hello Nisar,
Enter the following formula in Sheet2 A2, and then drag it rightwards up to cell C2:
=IF(Sheet1!$C$2>=21, Sheet1!A2, "")
i AM HIGHLY THANK FULL FORMULLA WORKS
A1 B1 C1 D1 E1 F1 D1
_________________________________________________
1450 1440 1073 301 66 73 D2
HOW I USE FUNCTION IN G2 CELL IN ABOUT CONSTANT B1 WHEN F1 IS GREATER THAN EQUAL TO E1
OTHER WISE MINUS E1 FROM B1 WHEN E1 IS GREATER THAN F1
Hello LAXMIDHAR,
If my understanding of your task is correct, you can use the following formula:
=IF(F1>=E1, B1, B1-E1)
hi, can anyone help me?
I don't to write like this
=IF(A1=10,F1=0,IF(A1=12,F1=0,IF(A1=13,F1=0,IF(A1=16,F1=0,IF(A1=18,F1=0,IF(A1=90,F1=0,IF(A1=91,F1=0,IF(A1=92,F1=0,IF(A1=93,F1=0,10000)))))))))
I want a shorter
Hi!
You can use the OR function instead of nested If's:
=IF(OR(A1=10, A1=12, A1=13, A1=16, A1=18, A1=90, A1=91, A1=92, A1=93), 0, 10000)
I want copy particular cell formula in google sheet and I want to paste special to other 3 column which macro program I have to use..
Hello! Good evening.
Pls guide me solve the problem.
1- If Table A1 value is 1, B1 is 54,
2- In continuously If A2 value is 0, b2 value is 0.
then whats the formula apply to get the total no. of receipts in both tables C1,C2. Pls give the one formula for both tables.
Recpt. No.
From To Total
1 54 ?
0 0 ?
Hello, Prem,
For us to be able to assist you better, please give us the result you need in C1 and C2.
Hi,
I need a formula whereby, when the value ranges from -0.03 to 0.03, it should appear as True and for other values it should appear as False.
i have a question..there are 4 subjects namely s1,s2,s3,s4 each 100 marks in a single group .a student should get aggregate morethan 200 to pass group and he must get 40 or above in each subject.if he get morethen 200 and failed in one subject than he is nt qualified for group........solve me showing pass/fail in each subject along with group
Can you help me with this? If A1:B1 match any A:B then add matching C cells?
Hello, TJ,
Please try this formula:
=IF(AND(INDIRECT("A1")=A3,INDIRECT("B1")=B3),"Matching","Non-matching")
A1 = RECEIVED / CANCELLED / DECLINED (Dropdown)
If A1="CANCELLED" then A2, A3, A4... will show CANCELLED
If A1="DECLINED" then A2, A3, A4... will show DECLINED
but
If A1="RECEIVED" then A2, A3, A4... must be blank
Please advise. Thank you
GOTCHA! :D
Hi, I am lost. can you help me with this problem.
I want to choose from a drop down list for the following
1) choose either ATM or CDM
2) choose either Shift1 or Shift2
3) choose the units available example 100, 200, 300 etc
4) choosing from 1) and 2) and 3) will get the result
the result will show
ATM Shift1 100 units = xxx
ATM Shift1 200 units = yyy
ATM Shift1 300 units = zzz
CDM Shift1 100 units = aaa
CDM Shift1 200 units = bbb
CDM Shift1 300 units = ccc
ATM Shift2 100 units = ddd
and so on
Is it possible? Please advise. Thanks
Hello, Michelle,
You can read how to create a dropdown for a worksheet in Making a cascading (dependent) Excel drop down list. For us to be able to assist you better, please describe the condition for the expected result.
=IF(AND(C26>=0,C26=3,C26=5,5,"error")))))
Hi
I have a 3 in one if statement to put value to certain conditions:
=IF(AND(C26>=0,C26=3,C26=5,5,"error")))))
If I make the last condition = 5, it gives me the "error" msg for anything else typed in - which is correct. When I say >5 it gives me 5 for any word typed in which should be giving me "error" msg? How do I fix this?
Thank you.
Hello, Shirleen,
This formula isn't true since a value cannot be equal to 3 and 5.5 at the same time. We suppose that you need a formula like this:
=IF(OR(C26>=0,C26=3,C26=5.5),"error", "")
Hi Svetlana!
I need assistance calculating the following information in Excel 2013, if possible. If you can simplify, please do.
--
* The “Severity” and “Status” of a case, determines how often the case should be updated
* Based on the “Last Worked” date, automatically calculate the “Due Date” for updating the case. The “Due Date” is based on business days and does not include U.S. holidays.
* Automatically countdown the difference (“Days Remaining”) between the “Last Worked” date and the “Due Date”
* Based on the “Status,” determine whether the case is “On Time,” “Overdue,” or “Completed”
--
Sev 1: If status is “In Progress” or “Customer Response Received”, update case everyday (1 day)
Sev 2: If status is “In Progress” or “Customer Response Received”, update case at least every 1 - 2 days
Sev 3/4: If status is “In Progress” or “Customer Response Received”, update case at least every 2 - 3 days
Regardless of the severity, an update is not needed for cases with a status of “Closed” or “Resolution Provided.”
--
Example 1 (based on current date 11/2/2015):
Case: 1234543 | Sev: 1 | Status: In Progress | Last Worked: 10/22 | Due Date: 10/23| Days remaining: -9 | Progress: Overdue
Example 2 (based on current date 11/2/2015):
Case: 5432123 | Sev: 1 | Status: Resolution Provided | Last Worked: 10/31 | Due Date: 11/2| Days remaining: | Progress: Completed
--
Thank you, in advance!