Create dropdown list and display different values from another worksheet

Jane_Hogan

New Member
Joined
Sep 20, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have created several dropdown lists on a worksheet which all need to display different values in order to provide input for a name generator cell (using the textjoin formula). I found a VBA code which let's me display different values in a dropdown list ; For example sheet1 shows a dropdown list in column B with the option water which displays as 'WA' and Thee as 'TH' and so on. I would like to apply the same to the dropdown lists in column C to E but I'm unable to solve 2 problems:
1) The VBA code only works with the active sheet (=Sheet1). What code should I use to reference the other sheet (=DropDownValues)? I prefer to keep the dropdown values on a separate sheet so it can be hidden.
2) Sheet1 has several dropdown lists (column B to E) which need to display different values coming from different tables/cells. I tried to use the same VBA code for the other dropdown values on the active sheet but it's not working and I'm seeing a compile error (see screenshots)). How can I apply similar VBA codes in one project/sheet?

Thank you for your help!

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

ProductName = Target.Value

If Target.Column = 2 Then

ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("BUValue"), 2, False)

If Not IsError(ProductCode) Then

Target.Value = ProductCode

End If

End If

End Sub


Thank you for your help!
 

Attachments

  • Screenshot DropdownValues.png
    Screenshot DropdownValues.png
    46.8 KB · Views: 62
  • Screenshot sheet 1.png
    Screenshot sheet 1.png
    63.1 KB · Views: 70
  • Screenshot VBA code.png
    Screenshot VBA code.png
    102.1 KB · Views: 64
  • Screenshot VBA compile error.png
    Screenshot VBA compile error.png
    81.5 KB · Views: 69

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It is because you are using same event trigger for the same sheet more than once.

You can use something like
Select Case Target.Column
Case 2
<code here>
Case 3
,code here.
 
Upvote 0
Hi Zot,

Many thanks for the feedback. I'm still having problems applying this code in the right way. Could you advise if below code is correct or needs to be changed?

And would you also know the answer to my 1st question as well?

Thank you!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ProductName = Target.Value
    If Target.Column = 2 Then
        ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("BUValue"), 2, False)
        If Not IsError(ProductCode) Then
            Target.Value = ProductCode
        End If
    End If
    Select Case Target.Column
    Case 2
    ProductName = Target.Value
    If Target.Column = 3 Then
        ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("LCValue"), 2, False)
        If Not IsError(ProductCode) Then
            Target.Value = ProductCode
        End If
    End If
End Sub
 
Upvote 0
@Jane_Hogan
  1. Can you show us the data validation formula?
  2. What are the tables name in sheet DropDownValues?
  3. Could you please upload a sample workbook (without sensitive data) to a file-sharing site like Dropbox.com or Google Drive, and then share the link here? Also, ensure that the link is accessible to anyone.
 
Upvote 0
Hi Akuina,

1) I had some issues with the XL2BB tool but now it works so please see below the data validation formulas
2) Table names in sheet DropDownValues:
- BU
- Channel
- Phase
- Language
I have the same tables on sheet1 but with different table names:
- BUValue
- ChannelValue
- PhaseValue
- LanguageValue
3) Dropbox link: Example.xlsm

Thank you!

Example.xlsm
ABCDEFGHIJKLMNOPQRS
1
2Business UnitChannelPhaseLanguageNAME
3WAOut of HomeAwarenessGermanWA_Out of Home_Awareness_GermanBusiness UnitValueChannelValuePhaseValueLanguageValue
4THSocial MediaMotivateFrenchTH_Social Media_Motivate_FrenchWaterWAOut of HomeOOHAwarenessAWAEnglishUK
5COSearchPurchaseDutchCO_Search_Purchase_DutchTheeTHSocial MediaSOCMotivateMOTGermanGE
6SOOnline Video AdsConvertDanishSO_Online Video Ads_Convert_DanishCoffeeCOSearchSEAPurchasePURFrenchFR
7ALTelevisionAwarenessSpanishAL_Television_Awareness_SpanishSodasSOOnline Video AdsOLVConvertCONDutchNL
8WAOut of HomeAwarenessItalianWA_Out of Home_Awareness_ItalianAlcoholALTelevisionTLVDanishDK
9THOut of HomeAwarenessGermanTH_Out of Home_Awareness_GermanSpanishSP
10COOut of HomeAwarenessGermanCO_Out of Home_Awareness_GermanPolishPO
11ItalianIT
12
Sheet1
Cell Formulas
RangeFormula
F3:F10F3=TEXTJOIN("_",TRUE,B3:E3)
Cells with Data Validation
CellAllowCriteria
B3:B10List=DropDownValues!$B$3:$B$7
C3:C10List=DropDownValues!$E$3:$E$7
D3:D10List=DropDownValues!$H$3:$H$6
E3:E10List=DropDownValues!$K$3:$K$10


Example.xlsm
ABCDEFGHIJKLM
1
2Business UnitValueChannelValuePhaseValueLanguageValue
3WaterWAOut of HomeOOHAwarenessAWAEnglishUK
4TheeTHSocial MediaSOCMotivateMOTGermanGE
5CoffeeCOSearchSEAPurchasePURFrenchFR
6SodasSOOnline Video AdsOLVConvertCONDutchNL
7AlcoholALTelevisionTLVDanishDK
8SpanishSP
9PolishPO
10ItalianIT
11
DropDownValues


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Created By Excel 10 Tutorial
    ProductName = Target.Value
    If Target.Column = 2 Then
        ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("BUValue"), 2, False)
        If Not IsError(ProductCode) Then
            Target.Value = ProductCode
        End If
    End If
End Sub
 
Upvote 0
Hi Zot,

Many thanks for the feedback. I'm still having problems applying this code in the right way. Could you advise if below code is correct or needs to be changed?

And would you also know the answer to my 1st question as well?

Thank you!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ProductName = Target.Value
    If Target.Column = 2 Then
        ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("BUValue"), 2, False)
        If Not IsError(ProductCode) Then
            Target.Value = ProductCode
        End If
    End If
    Select Case Target.Column
    Case 2
    ProductName = Target.Value
    If Target.Column = 3 Then
        ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("LCValue"), 2, False)
        If Not IsError(ProductCode) Then
            Target.Value = ProductCode
        End If
    End If
End Sub
I'm not able to test your example because I'm using older version of Excel with no TextJoin :). So, I'm just guessing here. This is how you can implement one event trigger for more than one column
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Select Case Target.Column
        Case Is = 2
            ProductName = Target.Value
            ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("BUValue"), 2, False)
            If Not IsError(ProductCode) Then
                Target.Value = ProductCode
            End If
        Case Is = 3
            ProductName = Target.Value
            ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("LCValue"), 2, False)
            If Not IsError(ProductCode) Then
                Target.Value = ProductCode
            End If
    End Select
End Sub

I see that you have yet to define LCValue, thus it will give error.
 
Upvote 0
The table names in sheet DropDownValues are actually Table1, Table2, ...etc.
Try this:
  1. Remove tables in Sheet1.
  2. Create four named ranges that refer to the first column of each table in the sheet 'DropDownValues.' Also, remove any unnecessary named ranges you have created.
  3. Set the data validation formula to refer to the named ranges. Using this method, the data validation list becomes dynamic. For a better understanding, check out this article:
    excel-tables-as-source-for-data-validation-lists

Jane_Hogan - Example.jpg


Jane_Hogan - Example - #1.xlsm
ABCDEF
1
2Business UnitChannelPhaseLanguageNAME
3COOLVMOTGECO_OLV_MOT_GE
4THOOHPURFRTH_OOH_PUR_FR
5ALTLVAL_TLV
Sheet1
Cell Formulas
RangeFormula
F3:F5F3=TEXTJOIN("_",TRUE,B3:E3)
Cells with Data Validation
CellAllowCriteria
B3:B10List=BU
C3:C10List=Channel
D3:D10List=Phase
E3:E10List=Language


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

If Target.Cells.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("B:E")) Is Nothing Then  'if target is in col B:E
        Dim fm, c As Range
        On Error Resume Next
           Set c = Evaluate(Target.Validation.Formula1)     'get the range where the list is located
        On Error GoTo 0
       
        If Not c Is Nothing Then
                fm = Application.Match(Target, c, 0)        'find selected item on the list
                Application.EnableEvents = False
                Target = c.Cells(fm).Offset(, 1)            'replace selected item with coresponding item in the next column
                Application.EnableEvents = True
        End If
    End If
End Sub

Note:
If the data validation are in a fixed range, say B3:E10, then you can change:
If Not Intersect(Target, Range("B:E")) Is Nothing Then
to:
If Not Intersect(Target, Range("B3:E10")) Is Nothing Then
The workbook:
Jane_Hogan - Example - #1
 
Upvote 0
Sorry, the code in post #7 is flawed; it will produce an error if you try to delete value in the cell with data validation.
Replace it with this one:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("B:E")) Is Nothing Then  'if target is in col B:E
        Dim fm, c As Range
        If Len(Target) > 0 Then
            On Error Resume Next
               Set c = Evaluate(Target.Validation.Formula1)     'get the range where the list is located
            On Error GoTo 0
           
            If Not c Is Nothing Then
                    fm = Application.Match(Target, c, 0)        'find selected item on the list
                    Application.EnableEvents = False
                    Target = c.Cells(fm).Offset(, 1)            'replace selected item with coresponding item in the next column
                    Application.EnableEvents = True
            End If
        End If
    End If
End Sub

Jane_Hogan - Example - #2.xlsm
 
Upvote 0
Hi Akuini,

Fantastic and you are a lifesaver! It worked but now I realised one additional issue. I would like to add additional dropdown columns where the value does not have to change and remains the same when selected. With your code, the value shows up as a blank cell value now instead. Is there a way to solve this as well?

I have added the 2 rows (see column D = year and column F = season) in below document/link and added the respective tables on the dropdownvalue sheet. Could you have a look again and advise what needs to be changed to the code to make it work for all dropdown columns even if they do not change value upon selection.

Dropbox link

Many thanks again for your support!
 
Upvote 0
I would like to add additional dropdown columns where the value does not have to change and remains the same when selected.

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

If Target.Cells.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("B:C,E:E,G:G")) Is Nothing Then  'if target is in col B:E
        Dim fm, c As Range
        If Len(Target) > 0 Then
            On Error Resume Next
               Set c = Evaluate(Target.Validation.Formula1)     'get the range where the list is located
            On Error GoTo 0
            
            If Not c Is Nothing Then
                    fm = Application.Match(Target, c, 0)        'find selected item on the list
                    Application.EnableEvents = False
                    Target = c.Cells(fm).Offset(, 1)            'replace selected item with coresponding item in the next column
                    Application.EnableEvents = True
            End If
        End If
    End If
End Sub

Col D & F keep their original value
Jane_Hogan - Example - #2 with column D & F update.xlsm
BCDEFGH
2Business UnitChannelYearPhaseSeasonLanguageNAME
3SOOLV2025MOTHighGESO_OLV_2025_MOT_High_GE
4WAOOH2026PURLowFRWA_OOH_2026_PUR_Low_FR
5ALTLV2024PURMidDKAL_TLV_2024_PUR_Mid_DK
Sheet1
Cell Formulas
RangeFormula
H3:H5H3=TEXTJOIN("_",TRUE,B3:G3)
Cells with Data Validation
CellAllowCriteria
B3:B10List=BU
C3:C10List=Channel
D3:D10List=Year
E3:E10List=Phase
F3:F10List=Season
G3:G10List=Language
 
Upvote 0
Solution

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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