Using logical functions in Excel: AND, OR, XOR and NOT

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

  1. 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.
  2. 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.
  3. 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.
  4. A logical function returns the #VALUE! error if none of the arguments evaluate to logical values.
  5. 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.
  6. 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:

AND(logical1, [logical2], …)

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.

Using the AND function in Excel formulas

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")
An example of the IF formula with a nested AND function

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")
An Excel formula for the BETWEEN condition

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:
Using IF with the MEDIAN function to find out all values between the given two values

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")
An Excel Between formula that works for text values as well as for numbers and dates

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:

OR(logical1, [logical2], …)

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.

Using the OR function in Excel

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))
The AND/OR formula to test multiple conditions

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.
Using the OR function in Excel conditional formatting

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 :

XOR(logical1, [logical2],…)

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:
Excel XOR formula with multiple logical statements

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")
Using the Excel XOR function in a real-life scenario

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")
The IF formula with a nested XOR function

Using the NOT function in Excel

The NOT function is one of the simplest Excel functions in terms of syntax:

NOT(logical)

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")
Using the NOT function in Excel

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 :(")
A nested IF statement with NOT / ISBLANK functions

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

  1. Hi :). I have two possible outcomes for my formula - either cell 'J' (if cell E is greater or equal to cell G and less than or equal to cell H) or cell 'K' (if cell E is less than cell G or greater than cell H). here is my formula: =IF(E3>=G3*(AND(E3<=H3)),J3),IF(OR(E3H3))),K3) which returns #VALUE

    Please help :(.

    • Carl:
      Text and numbers don't mix very well in Excel formulas, so make sure your numbers are actually formatted as numbers.

  2. I'm looking for the correct and/or statement. When it was just AND statement, it worked. Now that I need to incorporate OR, it's not working.

    Current: =IF(AND(A46="Pending", TODAY()>E46), TODAY()-E46,"Awarded")

    Need to turn it into something that will give me the following responses.
    If A46="Closed", "Closed"

    Pretty much A46 will either be Pending, Awarded or Closed.
    Pending = Today -A46 (to give me outstanding number of days)
    Awarded = Awarded
    Closed = Closed

    HELP!

  3. Hello. how do I write for formula for:
    if K70>=33 A, if less than 33 but more than equal 28 B, if less than 28 C

    ?
    thank you

    • Macaduta:
      The logic in your statement needs to be clearer.
      It should read like "If K70 is greater than or equal to 30 then A, etc.
      What is "A", "B" and or "C". Values held in other cells? Partial cell addresses?
      The second condition needs to be clarified. Is this an AND or an OR relation to the first statement?
      After Excel checks these conditions what is it supposed to do? What if none of the conditions are true?

    • macaduta,

      what i did understand was
      if there is a value >= to 33 is "A"
      then if there is a value between 28-32 is "B"
      and if there is a value 27),AND(K7032,"A",K70<28,"C")

      • IFS(AND(AND(K70>27),AND(K7032,"A",K70<28,"C")

  4. Hi all

    I need a forumla/macro for the following argument.

    The results should be returned in cell C1 by way of a changed cell colour only.

    There are a bunch of arguments applied to each cell based on the values given in cells A1 and B1.

    If any of the conditions are met in the below table then the resulting colour should be shown in cell C1

    Cell A1 AND Cell B1 then
    VL VL Green
    VL L Green
    VL M Green
    VL H Amber
    VL VH Amber
    L VL Green
    L L Green
    L M Amber
    L H Amber
    L VH Amber
    M VL Amber
    M L Amber
    M M Amber
    M H Red
    M VH Red
    H VL Amber
    H L Red
    H M Red
    H H Red
    H VH Black
    VH VL Red
    VH L Red
    VH M Red
    VH H Black
    VH VH Black

    I'm assuming I'm going to need a macro for this?

    Many thanks

    • Jo:
      Before you look over my solution, let me say to you and anyone else seeing it that I know this formula contains about 20 too many IF/AND statements. It should be in a VLOOKUP, INDEX/MATCH or ideally in VBA code. However, the VLOOKUP and INDEX/MATCH solutions are more difficult for me to explain and the VBA is outside the scope of this blog. So, with that said I will caution you that something like this could be very difficult for anyone coming after you to follow and if you need to modify it, will be a pain. Anyway, these were the conditions you outlined so here it is.
      =IF(AND(A1="VL",B1="H"),"Amber",IF(AND(A1="VL",B1="VH"),"Amber",IF(AND(A1="M",B1="M"),"Amber",
      IF(AND(A1="M",B1="VL"),"Amber",IF(AND(A1="M",B1="L"),"Amber",IF(AND(A1="L",B1="VH"),"Amber",IF(AND(A1="L",B1="H"),"Amber",
      IF(AND(A1="L",B1="M"),"Amber",IF(AND(A1="H",B1="VL"),"Amber",IF(AND(A1="H",B1="VH"),"Black",IF(AND(A1="VH",B1="H"),"Black",
      IF(AND(A1="VH",B1="VH"),"Black",IF(AND(A1="VL",B1="VL"),"Green",IF(AND(A1="L",B1="VL"),"Green",IF(AND(A1="L",B1="L"),"Green",
      IF(AND(A1="VL",B1="L"),"Green",IF(AND(A1="VL",B1="M"),"Green",IF(AND(A1="H",B1="L"),"Red",IF(AND(A1="H",B1="L"),"Red",
      IF(AND(A1="H",B1="M"),"Red",IF(AND(A1="H",B1="H"),"Red",IF(AND(A1="M",B1="H"),"Red",IF(AND(A1="M",B1="VH"),"Red",IF(AND(A1="VH",B1="VL"),"Red",
      IF(AND(A1="VH",B1="L"),"Red",IF(AND(A1="VH",B1="M"),"Red"))))))))))))))))))))))))))
      This will put the color's words in the C cells because I needed something easy to follow during the testing. Also, you can now conditionally format the column using a IF cell text is "Amber" or whatever approach to format the cell's color. Otherwise, the Conditional Formatting formula would be a mess.
      As I say, I know there are other ways to accomplish this, but at the end of the day, I think this is the easiest, in this forum.

  5. I sell a product that has different names ( up to 700) is there a formula that I can use that can count how many times I have sold a product with one name by comparing the sales data against the list of names ?

    • Maria:
      Enter SUMPRODUCT in the search field here on AbleBits. I think you'll find the answer to your question.

  6. Is there some form of if statement that has more than 2 logical parts.
    For instance, instead of outputting TRUE or FAlSE, or the equivalent. IT could output any number of different responses.
    To copy your demonstration system;
    IF([logical1], [logical2], … [logical{n}], …, [Response1], [Response2], Response{n})

    • Sam:
      There are several techniques that work the way you're asking about. Their use depends on the required logic of the situation.
      Nested IF statements are one. They look like this:
      =IF(Q2,"Paid",IF(S2>=TODAY(),"Not due","Overdue"))
      IF AND statements look like this:
      =IF((AND(D1="eng", B11="bank1")), IBAN1
      IF OR statements look like this:
      =IF( OR( A120), "Less than 10 or more than 20")
      Then there are COUNTIF, SUMIF, IFS and others most of which are explained here in the various ABLEBITS articles. Just enter one of the above titles in the Search box and begin to learn about IF statements.

  7. IMT 23 clause
    If IMT 23 'Yes', 50 % depreciation charges on assessed amount, if No 0 % depreciation excel formula & function

  8. PLEASE CONVERT THE FOLLOWING FORMULA

    IF "A1">TODAY DATE AND EQUAL TO DATE (2018,10,1) THEN "YES" AND IF "A1">TODAY DATE AND > DATE(2018,10,1) THEN "NO" AND IF "A1" < TODAY DATE THEN "EXPIRE".

  9. please help me for below I need formula for below things

    1 TO 15000 500
    15001 TO 100000 1000
    100001 TO 250000 2500
    250001 TO 1000000 5000
    10000001 AND MORE -0.60%

    • Ramanan:
      I think this is what you want.
      Where the value you want to check is in cell H16
      =IF(H16>1000000,(H16*-0.06),IF(H16>250000,5000,IF(H16>100000,2500,IF(H16>15000,1000,IF(H16>1,500)))))

  10. 1 TO 15000 500
    15001 TO 100000 1000
    100001 TO 250000 2500
    250001 TO 1000000 5000
    10000001 AND MORE -0.60%

  11. Hi All,

    I need a formula to do the following;
    if it starts with a Letter remove all the dash, add space after the last letter,and add 1st 2 digits from the column next to it

    QP_-00091948-00-0 01.3

    The data is in 2 columns A and B.

    thank you

  12. Hello, can I ask for help?

    I have to count how many times each person went to some places in a certain range of date from different sheet.

    For example
    (Sheet 1)
    PLACES DATE PERSON
    Sydney 2018/4/3 A
    Perth 2018/3/7 A

    (Sheet 2)
    DATE RANGE TIMES Of VISIT
    2018/3/10~2018/4/5 ???

    I have tried use 'COUNTIFS' but I got '0' for the result.
    Can you tell me problem that might happen in this case?

    Thank you!

  13. hi
    i need a formula for. .

    if year 2015 then get interest 8%
    &
    if year 2016 then get interest 7.9% if year 2017 then get interest 7.8%

    • Your question is unclear, but I'm thinking you should somehow separate the data by year and then apply the interest. Otherwise, how will Excel know what year it is?

  14. HOW TO MERGE BOTH FORMULA IN A CELL

    =IF(E53=12,"BENF-CPI",IF(E53=1,IF(OR(F53="DN12",F53="DB12"),"CPI-NSDL POA",IF(OR(F53="DN22",F53="DB21"),"CPI-CDSL POA"))))

    =IF(E53=11,IF(AND(J53="02-12047200-00100398",AG53="02-12047200-00100383"),"IST",IF(J53=AG53,"IST","P2P")))

    • Create a whats app group for Excel where we can ask any Questions and reply to all that will be great.

  15. Good job you are doing.

  16. I got it

  17. Hi,
    I am new to excel. I need a formula for following situation:-
    A5=886.7, B5=1.5%
    A6=900, B6=1.5% , C1=0.0%, C2=0.1%, C3=0.2% , C4=0.3%

    Now Condition when,
    0<A6<=999, B6=B6+C1
    1000<=A6<=1499 , B6= B6+C2
    1500<=A6<=1999 , B6= B6+C3
    20002000
    Thanks for the help!!

  18. I would like to insert a value in E4 if d4 is = to a certain value,but there are 7 possible values:
    if d4 = 8.0 then 5
    d4 = 8.1 then 7
    d4 = 8.2 then 9
    d4 = 8.3 then 11
    d4 = 8.4 then 13
    d4 = 8.5 then 15

    Thanks!

  19. In Cell A1 I simply have the number 1.

    What I need is the following:

    In cell B1 I need an if statement that returns the letter D if A1 is less than 5, C if less than 10, B if less than 15 and A if less than 20.

    Thanks

  20. How to make a formula for time and minute for example i have to make a table for my employer when they came to work and when they finish work.cell A 7:30 and cell B 15:30 how much time they work.

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)