Find a course

Module 3: Logic, Lookups and Clean DataLesson 7 of 1212 min

XLOOKUP, VLOOKUP and Fixing Errors

Pull matching values from another table with XLOOKUP and VLOOKUP, understand exact matches, and read and fix errors like #N/A, #REF! and #DIV/0!.

Step 1 of 10Learn

The two-table problem

Jamal has an orders sheet listing a product code in each row, such as P-104, and a separate price list tab with codes in column A and prices in column C. For each order he needs the price. Copying prices by hand from one tab to the other is slow and goes wrong the moment a price changes.

This is a lookup: find a value in one list and return the matching value from another column. It's one of the most useful skills in spreadsheets.

Google Sheets has XLOOKUP:

=XLOOKUP(search_key, lookup_range, result_range, [missing_value])

In plain words: find this, in this column, and give me the value from that column. For Jamal:

=XLOOKUP(B2, Prices!A2:A200, Prices!C2:C200, "Not found")

  • B2 is the product code on the order.
  • Prices!A2:A200 is the column of codes on the Prices tab. (Sheet name, exclamation mark, range.)
  • Prices!C2:C200 is the column of prices to return.
  • "Not found" is shown if the code doesn't exist, instead of an error.

XLOOKUP looks for an exact match by default, which is what you want for codes, emails and IDs. The lookup and result ranges must be the same size. And because they're separate arguments, the result column can be to the left or right of the lookup column.

When copying this formula down, lock the ranges with dollar signs (Prices!$A$2:$A$200) so they don't shift.

XLOOKUP across two tabs: the code P-104 on the Orders tab is found in column A of the Prices tab, and the price from column C on the same row is returned (illustrative prices)Orders tabBC12CodePriceP-10412.50Prices tab (illustrative)ABC1234CodeNamePriceP-101Mug8.00P-104Lamp12.50P-107Vase19.001 find2 return the same row=XLOOKUP(B2, Prices!A2:A200, Prices!C2:C200, "Not found")

Step 2 of 10Practice · Choose one

Which formula returns the email address (column D) for the employee ID in G2, where IDs are in column A of the same sheet, rows 2–500?

Step 3 of 10Learn

VLOOKUP: the older cousin

You'll meet VLOOKUP in many existing spreadsheets, so it's worth knowing how to read it:

=VLOOKUP(search_key, range, index, is_sorted)

  • search_key: the value to find.
  • range: a block of columns. VLOOKUP always searches the first (leftmost) column of this block.
  • index: which column of the block to return, counting from 1.
  • is_sorted: use FALSE for an exact match.

Jamal's lookup with VLOOKUP would be =VLOOKUP(B2, Prices!A2:C200, 3, FALSE): find B2 in column A of the block and return the 3rd column, C.

Two traps make VLOOKUP error-prone:

  1. Forgetting FALSE. If the last argument is missing or TRUE, VLOOKUP assumes the first column is sorted and returns the closest match below the value. For product codes, that silently returns a wrong price instead of an error, the most dangerous kind of mistake.
  2. The fixed column number. If someone inserts a new column into the price list, the price moves to column 4, but the formula still says 3 and now returns the wrong field.

VLOOKUP also can't look to the left: the return column must be to the right of the search column. XLOOKUP avoids all three problems, so prefer it for new work, and use VLOOKUP when you must share files with people on very old spreadsheet software.

VLOOKUP searches only the first column of its block and returns the column at the index number: in A2:C200, column A is 1, B is 2 and C is 3ABC123CodeP-101P-104NameMugLampPrice8.0012.50index 1index 2index 3searched herereturned=VLOOKUP(B2, Prices!A2:C200, 3, FALSE)

Step 4 of 10Practice · True or false

VLOOKUP(B2, A2:C100, 3) without a fourth argument always looks for an exact match.

Step 5 of 10Compare

XLOOKUP vs. VLOOKUP

AspectXLOOKUPVLOOKUP
Default matchExactApproximate unless the last argument is FALSE
Where the result can beAny column, left or rightOnly to the right of the search column
Someone inserts a columnStill correct: ranges adjustFixed index may now return the wrong field
Value not foundOptional built-in fallback text#N/A unless wrapped in IFERROR
When to useNew work in Google SheetsReading older files, sharing with very old software

Step 6 of 10Learn

Reading error messages

Errors look alarming, but each one tells you what went wrong:

ErrorMeaningTypical cause
#N/AValue not availableA lookup could not find the search key
#DIV/0!Division by zeroDividing by an empty or zero cell
#REF!Invalid referenceThe formula points to a deleted cell, row or column
#NAME?Unknown nameA misspelled function (=SUMM) or text missing quotes
#VALUE!Wrong type of valueDoing math with text, such as ="ten"*2
#ERROR!Formula can't be parsedMissing parenthesis or a stray character

Hover over the cell, or click it, to see a short explanation from Sheets.

#N/A from a lookup usually means the value really isn't there, or it's there but different: an extra space ("P-104 "), a number stored as text, or a different spelling. The TRIM function, which you'll meet in the next lesson, removes stray spaces.

When an error is expected and harmless, such as a lookup for a code that hasn't been added yet, IFERROR shows something friendlier:

=IFERROR(B2/C2, 0) or =IFERROR(VLOOKUP(B2, A:C, 3, FALSE), "Check code")

Use IFERROR carefully. It hides every error, including real mistakes like a misspelled function or a deleted range. Fix the cause first; add IFERROR only for errors you understand and expect.

Step 7 of 10Practice · Match the pairs

Match each error with its most likely cause.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. #N/A
  2. #DIV/0!
  3. #REF!
  4. #NAME?
  5. #VALUE!

Step 8 of 10Note

Step 9 of 10Practice · What would you do?

What would you do?

Your XLOOKUP for customer ID 10452 returns #N/A, but you can see 10452 in the customer list. The list was pasted in from another system, and its IDs sit on the left side of their cells while your IDs sit on the right. What is the most likely cause?

Step 10 of 10Practice · Choose one

Which VLOOKUP returns the value from the 4th column of A2:E300 for an exact match of the name in H2?