BUS 120 · Spreadsheets and Business Data

Lookup Functions

Learn XLOOKUP, VLOOKUP and INDEX with MATCH so a spreadsheet can fetch prices, names and rates for you.

Key takeaways

  • A lookup needs a value to search for, a place to search and a place to bring the answer back from.
  • XLOOKUP handles most jobs in newer Excel, while VLOOKUP and INDEX with MATCH work in older files too.
  • Use exact match for names and codes, and approximate match only for sorted tiers such as discount bands.
  • Each item in the lookup list needs a unique key, or the function will quietly return the first match.
  • A missing match usually points to a typo, a stray space or a number stored as text.

Hana runs an invented plant nursery. She keeps a price list on one tab and writes sales on another. For every sale, she flips to the price list, finds the item, memorizes the price, flips back and types it. By noon she has charged a customer for a lemon tree when they bought a lemon balm.

People are poor at this job. We lose our place. Spreadsheets are excellent at it, as long as you ask clearly. A lookup function searches a list for one thing and brings back something that sits beside it. It is the spreadsheet version of finding a name in a contact list and reading the phone number.

The idea: a key and a table

A lookup works best when each row in your list has a key, a value that identifies that row and no other. A product code is a good key. A first name is a poor one, because two people may share it.

The list you search is often called a lookup table. Here is Hana's, on a tab named Prices.

Row A (Code) B (Item) C (Price) D (Cost)
2 LB01 Lemon balm 6 2
3 LT02 Lemon tree 45 22
4 RM03 Rosemary 8 3
5 FN04 Fern 14 6

On her sales tab, she wants to type only the code and the quantity. Everything else should appear by itself.

What every lookup does
1 Take the key The sale row says RM03. 2 Search the key column Walk down the codes until RM03 is found. 3 Move across Stay on that row and go to the price column. 4 Return the value Bring back 8. Take the key The sale row says RM03. 1 Search the key column Walk down the codes until RM03 is found. 2 Move across Stay on that row and go to the price column. 3 Return the value Bring back 8. 4

Why not just type the price?

Typing the price on each sale seems harmless. But it creates many copies of one fact. When the price of rosemary changes, you must remember the new figure every time you type it. One tired afternoon, you use the old one. Another day, you swap two digits.

A lookup keeps each fact in one home. The price list is where prices live. Every other tab asks that list. If the list is right, the sales lines are right. If a price is wrong, you fix it in one place, and you know exactly where to look.

Good keys make this work. Hana's codes are short, they never repeat and they do not change when an item is renamed. If she starts selling a larger rosemary, it gets a new code, not a reused one. Reusing old codes is a classic way to make last year's records tell a false story.

XLOOKUP: the modern way

XLOOKUP is available in Microsoft 365 and other recent versions of Excel, and in some other spreadsheet programs. Its basic form is:

=XLOOKUP(what_to_find, where_to_look, what_to_return, if_not_found)

With the code in A2 of the sales tab:

=XLOOKUP(A2, Prices!$A$2:$A$5, Prices!$C$2:$C$5, "Check code")

Read it aloud: find A2 in the code column, return the matching price, and if you cannot find it, say "Check code." By default XLOOKUP looks for an exact match. The dollar signs lock the list so the formula can be copied down, as you learned in cells and references.

VLOOKUP: the old workhorse

You will meet VLOOKUP in many existing files, so learn to read it. The V stands for vertical.

=VLOOKUP(what_to_find, table, column_number, FALSE)

For the price:

=VLOOKUP(A2, Prices!$A$2:$D$5, 3, FALSE)

It searches the first column of the table, then returns the value from the third column of that table. The final FALSE demands an exact match. If you leave it out, VLOOKUP assumes an approximate match, which can return a confident wrong answer.

VLOOKUP has two limits. It can only return values to the right of the search column. And the column number is just a count. If someone inserts a column inside the table, the number 3 may now point somewhere else.

There is also HLOOKUP, which searches across a top row instead of down a column.

INDEX and MATCH: two small tools, one team

MATCH tells you the position of a value in a list. INDEX returns the item at a position in a list.

=MATCH("RM03", Prices!$A$2:$A$5, 0)      → 3
=INDEX(Prices!$C$2:$C$5, 3)              → 8

The 0 in MATCH means exact. Put one inside the other:

=INDEX(Prices!$C$2:$C$5, MATCH(A2, Prices!$A$2:$A$5, 0))

This pair works in older versions, can look left or right and does not rely on counting columns.

Three ways to look something up
XLOOKUP Exact match by default Looks left or right Has a built-in not-found message Needs a recent version VLOOKUP Common in older files Returns only to the right Uses a column count Add FALSE for exact match INDEX + MATCH Works in old and new versions Looks left or right Two functions to nest Use 0 for exact match XLOOKUP Exact match by default Looks left or right Has a built-in not-found message Needs a recent version VLOOKUP Common in older files Returns only to the right Uses a column count Add FALSE for exact match INDEX + MATCH Works in old and new versions Looks left or right Two functions to nest Use 0 for exact match

A worked example: building a sales line

Hana's sales tab holds three sales. She types the code in column A and quantity in column B.

Row A (Code) B (Qty)
2 RM03 6
3 LT02 2
4 FN04 3

She adds these formulas in row 2 and copies them down.

C2 Item:    =XLOOKUP(A2, Prices!$A$2:$A$5, Prices!$B$2:$B$5, "Check code")
D2 Price:   =XLOOKUP(A2, Prices!$A$2:$A$5, Prices!$C$2:$C$5)
E2 Cost:    =XLOOKUP(A2, Prices!$A$2:$A$5, Prices!$D$2:$D$5)
F2 Revenue: =B2*D2
G2 Profit:  =B2*(D2-E2)

The results:

Item Price Cost Revenue Profit
Rosemary 8 3 48 30
Lemon tree 45 22 90 46
Fern 14 6 42 24

Check row 3: revenue is 2 × 45 = 90, and profit is 2 × (45 - 22) = 46. Totals are revenue 48 + 90 + 42 = 180 and profit 30 + 46 + 24 = 100. The cost of the plants sold is 180 - 100 = 80. In bookkeeping terms, that is cost of goods sold and gross profit.

One caution: if Hana raises a price on the list next month, these old sales rows will change too. For records of past sales, copy them and use Paste Values, which keeps the numbers and drops the formulas.

Profit on each of Hana's three sales
6 rosemary $30 2 lemon trees $46 3 ferns $24 6 rosemary $30 2 lemon trees $46 3 ferns $24

Together they make $100 of gross profit on $180 of revenue. Hypothetical example: the numbers are made up to show the method, not a quote or an average.

Test your lookup before you trust it

Before copying a lookup down thousands of rows, test it on cases you can verify by eye. Try the first item in the list, the last item and one in the middle. The first and last catch ranges that start or end one row off.

Then try a code that does not exist, such as ZZ99. You should see your "Check code" message. If you see a price instead, the function is using approximate match when you wanted exact.

Next, count the problems. =COUNTIFS(C2:C4, "Check code") tells Hana how many sales lines failed. On her three rows it returns 0. On a long list, that single cell saves her from scrolling.

Finally, compare totals. If the sales tab shows 11 plants sold (6 rosemary, 2 lemon trees and 3 ferns), the stock records should drop by 11 as well. A lookup can run without errors and still point at the wrong list. A total that agrees with a separate record is much stronger evidence.

Approximate match: for tiers

Sometimes you want the band a number falls into. Hana gives bigger orders a discount. Her tier table lists the lowest order amount for each rate. The rates are invented.

Row F (Order at least) G (Discount)
2 0 0%
3 100 5%
4 250 10%

For an order total in B8:

=VLOOKUP(B8, $F$2:$G$4, 2, TRUE)
=XLOOKUP(B8, $F$2:$F$4, $G$2:$G$4, , -1)

The XLOOKUP version leaves the "not found" slot empty and uses -1, meaning "exact match, or the next smaller value." An order of 180 is at least 100 but under 250, so the discount is 5%. That saves 180 × 0.05 = 9, leaving 171. An order of exactly 250 gets 10%.

VLOOKUP with TRUE needs the first column sorted from smallest to largest. If the tiers are shuffled, the answers can be wrong without any warning.

Why use a table instead of a stack of IF tests? A table is easy to read and easy to change. If Hana adds a 15% tier for orders of 500, she types one new row and widens the range. With nested IFs, she would have to rewrite the formula and get the order right again.

Always test the edges of each band. An order of 99 earns nothing. An order of 100 earns 5%, a saving of 5. An order of 249 still earns 5%. These four or five test orders take a minute and prove that the bands begin where the shop's policy says they do.

A tier discount like this one applies a single rate to the whole order. Some systems, such as many income tax schedules, apply each rate only to the slice of money inside that band. The two designs give very different answers, so find out which one you are modeling before you write the formula.

The common mistake: matches that almost match

Hana types a code and gets #N/A, which means "not available." The code is clearly on the list. What happened? Check these in order.

  1. Extra spaces. "RM03 " with a trailing space is a different string. Use TRIM from logical and text functions.
  2. Number versus text. The number 1001 and the text "1001" do not match. Convert one side.
  3. Look-alike characters. The letter O and the digit 0 fool human eyes.
  4. A range that is too short. A new product in row 6 is outside $A$2:$A$5.

The opposite problem is worse. Suppose two rows share the code FN04 with different prices. A normal lookup returns the first one it meets and says nothing. Count your keys with =COUNTIFS($A$2:$A$100, A2). Any result above 1 is a duplicate.

Resist wrapping every lookup in IFERROR with a zero. A zero price will happily flow into your revenue. A visible message such as "Check code" is far better.

Where accountants use lookups

Lookups turn account numbers into account names, match employee codes to pay rates and pull a customer's payment terms onto an invoice. They also help compare two lists, such as your records against a bank download, during a bank reconciliation. Matching on amount alone can mislead, since two payments may be equal. Use a reference number when you have one.

A lookup is only as good as the list behind it. The next lesson, tables, sorting and filtering, shows how to keep that list neat and make ranges grow on their own.

Words to know

Lookup function a function that finds a value in a list and returns related information.

Key a value that identifies one row and no other.

Lookup table the list that a lookup function searches.

Exact match a lookup that accepts only a value identical to the one you asked for.

Approximate match a lookup that returns the band a number falls into, usually the next smaller value.

Duplicate a second row that repeats a key that should appear only once.

Check yourself

  1. What three things does every lookup need?
  2. Using the price list, what does =INDEX(Prices!$B$2:$B$5, MATCH("LT02", Prices!$A$2:$A$5, 0)) return?
  3. Hana sells 4 lemon balms. What are the revenue and the profit?
  4. Using the discount tiers, what does an order of 300 pay after the discount?
  5. Why should you add FALSE at the end of a VLOOKUP for product codes?
  6. Why is a duplicate key dangerous?
Show the answers
  1. A value to search for, a place to search and a place to return the answer from.
  2. LT02 is second in the code list, so it returns Lemon tree.
  3. Revenue is 4 × 6 = 24 and profit is 4 × (6 - 2) = 16.
  4. The rate is 10%, so the discount is 30 and the customer pays 270.
  5. Without it VLOOKUP uses approximate match and may return a wrong row without any error.
  6. The lookup returns only the first match and gives no warning that another row disagrees.

A free self-study program written for 4Accounting with AI writing tools and not reviewed line by line by a person. It is not an accredited degree and awards no credit. Check anything you rely on against a textbook or primary source. Report an error.

In this unit: Foundations

  1. How Spreadsheets Think: Cells, Ranges and ReferencesLearn how cells, ranges and references work, and why a copied formula changes in ways that can help or hurt you.11 min
  2. Formulas and Essential FunctionsBuild formulas that follow the order of operations and use SUM, AVERAGE, COUNT, ROUND and conditional totals.11 min
  3. Logical and Text FunctionsUse IF, AND, OR and IFERROR to make decisions in a sheet, and text functions to clean up messy imported data.12 min
  4. Lookup FunctionsLearn XLOOKUP, VLOOKUP and INDEX with MATCH so a spreadsheet can fetch prices, names and rates for you.11 min
  5. Tables, Sorting and FilteringArrange data in clean rows, turn it into an Excel table, then sort and filter it without scrambling your records.10 min

In the news