Logical and Lookup Functions in Excel
Logical and Lookup Functions in Excel
The following table provides a short summary of what each logical function does to help you
choose the right formula for a specific task.
Returns TRUE
The formula returns TRUE if a
if all of the
value in cell A3 is greater than or
AND arguments =AND(A3>=10, B3<5)
equal to 10, and a value in B3 is
evaluate to
less than 5, FALSE otherwise.
TRUE.
1
Returns the
reversed logical
value of its
argument. I.e. If The formula returns FALSE if a
NOT the argument is =NOT(A3>=10) value in cell A3 is greater than or
FALSE, then equal to 10; TRUE otherwise.
TRUE is
returned and
vice versa.
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
2
1.1.1. 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:
3
1.1.2. 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:
=IF(AND(A3>=B3,A3<=C3),"Yes", "No")
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.
4
And now, let's write down a few formulas for you to get a feel how the OR function in Excel
works.
Formula Description
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:
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.
5
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:
=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:
XOR(logical1, [logical2],…)
6
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:
=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:
7
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.
=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 reasonable results:
8
1.4. 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")
9
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.
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:
10
In simple terms, 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.
11
Return value (matching value or match) - a value at the same position as the lookup value but in
another column or row (depending on whether you do vertical or horizontal lookup).
Lookup table. In computer science, a lookup table is an array of data, which is generally used to
map input values to output values. In terms of this tutorial, an Excel lookup table is nothing else
but a range of cells where you search for a lookup value.
Main table (master table) - a table into which you pull matching values.
Your lookup table and main table may have different structure and size, however they should
always contain at least one common unique identifier, i.e. a column or row that holds identical
data, depending on whether you want to perform a vertical or horizontal lookup.
The following screenshot shows a sample lookup table that will be used in many of the below
examples.
Below is a quick overview of the most popular formulas to perform lookup in Excel, their main
advantages and drawbacks.
12
1.6. LOOKUP function
The LOOKUP function in Excel can perform the simplest types of vertical and horizontal
lookups.
Pros: Easy-to-use.
Cons: Limited functionality, cannot work with unsorted data (requires sorting the lookup
column/row in ascending order).
At the most basic level, the LOOKUP function in Excel searches a value in one column
or row and returns a matching value from the same position in another column or row.
There are two forms of LOOKUP in Excel: Vector and Array. Each form is explained
individually below.
In this context, a vector refers to a one-column or one-row range. Consequently, you use
the vector form of LOOKUP to search one row or one column of data for a specified
value, and pull a value from the same position in another row or column.
Where:
Let's say, you have a list of sellers in column D (D2:D5) and the products they sold in
column E (E2:E5). You are creating a dashboard where your users will enter the seller's
13
name in B2 and you need a formula that would pull a corresponding product in B3. The
task can be easily accomplished with this formula:
=LOOKUP(B2,D2:D5,E2:E5)
If your source data has a horizontal layout, i.e. the entries reside in rows rather than
columns, then supply a one-row range in
the lookup_vector and result_vector arguments, like this:
=LOOKUP(B2,E1:H1,E2:H2)
1.6.2.3. 5 things you should know about vector form of Excel LOOKUP
1. Values in lookup_vector should be sorted in ascending order, i.e. from largest to
smallest or from A to Z, otherwise your Excel Lookup formula may return an error or
14
incorrect result. If you need to do lookup on unsorted data, then use either INDEX
MATCH or OFFSET MATCH.
2. Lookup_vector and result_vector must be a one-row or one-column range of the
same size.
3. The LOOKUP function in Excel is case-insensitive, it does not differentiate
uppercase and lowercase text.
4. Excel LOOKUP works based on approximate match. More precisely, a Lookup
formula searches for exact match first. If it cannot find the lookup value exactly, it
looks up the next smallest value, i.e. the largest value in lookup_vector that is less
than or equal to lookup_value.
For example, if your lookup value is "5", the formula will search it first. If "5" is not
found, it will search "4". If "4" is not found, it will search "3", and so on.
LOOKUP(lookup_value, array)
Where:
• Array - a range of cells where you want to search for the lookup value. The
values in the first column or row of the array (depending on whether you do V-
lookup or H-lookup) must be sorted in ascending order. Uppercase and lowercase
characters are deemed equivalent.
For example, with the seller names located in the first column of the array (column A)
and order dates in the last column of the array (column C), you can use the following
formula to search the name and pull the matching date:
=LOOKUP(B2,D2:F5)
15
NB: The array form of the Excel LOOKUP function should not be confused with Excel array
formulas. Although it operates on arrays, LOOKUP is still a regular formula, which is
completed in the usual way by pressing the Enter key.
Let's say, you have a list of abbreviations in column A and you want to replace them with
full names, where "C" stands for "Completed", "D" is "Development, and "T" is
"Testing". The task can be accomplished with the following nested IF function:
NB: For an Excel Lookup formula to work correctly, the values in lookup_array should be
sorted from A to Z or from smallest to largest.
16
If you are pulling values from a lookup table, then you can embed a Vlookup function in
the lookup_value argument to retrieve a match.
Assuming the lookup value is in cell E2, the lookup table is A2:C7, and the column of interest
("Status") is the 3rd column in the lookup table, the following formula does the job:
As demonstrated in the screenshot below, the formula retrieves the project status from the lookup
table and replaces an abbreviation with the corresponding word:
It's an improved version of the LOOKUP function specially designed to do vertical lookup in
columns.
Pros: Relatively easy to use, can work with exact and approximate match.
Cons: Cannot look at its left, stops working when a column is inserted into or removed from the
lookup table, a lookup value cannot exceed 255 characters, requires much processing power on
large datasets.
As you see, the function has 4 arguments - the first three are requited and the last one is optional.
As an example, please have a look at the below formula and try to "translate" it into English:
17
At this point, we can only say that the formula obviously looks up the word "lion".
The VLOOKUP function searches for the lookup value in the first column of the table array,
which may contain various text values, numbers, dates, and logical values.
So, you can now read the above formula a little further: search for "lion" in the range A2:A6
(because A is the 1st column in our table array). So far, so good, right?
• Col_index_num is the number of the column from which the value should be returned.
The counting starts from the leftmost column in the table array, which is 1.
Meaning, our sample formula will return a matching value from column B, which is 2nd in the
table array.
Knowing all the arguments, you should now have no problem reading the whole formula: search
for "lion" in A2:A6, find an exact match, and return a value from column B in the same row:
For the sake of convenience, you can type the value of interest in some cell, say E1, replace the
"hardcoded" text with the cell reference, and get the formula to look up any value you input in
E1:
18
1.7.1. How to do a Vlookup in Excel
When using VLOOKUP formulas in real-life worksheets, the main rule of thumb is this: lock
table array with absolute cell references (like $A$2:$C$11) to prevent it from changing when
copying a formula to other cells.
The lookup value in most cases should be a relative reference (like E2) or you can lock only the
column coordinate ($E2). When the formula gets copied down the column, the reference will
adjust automatically for each row.
To see how it works in practice, please consider the following example. To our sample table, we
have added one more column that ranks the animals by speed (column A) and want to find the
1st, 5th and 10th fastest sprinter in the world. For this, enter the lookup ranks in some cells (E2:E4
in the screenshot below), and use the following formulas:
Enter the above formulas in cells F2 and G2, select those cells, and drag the formulas to the
below rows:
19
If you investigate the formula in a lower row, you will notice that the lookup value reference has
adjusted for that specific row, while the table array is locked:
1. The VLOOKUP function cannot look at its left. It always searches in the leftmost
column of the table array and returns a value from a column to the right. If you need to
pull values from left, use the INDEX MATCH combination that can does not care about
the positioning of the lookup and return columns.
2. The VLOOKUP function is case-insensitive, meaning that uppercase and lowercase
characters are treated as equivalent. To distinguish the letter case, use case sensitive
VLOOKUP formulas.
3. Remember about the importance of the last parameter. Use TRUE for approximate match
and FALSE for exact match.
4. When searching for approximate match, make sure the data in the lookup column is
sorted in ascending order.
5. If the lookup value is not found, a #N/A error is returned. For information about other
errors, please see Why Excel VLOOKUP is not working.
In practice, the Excel VLOOKUP function is rarely used with data in the same worksheet. Most
often you will have to pull matching data from a different worksheet.
To Vlookup from a different Excel sheet, put the worksheet's name followed by an exclamation
mark in the table_array argument before the range reference. For example, to search in the range
A2:B10 on Sheet2, use this formula:
=VLOOKUP("Product1", Sheet2!A2:B10, 2)
20
Of course, you don't have to type the sheet's name manually. Simply, start typing the formula
and when it comes to the table_array argument, switch to the lookup worksheet and select the
range using the mouse.
For instance, this is how you can look up the A2 value in the range A2:A9 on
the Prices worksheet and return a matching value from column C:
N.B:
To Vlookup from a different Excel workbook, put the workbook's name enclosed in square
brackets before the worksheet's name.
For example, here's the formula to look up the A2 value on the sheet named Prices in
the Price_List.xlsx workbook:
21
If either a workbook name or worksheet name contains spaces or non-alphabetical characters,
you should enclose them in single quotes like this:
The easiest way to make a VLOOKUP formula that refers to a different workbook is this:
Once you close the file with your lookup table, the VLOOKUP formula will continue working,
but it will now display the full path for the closed workbook:
22
1.7.5. How to Vlookup from a named range in another sheet
In case you plan to use the same lookup range in many formulas, you can create a named range
for it and type the name directly in the table_array argument.
To create a named range, simply select the cells and type the name you want in the Name box to
the left of the Formula bar.
For this example, we gave the name Prices_2020 to the data cells (A2:C9) in the lookup sheet and
get this compact formula:
23
1.7.6. Excel VLOOKUP exact match (FALSE)
If range_lookup is set to FALSE, a Vlookup formula searches for a value that is exactly equal to
the lookup value. If two or more matches are found, the 1st one is returned. If an exact match is
not found, the #N/A error occurs.
If range_lookup is set to TRUE or omitted (default), the formula looks up the closest match.
More precisely, it searches for an exact match first, and if an exact match is not found, looks for
the next largest value that is less than the lookup value.
• The lookup column must be sorted in ascending order, from smallest to largest, otherwise
a correct value may not be found.
• If the lookup value is smaller than the smallest value in the lookup array, a #N/A error is
returned.
The following examples will help you better understand the difference between an exact match
and approximate match Vlookup and when each formula is best to be used.
For this example, let's take the animal speed table, swap the columns, and try to find the animals
that can run 80, 50 and 30 miles per hour. With the lookup values in D2, D3 and D4, enter the
below formula in E2, and then copy it down to two more cells:
As you can see, the formula returns "Lion" in E3 because they run exactly 50 per hour. For the
other two lookup values an exact match is not found, and #N/A errors appear.
24
1.7.9. Example 2. How to Vlookup for approximate match
To look up an approximate match, there are two essential things you need to do:
Sorting the lookup column is very important because the VLOOKUP function stops searching as
soon as it finds a close match smaller than the lookup value. If the data is not sorted properly,
you may end up having really strange results or a bunch of #N/A errors.
For our sample data, an approximate match Vlookup formula goes as follows:
• For a lookup value of "80", "Cheetah" is returned because its speed (70) is the closest
match that is smaller than the lookup value.
25
• For a lookup value of "30", a #N/A error is returned because the lookup value is less than
the smallest value in the lookup column.
It's a horizontal counterpart of VLOOKUP that searches for a value in the first row of the lookup
table and returns the value in the same position from another row.
Cons: Can only search in the topmost row of the lookup table, is affected by the insertion or
deletion of rows, a lookup value should be under 255 characters.
• Lookup_value (required) - the value to search for. It can be a cell reference, numeric
value or text string.
• Table_array (required) - two or more rows of data in which the lookup value is
searched. It can be a regular range, named range or table. Lookup values should always
be located in the first row of table_array.
26
• Row_index_num (required) - the row number in table_array from which the value
should be returned. For example, to return the matching value from the 2nd row, set
row_index_num to 2, and so on.
• Range_lookup (optional) - a logical (Boolean) value that instructs HLOOKUP to search
with exact or approximate match.
If FALSE, only an exact match is returned. If no value in a specified row matches the lookup
value exactly, HLOOKUP throws the #N/A error.
To make things easier to understand, you can translate Excel's HLOOKUP syntax:
To see how it works in practice, let's make a simple Hlookup example. Supposing you have a
table with some basic information about the planets of our Solar system (please see the
screenshot below). What you want is a formula that returns the diameter of the planet whose
name is entered in cell B5.
Now you put the arguments together and get the following formula:
=HLOOKUP(B5,B1:J3,2,FALSE)
27
1.8.2. 3 things you should know about Excel HLOOKUP function
Whenever you do a horizontal lookup in Excel, please remember the following facts:
1. The HLOOKUP function can only search in the top-most row of table_array. If you need
to look up somewhere else, consider using an Index / Match formula.
2. HLOOKUP in Excel is case-insensitive, it does not distinguish uppercase and lowercase.
3. If range_lookup is set to TRUE or omitted (approximate match), the values in the first
row of table_array must be sorted in ascending order (A-Z) left to right.
Now that the HLOOKUP function has started to look a bit more familiar to you, let's discuss a
few more formula examples to consolidate the knowledge.
Please keep in mind that although we say "approximate match", any Hlookup formula searches
for an exact match in the first place. But setting the last argument to FALSE allows the formula
to return an approximate match (the nearest value that is less than the lookup value) if an exact
match is not found; TRUE or omitted returns the #N/A error in this case.
To better illustrate the point, consider the following HLOOKUP examples.
For instance, to find out the planet whose average temperature is around -340 °F, use the
following formula (range_lookup set to TRUE or omitted as in this example):
=HLOOKUP(B4, B1:I2, 2)
Please remember that an approximate match requires sorting the values in the top row from
smallest to largest or from A to Z, otherwise your Hlookup formula may return a wrong result.
As you can see in the screenshot below, our formula returns Uranus, one of the coldest planets in
the Solar System maintaining an average of -346 degrees Fahrenheit.
28
HLOOKUP with exact match
If you know the lookup value exactly, you can set the last parameter of HLOOKUP to FALSE:
=HLOOKUP(B4, B1:I2, 2, FALSE)
On the one hand, an approximate match Hlookup is more user-friendly because it does not
require sorting data in the first row. On the other hand, if the exact match is not found, an #N/A
error will be returned.
NB: Not to scare your users by N/A errors, you can embed your Hlookup formula
in IFERROR and display your own message, for example:
29