Forum Discussion

DDBDS's avatar
DDBDS
Copper Contributor
Sep 01, 2021
Solved

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...
  • Juliano-Petrukio's avatar
    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 

Resources