Home > Validation > Drop Downs > Macro
|
Select a product name from a data validation drop down list, and a macro automatically changes the product name to a product code. See how to set this up, and get the sample file for testing. |

To help you select the correct product when ordering, this data validation drop down list shows the product name, followed by its product code.

However, the order department only needs the product code, so a macro automatically updates the cell, after you've selected an item. Instead of showing all the product info, only the code is left in the cell.
This video shows the steps for setting up the drop down list, and you can get the sample file in the download section below. The full transcript for the video is at the end of the page.
On the Codes sheet, there is a list with 3 columns:
Here is the formula in cell C2: = B2 & " -- " & A2
NOTE: The ProductShow column will be used in the drop down list, so people can see both the product name and code.

The list was formatted as a named Excel table, by following these steps:
TIP: Sort the list by Product name, so products will be easy to find in the drop down list.

Next, I created a named range based on the ProductShow column, by following these steps.
NOTE: Because it's based on a formatted table, the ProdList named range is dynamic -- it will automatically grow or shrink, if you add or remove products.
On the DataEntry sheet, there are drop down lists in the Product column. Here are the steps to set those up:

Now we have our drop-down list, showing the descriptive name and the code.

To test the drop down list:

There's code on DataEntry sheet's code module, that runs automatically, if you enter a value in column B (column #2)
The code is shown in the next section, and to see the code in the sample file:
Here are the steps that the code goes through:
Here is the Excel VBA code that is on the DataEntry sheet's code module.
Private Sub Worksheet_Change _
(ByVal Target As Range)
' www.contextures.com
On Error GoTo errHandler
Dim wsCodes As Worksheet
Set wsCodes = Worksheets("Codes")
If Target.Cells.Count > 1 _
Then GoTo exitHandler
If Target.Column = 2 Then
If Target.Value = "" _
Then GoTo exitHandler
Application.EnableEvents = False
Target.Value = wsCodes.Range("A1") _
.Offset(Application. _
WorksheetFunction _
.Match(Target.Value, _
wsCodes.Range("ProdList"), 0), 0)
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
errHandler:
If Err.Number = 13 _
Or Err.Number = 1004 Then
GoTo exitHandler
Else
Resume Next
End If
End Sub
Here is the full transcript for the video at the top of this page.
On this worksheet we want users to be able to select a product from a drop-down list and then enter a quantity.
On another sheet, we have the product list. @e have codes, and then a descriptive name. In another column, we've used the ampersand operator to combine the product name and the code.
We'd like to use this full description in the drop-down list, so people can see the name that will mean more to them than the code, but we'll also see the code.
When the list is entered, we're going to have to send this to the accounting department, and they're only interested in the ID number so they can get all the pricing information.
So on this sheet, after something's selected here, we're going to run some code that will automatically change this full description into the matching ID number.
The first step is to create a table from this range of cells.
The next step is to create a named range based on this ProductShow column.
This list is dynamic -- it will grow or shrink, as we add or remove products.
On the DataEntry sheet, I'll select all the cells where I want the drop downs.
Now we have our drop-down list, showing the descriptive name and the code.
There's code on this worksheet, if you right-click, and click View Code, here's what's happening.
To see how it works, we'll select bookshelf, and the code should become 89307.
As soon as I click on that, the code runs and it changes that full description into just the product code.
Don't miss my monthly Excel newsletter! You'll get quick tips, article links, and a bit of fun. Add your email, and click Subscribe.
Next, when you get my reply, click the Confirm button. I add this step to protect you from spam!
Data Validation With Combo Box
Last updated: May 27, 2025 6:42 PM