How to look up a value using the first few characters of a code
Formula (Excel and Google Sheets)
=XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown")Cut the code down with LEFT and look that up: =XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown").
- Result on the example
- one result per row in
B2, filled down toB8 - Works in
- Excel 2021, 2024 and Microsoft 365
- Google Sheets
- Same formula
See it work
The formula is already in B2, filled down to B8. Change any input and the result updates, or edit the formula to experiment.
How it works
LEFT(A2,3) takes the first three characters, "021" for 02134, and XLOOKUP finds that prefix in the table. LEFT always returns text, and the prefixes are stored as text, so "021" keeps its leading zero and matches.
=XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown")Result"Boston"
A2"02134"The value in A2.
LEFT(A2,3)"021"LEFT: Characters from the start of text.- text
A2"02134" - num_chars
3
- text
$E$2:$E$76 rows × 1 col: "021", "100", "303", "606", …A range: 6 cells in one column.
$F$2:$F$76 rows × 1 col: "Boston", "New York", "Atlanta", "Chicago", …A range: 6 cells in one column.
XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown")"Boston"XLOOKUP: Searches one range and returns the matching item from another.- lookup_value
LEFT(A2,3)"021" - lookup_array
$E$2:$E$76 rows × 1 col: "021", "100", "303", "606", … - return_array
$F$2:$F$76 rows × 1 col: "Boston", "New York", "Atlanta", "Chicago", … - if_not_found
"Unknown"
- lookup_value
In Google Sheets
The same formula works in Google Sheets: =XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown"). We ran it in Google Sheets mode on the same data and it returns the same results.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =VLOOKUP(LEFT(A2,3),$E$2:$F$7,2,FALSE) | Boston, Chicago, San Francisco… | The same lookup in every version; an unknown prefix shows #N/A instead of "Unknown". |
| =XLOOKUP(TRUE,LEFT(A2,LEN($E$2:$E$7))=$E$2:$E$7,$F$2:$F$7,"Unknown") | Boston, Chicago, San Francisco… | Compares each prefix with the same number of characters, so prefixes of different lengths can share one table. |
Common mistakes
- ZIP codes stored as numbers lose their leading zero: LEFT(2134,3) is "213". Use
LEFT(TEXT(A2,"00000"),3). - If the prefix table holds numbers (21, 100), they never equal the text from LEFT. Store prefixes as text, or convert with
--LEFT(A2,3).
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Look up a value using the first few characters of a code
B2, return the region for the ZIP in A2 from the prefix table, or "Unknown". Fill down to B8.Questions
How do I look up by the last characters instead?
Use RIGHT: =XLOOKUP(RIGHT(A2,4),…).
How do I make longer prefixes win over shorter ones?
Use the LEN-based alternative and sort the table so longer prefixes come first; XLOOKUP returns the first match from the top.
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.