VBA code for multiple selections within a drop-down list - Suggestions please

tanya16

New Member
Joined
Jun 25, 2015
Messages
1
Hi all, I'm new to the forum so apologies beforehand for any errors while posting this.

I'm also very new to VBA coding, but looked around and eventually put together the below code from various sources, with some flaws though. My requirements with the coding are:

1) Select multiple items within an Excel validation (drop-down) list
2) There are several validation lists on my worksheet but I want to allow multiple selection for one column only. Hence I've added the line If Target.Column = 16 Then , where '16' is the column number I'm applying the code to.
3) Separate multiple items within a cell using commas
4) Don't allow duplicates within a cell e.g. if the list is a range of colours, users should be able to select as many colours from the list as they like, but never select the same colour twice. Note that this limitation is on the cell level only. When user proceeds to the next cell, they can again select any/all colours
5) Allow cell content to be erased / cells to be left blank.

Bonus: Show an error message if user selects a duplicate item within a cell




:biggrin:Good news: With the code below I've been able to achieve points 1-4
:(Bad news: I'm not being able to leave the cells blank or erase content.
(To confirm, when creating the data validation list, I have made sure to tick the 'Ignore Blank Cell' check box).

Is anyone able to help me incorporate point 5 into the below code please?
If I can also get an error message to pop up, that's a bonus. But I can live without it!

Many thanks!



Private Sub Worksheet_Change(ByVal Target As Range)
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 = 16 Then
If InStr(1, oldVal, newVal, 1) > 0 Then
Target.Value = oldVal 'reinsert old value, user selected same value twice
Else
If oldVal = "" Then
Else
If newVal = "" Then
Else
Target.Value = oldVal & ", " & newVal
End If
End If
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top