Home > Validation > Drop Downs > Multiple Selection Excel Data Validation - Select Multiple ItemsInstead of limiting an Excel data validation drop down list to a single selection, you can use this macro to allow multiple selections. Video and written instructions, and get the free workbook to see how it works. |
Multiple Item Selection ArticlesThis is the main page for the data validation multiple selection technique. I have also written several blog articles, and you can join in the discussion in the comments there:
|
Video Multiple Item Selection ExamplesTo see how this technique works, and a few multiple selection examples, you can watch this short video. |
Select Items With Drop Down ListTo make data entry easier, you can add an Excel data validation drop down list to your worksheet. Then, click the arrow, and select an item to fill the cell. In the worksheet shown below, you can select any month from the drop down list, and it overwrites any previous entry in the cell.
|
Delete Code LinesOr, if you don't want to limit the code to a specific column, you could delete the two rows that are marked with a red dot. When those lines are removed, the code will run when any cell with a drop down list is selected.
|
Video: Multiple Item Selection SetupTo see the steps for setting up data validation with multiple selection, watch this short video tutorial |
Allow Editing With Multiple SelectionsWith this technique, you can select multiple items from a drop down list, but if you try to edit the cell, you might see a data validation error alert, or find that the VBA code simply added your revised text, to the existing text -- probably not the result that you wanted!
Edit Multiple Selection Drop Down ListIn the sample file, on the SameCellEdit worksheet, there is an "Edit Entries" check box. Check that box, and you can edit the cell, without any problems. To edit a cell with multiple items selected:
Return to Multiple SelectionAfter you finish editing the cell(s), press Enter, to complete the editing. Then, remove the check mark from the Edit Entries check box.
|
Add an Editing Check BoxTo add a check box on your worksheet:
|
The Revised Multiple Selection VBA CodeIn the sample file, you can see the code that allows editing when the Edit Entries box is checked. In the screen shot shown below, the EditMode range is set as a variable (rngEdit), and the code looks at that cell's value.
|
Allow New Items in List - Add SortOn the SameCellAddSort sheet in the sample file, there is an example where you can add new items to the drop down lists, but only in specific columns (C and D). The new items are added to the Lists worksheet, and then the list is sorted A-Z.
To see this example, and how the Excel VBA code works, watch this short video tutorial Video Timeline
|
No Duplicates in CellOn the SameCellNoDups sheet, there is an example where you are not allowed to select the same item twice in any of the cell.
On the SameCellNoDupsCol sheet, duplicate selections are allowed in cells in columns B and D, but no duplicates can be entered in the cells in column C. |
Multiple Selection Sample CodeThe following code samples should be copied onto the worksheet module where you want to use the multiple selection technique. This code adds multiple items in the same cell, separated by a comma. There are many more code examples in the sample workbook that you can download below. NOTE: This code does not work if the sheet is protected. Private Sub Worksheet_Change(ByVal Target As Range) ' Developed by Contextures Inc. ' www.contextures.com Dim rngDV As Range Dim oldVal As String Dim newVal As String If Target.Count > 1 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 Target.Column = 3 Then If oldVal = "" Then 'do nothing Else If newVal = "" Then 'do nothing Else Target.Value = oldVal _ & ", " & newVal ' NOTE: you can use a line break, ' instead of a comma ' Target.Value = oldVal _ ' & Chr(10) & newVal End If End If End If End If exitHandler: Application.EnableEvents = True End Sub |
Code on Protected WorksheetIf the worksheet is protected, the Cells.SpecialCells(xlCellTypeAllValidation) code will not work. Instead, you can check the .Validation.Type property of the target cell, to see if it contains a drop down list. This example is on the SameCellProtect sheet in the sample file. It is set up to work on column 3 only. NOTE: The Premium Version also works on protected worksheets. Private Sub Worksheet_Change(ByVal Target As Range) ' Developed by Contextures Inc. ' www.contextures.com 'code runs on protected sheet Dim oldVal As String Dim newVal As String Dim strSep As String Dim strType As Long 'add comma and space between items strSep = ", " If Target.Count > 1 Then GoTo exitHandler 'checks validation type of target cell 'type 3 is a drop down list On Error Resume Next strType = Target.Validation.Type If Target.Column = 3 And strType = 3 Then Application.EnableEvents = False newVal = Target.Value Application.Undo oldVal = Target.Value If oldVal = "" Or newVal = "" Then Target.Value = newVal Else Target.Value = oldVal _ & strSep & newVal End If End If exitHandler: Application.EnableEvents = True End Sub |
Filter with Slicer (Macro)In this example, there are multiple weekday names selected in the WorkDays column. Above the table, click on a Slicer, to filter the WorkDays column for any rows that contain the selected weekday name. In the sample file, code runs automatically, to allow multiple selection, and and to filter the WorkDays column. You can get this sample file in the Download section, below, and there are notes in the Excel file, on how this technique is set up. |
Download the Sample Data Validation FileMultiple Selection Example: To experiment with the multiple selection technique, you can download the zipped sample file: Select Multiple Items from Excel Data Validation List. If you copy the code into a different workbook, remember to check the code for sheet names, range names, row numbers and column numbers, and adjust those if necessary. Filter With Slicer: To test the Filter with Slicer technique, you can download the zipped sample file: Filter Data in Multi-Select Cells. The zipped Excel file contains macros, so be sure to enable macros if you want to test the code. |
Last updated: July 10, 2023 4:18 PM