0% found this document useful (0 votes)
39 views29 pages

Logical and Lookup Functions in Excel

The document discusses logical functions in Excel including AND, OR, XOR and NOT. It provides the syntax and examples of formulas using each function. Common uses are described, including using AND and OR with IF statements and to test multiple conditions.

Uploaded by

Franklin Asiamah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views29 pages

Logical and Lookup Functions in Excel

The document discusses logical functions in Excel including AND, OR, XOR and NOT. It provides the syntax and examples of formulas using each function. Common uses are described, including using AND and OR with IF statements and to test multiple conditions.

Uploaded by

Franklin Asiamah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIT 1

EXCEL LOGICAL FUNCTIONS


1.0 Introduction
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

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.

The formula returns TRUE if A3 is


Returns TRUE
greater than or equal to 10 or B3 is
if any argument
OR =OR(A3>=10, B3<5) less than 5, or both conditions are
evaluates to
met. If neither of the conditions it
TRUE.
met, the formula returns FALSE.

The formula returns TRUE if


Returns a either A3 is greater than or equal to
logical 10 or B3 is less than 5. If neither of
XOR =XOR(A3>=10, B3<5)
Exclusive Or of the conditions is met or both
all arguments. conditions are met, the formula
returns FALSE.

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.

1.1. 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

Returns TRUE if A3 contains "Bananas" and B3


=AND(A3="Bananas", B3>C3)
is greater than C3, FALSE otherwise.

Returns TRUE if B3 is greater than 20 and B3 is


=AND(B3>20, B3=C3)
equal to C3, FALSE otherwise.

Returns TRUE if A3 contains "Bananas", B3 is


=AND(A3="Bananas", B3>=30, B3>C3) greater than or equal to 30 and B3 is greater than
C3, FALSE otherwise.

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:

=IF(AND(A3="Bananas", B2>C2), "Good", "Bad")

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:

Formula to check if X is between Y and Z, inclusive:

=IF(AND(A3>=B3,A3<=C3),"Yes", "No")

Formula to check if X is between Y and Z, not inclusive:

=IF(AND(A3>B3, A3<C3),"Yes", "No")

1.2. 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.

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

Returns TRUE if A3 contains "Bananas" or


=OR(A3="Bananas", A3="Oranges")
"Oranges", FALSE otherwise.

Returns TRUE if B3 is greater than or equal to 40 or


=OR(B3>=40, C3>=20)
C3 is greater than or equal to 20, FALSE otherwise.

Returns TRUE if either B2 or C2 is blank or both,


=OR(B3=" ", C3="")
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:

1.2.1. IF function with nested OR

=IF(OR(B3>30, C3>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.

1.2.2. Excel AND / OR functions in one formula

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:

=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))

1.3. 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],…)

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:

• 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

Returns TRUE because the 1st argument is TRUE and the


=XOR(1>0, 2<1) TRUE
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:

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.

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 reasonable results:

=IF(XOR(B2="Won", C2="Won"), "Yes", "No")

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:

=IF(NOT(ISBLANK(C2)), C2*0.15, “No bonus”)

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.

LOOKUP & REFERENCE FUNCTIONS


1.5. Key Terms
Before we dive into the arcane twists of Excel Lookup formulas, let's define the key terms to
ensure that we are always on the same page.

Lookup - searching for a specified value in a table of data.

Lookup value - a value to search for.

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).

1.6.1. Excel LOOKUP function - syntax and uses

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.

1.6.2. Excel LOOKUP function - vector form

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.

The syntax of the vector Lookup is as follows:

LOOKUP(lookup_value, lookup_vector, [result_vector])

Where:

Lookup_value(required) - a value to search for. It can be a number, text, logical value


of TRUE or FALSE, or a reference to a cell containing the lookup value.

Lookup_vector(required) - one-row or one-column range to be searched. It must be


sorted in ascending order.

Result_vector(optional) - one-row or onea-column range from which you want to


return the result - a value in the same position as the lookup value. Result_vector must
be the same size as lookup_range. If omitted, the result is returned from lookup_vector.

The following examples demonstrate two simple Lookup formulas in action.

1.6.2.1. Lookup formula for vertical search - search in one-column range

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)

To better understand the arguments, please have a look at this screenshot:

1.6.2.2. Lookup formula for horizontal search - search in one-row range

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.

5. If lookup_value is smaller than the smallest value in lookup_vector, Excel LOOKUP


returns the #N/A error.

1.6.3. Excel LOOKUP function - array form


The array form of the LOOKUP function searches the specified value in the first column
or row of the array and retrieves a value from the same position in the last column or row
of the array.

The array Lookup has 2 arguments, both of which are required:

LOOKUP(lookup_value, array)

Where:

• Lookup_value - a value to search for in an array.

• 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.

1.6.4. Lookup as alternative to nested IFs

In all of the Lookup formulas we've discussed so far,


the lookup_vector and result_vector arguments were represented by range references.
However, the syntax of the Excel LOOKUP function allows supplying the vectors in the
form of a vertical array constant, which enables you to replicate the functionality
of nested IF with a more compact and easy-to-read formula.

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:

=IF(A2="c", "Completed", IF(A2="d", "Development", IF(A2="t", "Testing",


"")))

Or, by using this Lookup formula:

=LOOKUP(A2, {"c";"d";"t"}, {"Completed";"Development";"Testing"})

As shown in the screenshot below, both formulas yield identical results:

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:

=LOOKUP(VLOOKUP(E2, $A$2:$C$7, 3, FALSE), {"c";"d";"t"},


{"Completed";"Development";"Testing"})

As demonstrated in the screenshot below, the formula retrieves the project status from the lookup
table and replaces an abbreviation with the corresponding word:

1.7. VLOOKUP function

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.

The syntax for the VLOOKUP function is as follows:

VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])

As you see, the function has 4 arguments - the first three are requited and the last one is optional.

• Lookup_value is the value to search for.


This can be a value (number, date or text), cell reference (reference to a cell containing a
lookup value), or the value returned by some other function. Unlike numbers and cell
references, text values should always be in enclosed in "double quotes".

As an example, please have a look at the below formula and try to "translate" it into English:

=VLOOKUP("lion", A2:B6, 2, FALSE)

17
At this point, we can only say that the formula obviously looks up the word "lion".

• Table_array is two or more columns of data.

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.

• Range_lookup (optional) - determines whether to search for approximate or exact match:


o TRUE or omitted (default) - approximate match. If an exact match is not found,
the formula searches for the closest match, i.e. the largest value that is smaller
than the lookup value. Requires sorting the lookup column in ascending order.
o FALSE - exact match. The formula searches for a value exactly equal to the
lookup value. If an exact match is not found, a #N/A value is returned.

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:

=VLOOKUP("lion", A2:B6, 2, FALSE)

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:

=VLOOKUP(E1, A2:B6, 2, FALSE)

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:

To pull the animal names from column B:

=VLOOKUP($E2, $A$2:$C$11, 2, FALSE)

To extract speed from column C:

=VLOOKUP($E2, $A$2:$C$11, 3, FALSE)

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.7.2. Excel VLOOKUP - 5 things to remember

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.

1.7.3. How to Vlookup from another sheet in Excel

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:

=VLOOKUP(A2, Prices!$A$2:$C$9, 3, FALSE)

N.B:

• If the spreadsheet name contains spaces or non-alphabetical characters, it must be


enclosed in single quotation marks, e.g. 'Price list'!$A$2:$C$9.
• In case you use a VLOOKUP formula for multiple cells, remember to lock
table_array with the $ sign, like $A$2:$C$9.

1.7.4. How to Vlookup from another workbook in Excel

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:

=VLOOKUP(A2, [Price_List.xlsx]Prices!$A$2:$C$9, 3, FALSE)

21
If either a workbook name or worksheet name contains spaces or non-alphabetical characters,
you should enclose them in single quotes like this:

=VLOOKUP(A2, '[Price List.xlsx]Prices'!$A$2:$C$9, 3, FALSE)

The easiest way to make a VLOOKUP formula that refers to a different workbook is this:

1. Open both files.


2. Start typing your formula, switch to the other workbook, and select the table array using
the mouse.
3. Enter the remaining arguments and press the Enter key to complete your formula.

The result is as shown below:

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:

=VLOOKUP(A2, Prices_2020, 3, FALSE)

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.

1.7.7. Excel VLOOKUP approximate match (TRUE)

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.

An approximate match Vlookup works with the following caveats:

• 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.

1.7.8. Example 1. How to do an exact match Vlookup

To look up an exact match, just put FALSE in the last argument.

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:

=VLOOKUP(D2, $A$2:$B$12, 2, FALSE)

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:

• Sort the first column of table_array from smallest to largest.

• Use TRUE for the range_lookup argument or omit it.

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:

=VLOOKUP(D2, $A$2:$B$12, 2, TRUE)

And returns the following results:

• For a lookup value of "80", "Cheetah" is returned because its speed (70) is the closest
match that is smaller than the lookup value.

• For a lookup value of "50", an exact match is returned (Lion).

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.

1.8. HLOOKUP function

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.

Pros: Easy to use, can return exact and approximate matches.

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.

1.8.1. Excel HLOOKUP syntax and uses


The HLOOKUP function in Excel has the following arguments:

HLOOKUP(lookup_value, table_array, row_index_num, [range_lookup])

• 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 TRUE or omitted, an approximate match is returned. What it means is if an exact match is


not found, your Hlookup formula will do a non-exact match and return the next largest value that
is less than lookup_value.

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:

HLOOKUP(lookup_value, table_array, row_index_num, [range_lookup])


into normal English:
HLOOKUP(search for this value, in this table, return a value from this row, [return an
approximate or exact match])

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.

In our Hlookup formula, we will be using the following arguments:


• Lookup_value is B5 - the cell containing the planet name you want to find.
• Table_array is B2:I3 - the table where the formula will look up the value.
• Row_index_num is 2 because Diameter is the 2nd row in the table.
• Range_lookup is FALSE. Because the first row of our table is not sorted from A to Z,
we can only look up with exact match, which works just fine in this example.

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.

1.8.3. How to use HLOOKUP in Excel - formula examples

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.

a. Horizontal lookup with approximate and exact match


As you already know, the HLOOKUP function in Excel can perform a lookup with exact and
non-exact match depending on which value is supplied to the range_lookup argument:
• TRUE or omitted - approximate match
• FALSE - exact match

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.

HLOOKUP with approximate match


Supposing you have a list of planets in row 2 (B2:I2) and their temperatures in row 1(B1:I1).
You want to find out which planet has a certain temperature that is input in cell B4.
You cannot rely on the off chance that your users know the lookup temperature exactly, so it
makes sense to return a nearest match if an exact value in not found.

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:

=IFERROR(HLOOKUP(B4, B1:I2, 2, FALSE), "Sorry, nothing has been found")

29

You might also like