Data Validation Show Codes Only

excelbytes

Active Member
Joined
Dec 11, 2014
Messages
291
Office Version
  1. 365
Platform
  1. Windows
I have the following data:

Screenshot 2024-09-23 135131.png

I want to create a drop down list with the items in column "C", but when an item is selected, I only want it to show the 2 letter code as shown in column "A". I found this code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("A2:A30")) Is Nothing Then Exit Sub   'Addresses of the cells with the dropdown

    'Turn off events to keep out of loops
    Application.EnableEvents = False


     'Change the "C1:D10" in the next line to the address of your list
    Target.Value = Application.VLookup(Target.Value, Range("A2:B8"), 2, False)

    'Turn events back on to get ready for the next change
    Application.EnableEvents = True
End Sub

But I can't get it to work. The dropdown list is on a worksheet called 'Tools" and the codes and descriptions are on a worksheet called "Data".

Thanks,
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I have the following data:

View attachment 117206

I want to create a drop down list with the items in column "C", but when an item is selected, I only want it to show the 2 letter code as shown in column "A". I found this code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("A2:A30")) Is Nothing Then Exit Sub   'Addresses of the cells with the dropdown

    'Turn off events to keep out of loops
    Application.EnableEvents = False


     'Change the "C1:D10" in the next line to the address of your list
    Target.Value = Application.VLookup(Target.Value, Range("A2:B8"), 2, False)

    'Turn events back on to get ready for the next change
    Application.EnableEvents = True
End Sub

But I can't get it to work. The dropdown list is on a worksheet called 'Tools" and the codes and descriptions are on a worksheet called "Data".

Thanks,
The range that you are looking up on is on the Data worksheet and this line looks up on the same sheet.

Target.Value = Application.VLookup(Target.Value, Range("A2:B8"), 2, False)

Does this work:

Target.Value = Application.VLookup(Target.Value, Data!Range("A2:B8"), 2, False)

I've never used this technique. I would have a Userform showing when a cell in Range("A2:A30") is selected.
 
Upvote 0
I think you just need to include a reference to the Data sheet for the lookup.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("A2:A30")) Is Nothing Then Exit Sub   'Addresses of the cells with the dropdown

   'Turn off events to keep out of loops
    Application.EnableEvents = False
    On Error GoTo ReEnable

     'Change the "C1:D10" in the next line to the address of your list
    Target.Value = Application.VLookup(Target.Value, Sheets("Data").Range("A2:B8"), 2, False)

ReEnable:
    'Turn events back on to get ready for the next change
    Application.EnableEvents = True

End Sub
 
Upvote 0
No, that didn't work. Since I use the list from column "C", when I make a selection, it only shows that item (e.g. AA Part1) rather than just AA.
 
Last edited:
Upvote 0
No, that didn't work. Since I use the list from column "C", when I make a selection, it only shows that item (e.g. AA Part1) rather than just AA.
You said that 'the codes and descriptions are on a worksheet called "Data".'

Can you use XL2BB to submit a Mini Sheet from Tools and the Data sheet?

Is it always the case that the code that you need is the first two characters of what is selected?
 
Upvote 0
DV Combo Test.xlsm
A
1Drop Down List
2AAPart1
3
4
5AAPart1
6
7
8
9
10
Tools
Cells with Data Validation
CellAllowCriteria
A2:A10List=Data!$C$2:$C$8


DV Combo Test.xlsm
ABC
1CodeDescriptionDrop Down
2AAPart1AAPart1
3BBPart2BBPart2
4CCPart3CCPart3
5DDPart4DDPart4
6EEPart5EEPart5
7FFPart6FFPart6
8GGPart7GGPart7
Data
Cell Formulas
RangeFormula
C2:C8C2=A2&B2
 
Upvote 0
DV Combo Test.xlsm
A
1Drop Down List
2AAPart1
3
4
5AAPart1
6
7
8
9
10
Tools
Cells with Data Validation
CellAllowCriteria
A2:A10List=Data!$C$2:$C$8


DV Combo Test.xlsm
ABC
1CodeDescriptionDrop Down
2AAPart1AAPart1
3BBPart2BBPart2
4CCPart3CCPart3
5DDPart4DDPart4
6EEPart5EEPart5
7FFPart6FFPart6
8GGPart7GGPart7
Data
Cell Formulas
RangeFormula
C2:C8C2=A2&B2
VLOOKUP can only search for a value in the leftmost column of a table and retrieve a corresponding value from a column to its right.

Data Validation Show Codes Only.xlsm
A
1Drop Down List
2DD
3EE
4EE
5FF
6CC
7GG
8DD
9FF
10
11
12
Tools
Cells with Data Validation
CellAllowCriteria
A2:A12List=Data!$A$2:$A$8


Data Validation Show Codes Only.xlsm
ABC
1Drop DownCodeDescription
2AAPart1AAPart1
3BBPart2BBPart2
4CCPart3CCPart3
5DDPart4DDPart4
6EEPart5EEPart5
7FFPart6FFPart6
8GGPart7GGPart7
Data
Cell Formulas
RangeFormula
A2:A8A2=B2&C2



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngLookup As Range

    ' Addresses of the cells with the dropdown.
    If Intersect(Target, Range("A2:A30")) Is Nothing Then
      Exit Sub
    End If
    
    Set rngLookup = Worksheets("Data").Range("C2:E8")

    ' Turn off events to keep out of loops.
    Application.EnableEvents = False
    
    ' Lookup the code of the item selected.
    Target.Value = Application.VLookup(Target.Value, rngLookup, 2, False)
  
    ' Turn events back on to get ready for the next change.
    Application.EnableEvents = True

End Sub
 
Upvote 0
I guess I had a senior moment! I obviously knew that, but had a lapse! It works correctly.

A couple things. Now it shows #N/A when nothing is selected. Can that be addressed?

I noticed that if I change "Worksheet_Change" in "Private Sub Worksheet_Change(ByVal Target As Range)" to something else it doesn't work, is that right?

This was a test workbook to figure out this process. When I apply this to the main workbook, it doesn't work, although I've gone over it a dozen times. I save the code directly to the worksheet with the drop down lists. Can it be in a Module instead? I actually have three different drop down lists on the same worksheet, can they all have "Private Sub Worksheet_Change(ByVal Target As Range)"?

Can there be more than one on a worksheet?

I also have three check boxes on that same worksheet. Could that affect the operation?
 
Upvote 0
I guess I had a senior moment! I obviously knew that, but had a lapse! It works correctly.

A couple things. Now it shows #N/A when nothing is selected. Can that be addressed?

I noticed that if I change "Worksheet_Change" in "Private Sub Worksheet_Change(ByVal Target As Range)" to something else it doesn't work, is that right?

This was a test workbook to figure out this process. When I apply this to the main workbook, it doesn't work, although I've gone over it a dozen times. I save the code directly to the worksheet with the drop down lists. Can it be in a Module instead? I actually have three different drop down lists on the same worksheet, can they all have "Private Sub Worksheet_Change(ByVal Target As Range)"?

Can there be more than one on a worksheet?

I also have three check boxes on that same worksheet. Could that affect the operation?
There is only one Private Sub Worksheet_Change(ByVal Target As Range) event handing procedure for each worksheet and the
code needs to be in the worksheet code module.

Regarding: Now it shows #N/A when nothing is selected. Can that be addressed?
Yes, Code below NOW considers this.

Regarding: I noticed that if I change "Worksheet_Change" in "Private Sub Worksheet_Change(ByVal Target As Range)" to something else it doesn't work, is that right?
There is no need to change this and if you do it won't work.

Regarding: I actually have three different drop down lists on the same worksheet, can they all have "Private Sub Worksheet_Change(ByVal Target As Range)"?
The Worksheet_Change event handler has to handle all changes to any cell on the worksheet if you want to handle changes.
Do you need code to respond each time an option from any of the validation lists is selected? If you do then you need to let us know the details as you have for this one.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngLookup As Range

    ' Addresses of the cells with the dropdown.
    If Not Intersect(Target, Range("A2:A30")) Is Nothing Then
        
      If Len(Trim(Target.Value)) = "" Then
        Exit Sub
      End If
      
      Set rngLookup = Worksheets("Data").Range("A2:C8")
  
      ' Turn off events to keep out of loops.
      Application.EnableEvents = False
      
      ' Lookup the code of the item selected.
      Target.Value = Application.VLookup(Target.Value, rngLookup, 2, False)
    
      ' Turn events back on to get ready for the next change.
      Application.EnableEvents = True

    End If

End Sub

Data worksheet needs to be set out like this,

Data Validation Show Codes Only.xlsm
ABC
1Drop DownCodeDescription
2AAPart1AAPart1
3BBPart2BBPart2
4CCPart3CCPart3
5DDPart4DDPart4
6EEPart5EEPart5
7FFPart6FFPart6
8GGPart7GGPart7
Data
Cell Formulas
RangeFormula
A2:A8A2=B2&C2
 
Upvote 0
Solution
HighAndWilder - I got it to work, figured out what my issues were. Thank you so much for your help.

The last question I have is - on my main worksheet I actually have 3 different requirements for this process. When I duplicate the code and just change it for the appropriate ranges, I get an
Ambiguous Name Detected: Worksheet Change. But when I change it to something else, it doesn't work. Am I not allowed to have more than one on a worksheet? Is there another name I can use to allow this to work?

Thanks again for all your help.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top