Excel Tutorial menu

Excel VLOOKUP and XLOOKUP

Lookup functions find a value in one table and bring back related information, like finding a product's price from its code.

The Example Table

A price list on a sheet, in A1:C5:

ABC
1CodeProductPrice
2P101Basmati Rice 5kg1850
3P102Cooking Oil 1L560
4P103Tea 900g1650
5P104Sugar 1kg160

On the bill, the cashier types only a code in F2, and Excel finds the product and price.

VLOOKUP

Example

text
=VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])
ArgumentMeaning
lookup_valueWhat to look for, e.g. the code in F2
table_arrayThe table, with the lookup column first
col_index_numWhich column of the table to return (1, 2, 3...)
range_lookupFALSE for an exact match, TRUE for an approximate match

Find the product name and price for the code in F2:

Example

text
=VLOOKUP(F2, $A$2:$C$5, 2, FALSE)
=VLOOKUP(F2, $A$2:$C$5, 3, FALSE)
F2ProductPrice
P103Tea 900g1650

Always use FALSE (exact match) for codes, names and IDs. If you leave it out, Excel uses approximate match and can return a wrong row without any error.

Limits of VLOOKUP

  • It only searches the first column of the table and returns values to its right
  • The column number breaks if someone inserts a new column in the table
  • It returns #N/A when the value is not found

XLOOKUP

XLOOKUP (Excel 2021, Microsoft 365 and Excel for the web) fixes these problems:

Example

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

The same lookups with XLOOKUP:

Example

text
=XLOOKUP(F2, A2:A5, B2:B5)
=XLOOKUP(F2, A2:A5, C2:C5, "Code not found")
F2ProductPrice
P102Cooking Oil 1L560
P999#N/ACode not found

Why XLOOKUP is better:

  • Exact match is the default
  • The return column can be on the left or the right
  • It does not break when columns are inserted
  • It has a built-in "not found" message

It can even look up from right to left, for example finding the code from a product name:

Example

text
=XLOOKUP("Sugar 1kg", B2:B5, A2:A5)
Result
P104

Approximate Match: Grade Tables

Approximate match is useful for ranges, like discount levels. The table must be sorted from smallest to largest:

HI
1From (Rs)Discount
200%
350005%
41000010%

Example

text
=VLOOKUP(B2, $H$2:$I$4, 2, TRUE)
BillDiscount
32000%
75005%
1500010%

Excel finds the largest "From" value that is not bigger than the bill.

INDEX and MATCH

Before XLOOKUP, experienced users combined INDEX and MATCH. It works in every version of Excel:

Example

text
=INDEX(C2:C5, MATCH(F2, A2:A5, 0))
  • MATCH(F2, A2:A5, 0) finds the position of the code (0 means exact match)
  • INDEX(C2:C5, position) returns the price in that position
F2Result
P1011850

Handling Not Found

With VLOOKUP, wrap it in IFERROR:

Example

text
=IFERROR(VLOOKUP(F2, $A$2:$C$5, 3, FALSE), "Not found")

If you get #N/A even though the value looks correct, check for extra spaces (use TRIM) or numbers stored as text.

Exercise

Exercise

In =VLOOKUP(F2, A2:D20, 3, FALSE), what does the 3 mean?

Learn Excel with a teacher

Join the CIT (Computer Information Technology) course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp