Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. 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

by Allen Wyatt
(last updated November 28, 2016)

5

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.
  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(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 (12078) applies to Microsoft Excel 2007 and 2010. 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

Checkboxes in a Merged Document

When creating a mail-merge document, you may want to include some special characters, such as check-marked boxes, in the ...

Discover More

Making Use of Extra Labels

Got extra labels left over after printing a mail merge? Here's what you can do to put those labels to good use.

Discover More

Selecting an Entire Worksheet

While editing, you may need to select everything in a worksheet. Excel provides three easy ways you can accomplish this.

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

MORE EXCELTIPS (RIBBON)

Colors in an IF Function

You can use the IF worksheet function to test for a number of different conditions or values. You can't use it to check for ...

Discover More

Using the WEEKNUM Function

Need to know which week of the year a particular date falls within? Excel provides the WEEKNUM function so you can easily ...

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
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 for this tip:

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 7 + 8?

2015-07-10 11:07:35

Stu Cram

I agree with Brian's suggestion and use that approach frequently. It works well and is straightforward to program, especially the INDEX / MATCH method.

My only suggestion is to store the result of the MATCH in a variable (say n) and then use x in the INDEX formula, especially when you may want to access several corresponding items from many columns.

ex. (Normally refer to a cell for the gymnast's name)
(An actual name is shown here.)

n = MATCH("gymnast_Names,"Bobbie Pinn", FALSE)

category = INDEX("Categories", n)
age = Year(Now()) - INDEX(YearOfBirth", n)
club = INDEX("ClubNames", n)

and so on.

Using named ranges of course is a great idea for many reasons.

-Stu


2013-05-06 07:40:44

Bryan

Your formulas are really weird. I don't understand why the offset is there (and this is a function you want to avoid at all costs, since it's volatile) in the first one, and I don't know why the second one is an array formula.

If your Item Code data is in A1:A100, Item Name data is in B1:B100, and validated dropdown is in C1, try these:

=VLOOKUP(C1,A1:B100,2,FALSE)
=INDEX(A1:A100,MATCH(C1,B1:B100,0))

Alan might think about using Access instead of Excel (an item having a unique item number sounds like a Primary Key to me!), but I understand why he may not need the complexity. Another good solution would be to add an ActiveX dropdown box and populate it through code (this way you can have the item number and item name display in the box, but put just the item number into some cell on the worksheet.


2012-12-27 07:21:44

Bill

Selecting text from end to beginning works for me.


2012-09-01 16:26:00

Michael Avidan - MVP

@ Jack Sons,

Select 'Review' > 'Changes' > 'Accept all changes'.

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2012-09-01 05:58:00

Jack Sons

No comment but a question:
If I copy this article to a Word document I get a vertical line to the left side of the text. What is the most simple (and effective) way to get rid of that line without altering anything of the lay out?


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.

Links and Sharing
Share