Forum Discussion
DDBDS
Sep 01, 2021Copper Contributor
Selecting multiple items from a dropdown list and returning different values
I've seen VBA code that will allow one to select multiple items from a drop-down list. I've seen VBA code that allow you to create a drop down list and return a different value. What I need is a co...
- Sep 02, 2021
Find attached and the code below as well.
Private Sub Worksheet_Change(ByVal Target As Range) Dim Oldvalue As String Dim Newvalue As String Dim selectedNum As Variant Application.EnableEvents = True On Error GoTo Exitsub If Target.Column = 3 Then If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then GoTo Exitsub Else: If Target.Value = "" Then GoTo Exitsub Else Application.EnableEvents = False Newvalue = Target.Value selectedNum = Application.VLookup(Newvalue, ActiveSheet.Range("dropdown"), 2, False) Newvalue = selectedNum Application.Undo Oldvalue = Target.Value If Oldvalue = "" Then Target.Value = Newvalue Else If InStr(1, Oldvalue, Newvalue) = 0 Then Target.Value = Oldvalue & ", " & Newvalue Else: Target.Value = Oldvalue End If End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End Sub
DDBDS
Sep 02, 2021Copper Contributor
Added. Thank you.
Juliano-Petrukio
Sep 02, 2021Bronze Contributor
Find attached and the code below as well.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Dim selectedNum As Variant
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 3 Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
selectedNum = Application.VLookup(Newvalue, ActiveSheet.Range("dropdown"), 2, False)
Newvalue = selectedNum
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
- DDBDSSep 02, 2021Copper ContributorMany, many thanks !