An implementation of Visual Basic that is built into Microsoft products.
Hi @Fernando Castro ,
Thanks for reaching out.
The code below is intended as a reference example, so you may need to adjust the cell ranges, separator, or duplicate-handling logic to fit the exact layout and requirements of your workbook.
You can do this with one Worksheet_Change event. The idea is to tell the code which cells should behave as multi-select drop-downs, then let the same logic run for any of those cells.
Place the code below in the worksheet module where the drop-downs are located. To get there, right-click the sheet tab, choose View Code, and paste it there. It should not go into a regular module.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim DropDownCells As Range
Dim NewValue As String
Dim OldValue As String
On Error GoTo ExitHandler
'Update these ranges to match your drop-down cells
Set DropDownCells = Union(Me.Range("B2:B10"), Me.Range("D2:D10"), Me.Range("F2:F10"))
If Target.CountLarge > 1 Then GoTo ExitHandler
If Intersect(Target, DropDownCells) Is Nothing Then GoTo ExitHandler
Application.EnableEvents = False
NewValue = Target.Value
Application.Undo
OldValue = Target.Value
If OldValue = "" Then
Target.Value = NewValue
ElseIf NewValue = "" Then
Target.Value = ""
Else
If InStr(1, ", " & OldValue & ", ", ", " & NewValue & ", ", vbTextCompare) = 0 Then
Target.Value = OldValue & ", " & NewValue
Else
Target.Value = OldValue
End If
End If
ExitHandler:
Application.EnableEvents = True
End Sub
The part you would adjust is this line:
Set DropDownCells = Union(Me.Range("B2:B10"), Me.Range("D2:D10"), Me.Range("F2:F10"))
For example, if the drop-downs are only in a few specific cells, you could write it like this instead:
Set DropDownCells = Union(Me.Range("B2"), Me.Range("D5"), Me.Range("H3"))
After that, each listed cell will keep its own selections independently. When you pick another item from one of those drop-downs, the code adds it to the existing value in that same cell, separated by a comma. It also avoids adding the same item twice.
Just make sure the workbook is saved as a macro-enabled workbook, such as .xlsm, and that macros are enabled when you open it.
Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.