# How to look up a value using the first few characters of a code

Cut the code down with LEFT and look that up: `=XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown")`.

**Excel:** `=XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown")` (Excel 2021, 2024 and Microsoft 365)

**Google Sheets:** the same formula works.

## Example

Sample data (row 1 is the header):

| A | B | C | D | E | F |
| --- | --- | --- | --- | --- | --- |
| ZIP | Region |  |  | Prefix | Region |
| 02134 |  |  |  | 021 | Boston |
| 60614 |  |  |  | 100 | New York |
| 94107 |  |  |  | 303 | Atlanta |
| 10001 |  |  |  | 606 | Chicago |
| 30303 |  |  |  | 941 | San Francisco |
| 98101 |  |  |  | 981 | Seattle |
| 60601 |  |  |  |  |  |

Formula in B2, filled to B8: `=XLOOKUP(LEFT(A2,3),$E$2:$E$7,$F$2:$F$7,"Unknown")`

Result: Boston; Chicago; San Francisco; New York; Atlanta; Seattle; Chicago

## 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.

## Other ways

- `=VLOOKUP(LEFT(A2,3),$E$2:$F$7,2,FALSE)`: 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")`: 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)`.

## 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.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/lookup-by-first-characters
