How To Create Drop Down List With Multiple Selections Or Values In Excel?


Excel is a powerful spreadsheet tool that is widely used by individuals, businesses, and organizations alike. One of the most useful features of Excel is the ability to create drop-down lists, which can greatly simplify data entry and ensure consistency across different cells or columns. In this tutorial, we will focus on how to create a drop-down list with multiple selections or values in Excel. This feature is particularly useful when you want to allow users to select more than one option from a list of choices. We will walk you through the step-by-step process of creating such a drop-down list, and you will learn how to customize it to suit your specific needs. By the end of this tutorial, you will have a better understanding of how to use Excel's drop-down list feature and be able to apply it to your own spreadsheets.

Create Drop Down List With Multiple Selections Or Values

Here we can complete the task just by inserting the VAB code into the sheet. So let us see a simple process to know how you can create a drop-down list with multiple selections or values in Excel.

Step 1

Consider any Excel sheet where you have a data validation list. First, right-click on the sheet name and select View Code to open the VBA application. Then copy the below-mentioned code into the text box as shown below.

Right click > View code > Copy code.

Code

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim xRng As Range
   Dim xValue1 As String
   Dim xValue2 As String
   If Target.Count > 1 Then Exit Sub
   On Error Resume Next
   Set xRng = Cells.SpecialCells(xlCellTypeAllValidation)
   If xRng Is Nothing Then Exit Sub
   Application.EnableEvents = False
   If Not Application.Intersect(Target, xRng) Is Nothing Then
      xValue2 = Target.Value
      Application.Undo
      xValue1 = Target.Value
      Target.Value = xValue2
      If xValue1 <> "" Then
         If xValue2 <> "" Then
            If xValue1 = xValue2 Or _
               InStr(1, xValue1, ", " & xValue2) Or _
                  InStr(1, xValue1, xValue2 & ",") Then
                  Target.Value = xValue1
               Else
                  Target.Value = xValue1 & ", " & xValue2
               End If
         End If
      End If
   End If
   Application.EnableEvents = True
End Sub

Step 2

From now on, we can select multiple values for a data validation list.

Note −

Use the code below to allow multiple selections in a drop-down list without creating duplicates (you can remove an item by selecting it once more).

Code
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim xRng As Range
   Dim xValue1 As String
   Dim xValue2 As String
   Dim semiColonCnt As Integer
   Dim xType As Integer
   If Target.Count > 1 Then Exit Sub
   On Error Resume Next
    
   xType = 0
   xType = Target.Validation.Type
   If xType = 3 Then
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      xValue2 = Target.Value
      Application.Undo
      xValue1 = Target.Value
      Target.Value = xValue2
      If xValue1 <> "" Then
         If xValue2 <> "" Then
            If xValue1 = xValue2 Or xValue1 = xValue2 & ";" Or xValue1 = xValue2 & "; " Then ' leave the value if only one in list
                  xValue1 = Replace(xValue1, "; ", "")
                  xValue1 = Replace(xValue1, ";", "")
                  Target.Value = xValue1
               ElseIf InStr(1, xValue1, "; " & xValue2) Then
                  xValue1 = Replace(xValue1, xValue2, "") ' removes existing value from the list on repeat selection
                  Target.Value = xValue1
               ElseIf InStr(1, xValue1, xValue2 & ";") Then
                  xValue1 = Replace(xValue1, xValue2, "")
                  Target.Value = xValue1
               Else
                  Target.Value = xValue1 & "; " & xValue2
               End If
               Target.Value = Replace(Target.Value, ";;", ";")
               Target.Value = Replace(Target.Value, "; ;", ";")
               If Target.Value <> "" Then
                  If Right(Target.Value, 2) = "; " Then
                     Target.Value = Left(Target.Value, Len(Target.Value) - 2)
                  End If
               End If
               If InStr(1, Target.Value, "; ") = 1 Then ' check for ; as first character and remove it
                  Target.Value = Replace(Target.Value, "; ", "", 1, 1)
               End If
               If InStr(1, Target.Value, ";") = 1 Then
                  Target.Value = Replace(Target.Value, ";", "", 1, 1)
               End If
               semiColonCnt = 0
               For i = 1 To Len(Target.Value)
                  If InStr(i, Target.Value, ";") Then
                     semiColonCnt = semiColonCnt + 1
                  End If
               Next i
               If semiColonCnt = 1 Then ' remove ; if last character
                  Target.Value = Replace(Target.Value, "; ", "")
                  Target.Value = Replace(Target.Value, ";", "")
               End If
            End If
      End If
      Application.EnableEvents = True
      Application.ScreenUpdating = True
   End If
End Sub

Conclusion

In this tutorial, we have used a simple example to demonstrate how you can create a drop-down list with multiple selections or values in Excel to highlight a particular set of data.

Updated on: 13-Jul-2023

420 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements