You are on page 1of 7

Data and Computing Skills

Week 7.2.
Using Formulas for Lookups
Leveraging Excel’s Lookup Functions
Finding data in a list or table is central to many Excel formulas. Excel provides several functions
to assist in looking up data vertically, horizontally, left to right, and right to left. By nesting some
of these functions, you can write a formula that looks up the correct data even after the layout of
your table changes.

How to get started (VLOOKUP)


There are four pieces of information that you will need in order to build the VLOOKUP syntax:
1. The value you want to look up, also called the lookup value.
2. The range where the lookup value is located. Remember that the lookup value should
always be in the first column in the range for VLOOKUP to work correctly. For example,
if your lookup value is in cell C2 then your range should start with C.
3. The column number in the range that contains the return value. For example, if you
specify B2:D11 as the range, you should count B as the first column, C as the second, and
so on.
4. Optionally, you can specify TRUE if you want an approximate match or FALSE if you want
an exact match of the return value. If you don't specify anything, the default value will
always be TRUE or approximate match.
Now put all of the above together as follows:
=VLOOKUP(lookup value, range containing the lookup value, the column number in the range
containing the return value, Approximate match (TRUE) or Exact match (FALSE)).

Looking up an exact value based on a left lookup column


Many tables are arranged so that the key piece of data, the data that makes a certain row unique,
is in the far--left column. While Excel has many lookup functions, VLOOKUP was designed for
just that situation. The next figure shows a table of employees. We want to fill out a simplified
paystub form by pulling the information from this table when an employee’s ID is selected.

1|P a g e
Data and Computing Skills

The user will select an employee ID from a data validation list in cell L3 (see Figure). From that
piece of data, the employee’s name, address, and other information will be pulled into the form.
The formulas for the paystub form in Figure are shown here:

• Employee Name
=VLOOKUP($L$3,$B$3:$I$12,2,FALSE)
• Pay
=VLOOKUP($L$3,$B$3:$I$12,5,FALSE)/VLOOKUP($L$3,$B$3:$I$12,4,FALSE)
• Taxes
=(M7-O8-O9)*VLOOKUP($L$3,$B$3:$I$12,6,FALSE)
• Insurance
=VLOOKUP($L$3,$B$3:$I$12,7,FALSE)
• Retirement
=M7*VLOOKUP($L$3,$B$3:$I$12,8,FALSE)
• Total
=SUM(O7:O10)
• Net Pay
=M7-O11
The formula to retrieve the employee’s name uses the VLOOKUP function. VLOOKUP takes four
arguments: lookup value, lookup range, column, and match. VLOOKUP will search down the first
column of the lookup range until it finds the lookup value. Once the lookup value is found,
VLOOKUP returns the value in the column identified by the column argument. In this case, the
column argument is 2, and VLOOKUP returns the employee’s name from the second column of
the lookup range.

2|P a g e
Data and Computing Skills

The other formulas also use VLOOKUP with a few twists. The address and insurance formulas
work just like the employee name formula, but they pull from a different column. The pay
formula uses two VLOOKUPs; one is divided by the other. The employee’s annual pay is pulled
from the fifth column, and that is divided by the frequency from the fourth column, resulting in
the pay for one paystub.
The retirement formula pulls the percentage from the eighth column and multiplies that by the
gross pay to calculate the deduction. Finally, the taxes formula deducts both insurance and
retirement from gross pay and multiplies that by the tax rate, found with VLOOKUP pulling from
the sixth column.
Of course, payroll calculations are a little more complex than this, but once you understand how
VLOOKUP works, you can build ever more complex models.

Looking up values horizontally


If the data is structured in such a way that your lookup value is in the top row rather than the
first column and you want to look down the rows for data rather than across the columns, Excel
has a function just for you.
The figure shows a table of cities and their temperatures. The user will select a city from a drop-
-down box, and you will return the temperature to the cell just below it.
=HLOOKUP(C5,C2:L3,2,FALSE)

The HLOOKUP function has the same arguments as VLOOKUP. The H in HLOOKUP stands for
“horizontal,” while the V in VLOOKUP stands for “vertical.” Instead of looking down the first

3|P a g e
Data and Computing Skills

column for the lookup_value argument, HLOOKUP looks across the first row. When it finds a
match, it returns the value from the second row of the matching column.

Make Excel Drop Down List in Worksheet Cell


Create a drop down list of items in a cell, with Excel's Data Validation feature. This will make
data entry easier, and reduce input errors and typing mistakes.

https://support.microsoft.com/en-us/office/apply-data-validation-to-cells-29fecbcc-d1b9-
42c1-9d76-eff3ce5f7249

Hiding errors returned by lookup functions


So far, we’ve used FALSE for the last argument of our lookup functions so that we return only
exact matches. When we force a lookup function to return an exact match and it can’t find one,
it returns the #N/A error.
The #N/A error is useful in Excel models because it alerts you when a match cannot be found.
But you may be using all or a portion of your model for reporting, and #N/As are ugly. Excel
has functions to see those errors and return something different.

4|P a g e
Data and Computing Skills

Figure shows a list of companies and CEOs. The other list shows CEOs and salaries. A VLOOKUP
function is used to combine the two tables. But we obviously don’t have salary information for
all of the CEOs, so we have a lot of #N/A errors.
=VLOOKUP(C3,$F$3:$G$11,2,FALSE)

In Figure the formula has been changed to use the IFERROR function to return a blank if there’s
no information available.
=IFERROR(VLOOKUP(C3,$F$3:$G$11,2,FALSE),"")
The IFERROR function accepts a value or formula for its first argument and an alternate return
value for its second argument. When the first argument returns an error, the second argument
is returned. When the first argument is not an error, the results of the first argument are
returned.

In this example, we’ve made our alternate return value an empty string (two double quotes with
nothing between them). That keeps the report nice and clean. But you could return anything you
want, such as “No info” or zero.

5|P a g e
Data and Computing Skills

XLOOKUP function
Use the XLOOKUP function to find things in a table or range by row. For example, look up the
price of an automotive part by the part number, or find an employee name based on their
employee ID. With XLOOKUP, you can look in one column for a search term and return a result
from the same row in another column, regardless of which side the return column is on.
Note: XLOOKUP is not available in Excel 2016 and Excel 2019, however, you may come across a
situation of using a workbook in Excel 2016 or Excel 2019 with the XLOOKUP function in it
created by someone else using a newer version of Excel.

Syntax

The XLOOKUP function searches a range or an array, and then returns the
item corresponding to the first match it finds. If no match exists, then XLOOKUP can return
the closest (approximate) match.

=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found],


[match_mode], [search_mode])

Argument Description
lookup_value The value to search for
Required* *If omitted, XLOOKUP returns blank cells it finds in lookup_array.
lookup_array The array or range to search
Required

return_array The array or range to return


Required

[if_not_found] Where a valid match is not found, return the [if_not_found] text you
Optional supply.
If a valid match is not found, and [if_not_found] is missing, #N/A is
returned.
[match_mode] Specify the match type:
Optional 0 - Exact match. If none found, return #N/A. This is the default.
-1 - Exact match. If none found, return the next smaller item.
1 - Exact match. If none found, return the next larger item.
2 - A wildcard match where *, ?, and ~ have special meaning.
[search_mode] Specify the search mode to use:
Optional 1 - Perform a search starting at the first item. This is the default.
-1 - Perform a reverse search starting at the last item.
2 - Perform a binary search that relies on lookup_array being sorted
in ascending order. If not sorted, invalid results will be returned.
-2 - Perform a binary search that relies on lookup_array being sorted
in descending order. If not sorted, invalid results will be returned.

6|P a g e
Data and Computing Skills

Translated into plain English, it says: search for the F1 value in A2:A6 and return a value from
C2:C6 in the same row. No column index numbers, no sorting, no other ridiculous quirks of
Vlookup!

7|P a g e

You might also like