Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Returning Item Codes Instead of Item Names.

Returning Item Codes Instead of Item Names

by Allen Wyatt
(last updated August 28, 2015)

4

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. Choose Insert | Name | Define. Excel displays the Define Name dialog box. (See Figure 1.)
  4. Figure 1. The Define Name dialog box.

  5. In the Name box, enter a descriptive name, such as ItemNames.
  6. Click OK to add the name and close the dialog box.
  7. Select cell B2 (the cell where you want your validation list).
  8. Choose Validation from the Data menu. Excel displays the Data Validation dialog box. (See Figure 2.)
  9. Figure 2. The Data Validation dialog box.

  10. Using the Allow drop-down list, choose List.
  11. In the Source box, enter an equal sign followed by the name you defined in step 4 (such as =ItemNames).
  12. 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(ItemNames,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 creating an array formula:

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

As with all array formulas, you enter this one by pressing Ctrl+Shift+Enter. The biggest drawback to it 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 (12077) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Tracked Changes Notification when Opening

If you have Word configured to show markup on-screen and you look through a document, it is easy to tell where changes have ...

Discover More

Non-Tiled Background Pictures

Background images for a worksheet are automatically repeated over and over (tiled) by Excel. This tip describes a workaround ...

Discover More

Viewing Same Cells on Different Worksheets

When switching from one worksheet to another, you might want to view the same portion of the new worksheet that you were ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Calculating Future Workdays

Need to calculate the date that is a certain number of workdays in the future? You can do so using a couple of different ...

Discover More

Making VLOOKUP Case Sensitive

The VLOOKUP function, like other lookup functions in Excel, is not case sensitive. In other words, it doesn't matter whether ...

Discover More

Using the COLUMN Function

Need to know the column number for use in a formula? The worksheet function you want is the COLUMN function, described in ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 six more than 8?

2012-09-02 13:44:41

Michael Avidan - MVP

@ DaveOnorato,

I can only assume you didn't read Vilnis Vesmas post and what she was aiming to.

I'm a great fun of short and simple formulas but if - by any chance - the source list of the DV could be expanded - the Dynamic way is the only way to avoid a Superfluous and unpleasant "white" space at the end of the DV.

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2012-09-02 12:20:30

DaveOnorato

a dynamic range name is cool, but naming the entire list simplifies the formula.
Name the list fullList, then the formula becomes simpler.
=VLOOKUP(B2,fullList,2,FALSE)


2012-09-01 14:04:43

Michael Avidan - MVP

@ Vilnis Vesma,

As far as I know - naming an entire(!) Column, forces the DV cell to show some blanks at the end of the list.
(Checked in "Excel 2010")

To my opinion, the appropriate way is to make the Named Range it REALLY Dynamic.

If your list starts at cell N1 - the use of:

=OFFSET($N$1,,,COUNTA($N:$N))

ensures that the DV cell will show only the typed items.

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2012-09-01 07:21:17

Vilnis Vesma

In step 2 you selected only the item names as a named range. This makes it unnecessarily difficult to add new items to the list, and it is no good including some 'spare' rows in your named range because these will turn up as blanks in the drop-down list. My original suggestion was to name the entire column. If you do this, Excel only shows the non-blank items in the range and you can safely add more whenever you want.

Your step 1 is not as robust as my suggestion (list in a separate worksheet) because in your model, if you modify your working sheet by inserting or deleting rows you could corrupt the data table.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.