Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Returning Item Codes Instead of Item Names.

Returning Item Codes Instead of Item Names

Written by Allen Wyatt (last updated April 25, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Alan can use data validation to create a drop-down list of valid choices for a cell. However, what he actually needs is more complex. He has a large number of item names with associated item codes. In cell B2 he can create a data validation list that shows all the item names (agitator, motor, pump, tank, etc.). The user can then choose one of these. When he references cell B2 elsewhere, however, he wants the item code—not the item name—returned by the reference. Thus, the reference would return A, M, P, TK, etc. instead of agitator, motor, pump, tank, etc.

There is no direct way to do this in Excel. The reason is because data validation lists are set up to include only a single-dimensional list of items. This makes it easy for the list to contain your item names. However, you can expand how you use the data validation list a bit to get what you want. Follow these steps:

  1. Someplace to the right of your worksheet data, set up a data table. This table will contain your item names and, to the right of each item name, the item code associated with that name.
  2. Select the cells that contain your item names. (Don't select the item codes, just the names.)
  3. Display the Formulas tab of the ribbon.
  4. Click the Define Name tool in the Defined Names group. Excel displays the New Name dialog box. (See Figure 1.)
  5. Figure 1. The New Name dialog box.

  6. In the Name box, enter a descriptive name, such as ItemNames. (Note that there can be no spaces in the name.)
  7. Click OK to add the name and close the dialog box.
  8. Select cell B2 (the cell where you want your validation list).
  9. Display the Data tab of the ribbon.
  10. Click the Data Validation tool in the Data Tools group. Excel displays the Data Validation dialog box. (See Figure 2.)
  11. Figure 2. The Data Validation dialog box.

  12. Using the Allow drop-down list, choose List.
  13. In the Source box, enter an equal sign followed by the name you defined in step 5 (such as =ItemNames).
  14. Click OK.

With these steps done, people can still use the data validation drop-down list to select valid item names. What you now need to do is reference the item code from the data table you set up in step 1. You can do that with a formula such as this:

=VLOOKUP(B2,OFFSET(Itemlist,0,0,,2),2,FALSE)

This formula can be used on its own (to put the desired item code into a cell) or it could be used within a larger formula, anyplace you would have originally referenced B2.

If, for some reason, you cannot create a data table for your item names and codes, you could approach the problem by entering your names and codes directly into a formula. Either of the following will work:

=CHOOSE(MATCH(B2,{"agitator","motor","pump","tank"}),"A","M","P","TK")
=INDEX({"A","M","P","TK"},MATCH(B2,{"agitator","motor","pump","tank"},0))

These formulas work just fine in Excel 2021 or the Excel in Microsoft 365. In other versions of Excel you'll need to enter it as an array formula by pressing Ctrl+Shift+Enter.

The biggest drawback to the in-a-formula approach is that it can quickly become unwieldy to keep the formula updated and there is a "viability limit" on how many pairs of codes and items you can include in the formula. (The limit is defined by formula length, so it depends on the length of your item names.) Also, this approach is good to only return the item code in another cell, rather than including it as part of a larger formula.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12078) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Returning Item Codes Instead of Item Names.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Formatting a Cover Page

Formal reports look better when they are set up with an introductory cover page. Here's how you can add a cover page in a ...

Discover More

Controlling the Plotting of Empty Cells

When creating a chart from information that contains empty cells, you can direct Excel how it should proceed. This tip ...

Discover More

Setting Default Options for Track Changes

The Track changes feature in Word is a great help in editing documents, particularly if you are working with others or ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Outstanding Macro Function Reference

Looking for a great reference that you can use to help figure out the various worksheet functions available in Excel? ...

Discover More

Strange ATAN Results

You may use Excel's trigonometric functions to do some quick calculations, and suddenly notice that the results in your ...

Discover More

Ways to Concatenate Values

Users of the most recent versions of Excel have four different ways available to combine values into strings. Even those ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2021-01-25 03:29:46

miha

Hi
can validation be used for insure that no small values like in 4or 5th digits ,0.00045, will entered
and it will became rounded to 0.000 before calculation ?


2017-06-26 11:20:52

Gregg

I do not understand why the Offset function is used here. The standard vlookup using the table works just fine. Furthermore, I recommend staying away from volatile functions unless absolutely necessary.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.