1

I have set up a spreadsheet with several columns, some of which have data validation dropdown lists. I need users to be able to select multiple options from the dropdown list. I have achieved this for one column using the VBA below, however I am struggling to find the right way to apply this to multiple columns. Please could someone help. At the moment I have used 'Target.Column = 9', however I need to apply this to columns 3,4,5,6,9.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 9 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

Thanks in advance

EEW13
  • 15
  • 1
  • 4

1 Answers1

2

You could extend your If

If Target.Column = 9 or Target.Column = 3 etc

but sometimes Select Case is a bit tidier.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Oldvalue As String
Dim Newvalue As String

Application.EnableEvents = True

On Error GoTo Exitsub

Select Case Target.Column
    Case 3, 4, 5, 6, 9
      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 Select

Exitsub:
Application.EnableEvents = True

End Sub
SJR
  • 22,986
  • 6
  • 18
  • 26