Map Text Codes to Numbers in Excel with a Lookup
Question details
Return a numeric value based on a text code entered in a spreadsheet cell.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants to maintain a list of text codes and their corresponding values, potentially from 1 through 5.
- Observed behavior
- A lookup formula returns the number associated with the entered code or a message when no match is found.
Choose an input cell and two empty columns for the lookup table. Check that each code has the intended numeric value and that the formula ranges cover all table rows.
Use XLOOKUP with a Two-Column Code Table
Use this method in Microsoft 365 to return a value from an easy-to-update lookup table.
Enter codes in H2:H20 and their corresponding numbers in I2:I20. For example, enter TL and UA with 1, WW with 2, and UI with 3.
Type a code such as TL in cell B2.
Select the result cell, such as C2, and enter =XLOOKUP(B2,$H$2:$H$20,$I$2:$I$20,"No match"). Press Enter.
Drag the fill handle from C2 down to calculate results for codes in column B. Add or change mappings in columns H and I as needed.

Use VLOOKUP in Older Excel Versions
Use this alternative when XLOOKUP is unavailable in your version of Excel.
Map Text Codes to Numbers in WPS Spreadsheets
Create the same two-column lookup table in WPS Spreadsheets and use VLOOKUP with IFERROR to return a number for each code.
- 1. Open your workbook: Open WPS Spreadsheets and select Open to choose your workbook.
- 2. Create the mapping table: Enter codes in H2:H20 and their corresponding numbers in I2:I20.
- 3. Enter a lookup formula: Select C2 and enter =IFERROR(VLOOKUP(B2,$H$2:$I$20,2,FALSE),"No match"). Press Enter.
- 4. Copy and save: Drag the fill handle from C2 down for other input rows, then select Save to keep your changes.

Frequently Asked Questions
How do I return a message when a code is not in the table?
In XLOOKUP, use the fourth argument, as in =XLOOKUP(B2,$H$2:$H$20,$I$2:$I$20,"No match"). With VLOOKUP, wrap the formula in IFERROR to display the message.
Why does VLOOKUP return the wrong value?
Check that the codes are in the first column of H2:I20, the numbers are in the second column, and the formula ends with FALSE for an exact match.
Can several codes return the same number?
Yes. Enter each code on its own row in column H and repeat the appropriate number in column I.
What should I do when I add more codes below row 20?
Extend the formula ranges to include the new rows. For example, change $H$2:$I$20 to $H$2:$I$30 in the VLOOKUP formula.




