Share via

Select multiple values from an Excel dropdown list?

Marie Hazelwood 1 Reputation point
2021-03-25T11:16:59.207+00:00

Online search suggested I use below VBA code to enable multiple selections in MS Excel dropdown list, save as Macro-Enabled and it should work. However, it's not working. Can you help?

'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = $G$ Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
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

Private Sub Workbook_Open()

End Sub

Developer technologies | Visual Basic for Applications

1 answer

Sort by: Most helpful
  1. dragon-jg 15 Reputation points
    2023-10-05T07:54:57.3066667+00:00

    Sorry for the late response. I just read your question. Hopefully this works for you:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rngDV As Range
    Dim oldVal As String
    Dim newVal As String
    Dim colLabel As String
    Dim strMatch As String
    Dim nbrRow As Integer
    
    If Target.Count > 1 Then GoTo exitHandler
    
    nbrRow = ActiveCell.Row
    
    If nbrRow = 1 Then GoTo exitHandler
    
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
    If rngDV Is Nothing Then GoTo exitHandler
    
    ' get current column's label, from row 1
    colLabel = Cells(1, Target.Column)
    strMatch = Left(colLabel, 8)
    If strMatch <> "Session " Then GoTo exitHandler
    
    On Error Resume Next
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
    On Error GoTo exitHandler
    
    If rngDV Is Nothing Then GoTo exitHandler
    
    If Intersect(Target, rngDV) Is Nothing Then
       'do nothing
    Else:
      Application.EnableEvents = False
      newVal = Target.Value
      Application.Undo
      oldVal = Target.Value
      Target.Value = newVal
        If oldVal = "" Then
            'do nothing
        Else
          If newVal = "" Then
            'do nothing
          Else
            Target.Value = oldVal _
            & Chr(10) & newVal
          End If
        End If
    End If
    
    exitHandler:
      Application.EnableEvents = True
    End Sub
    
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.