Multiple Items in Data Validation List

boilermaker1997

New Member
Joined
Sep 20, 2007
Messages
41
I found the following code on another website. It allows you to choose multiple items in a data validation list and display them in the same cell. The items are separated by commas. In addition, you can re-select an item and it will be removed from the cell.

I was wondering if someone would look at the code and tell me how to change it so the items are separated by a line break in the cell rather than commas.

Here is the code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lUsed As Long
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
lUsed = InStr(1, oldVal, newVal)
If lUsed > 0 Then
If Right(oldVal, Len(newVal)) = newVal Then
Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 2)
Else
Target.Value = Replace(oldVal, newVal & ", ", "")
End If
Else
Target.Value = oldVal _
& ", " & newVal
End If

End If
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi boilermaker1997,

That's an interesting idea.

Below is some code you can try.....

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngDV As Range
    Dim oldVal As String, newVal As String
    
    On Error GoTo exitHandler
    If Target.Count > 1 Or Target.Text = "" Then Exit Sub
    
    Set rngDV = Intersect(Columns("C"), _
        Target.SpecialCells(xlCellTypeSameValidation))
    If rngDV Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    newVal = Target.Text
    Application.Undo
    oldVal = Target.Text
    Target.Value = newVal
    If oldVal = "" Then GoTo exitHandler
    
    If oldVal = newVal Then
        Target.Value = ""
    ElseIf InStr(1, oldVal, newVal) > 0 Then
        If Right(oldVal, Len(newVal)) = newVal Then
            Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
        Else
            Target.Value = Replace(oldVal, newVal & Chr(10), "")
        End If
    Else
        Target.Value = oldVal & Chr(10) & newVal
    End If
  
exitHandler:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks for your help. I tried your code and unless I'm doing something wrong, it doesn't appear to be doing anything. I am only able to select one item at a time from a drop down list.
 
Upvote 0
Thanks for your help. I tried your code and unless I'm doing something wrong, it doesn't appear to be doing anything. I am only able to select one item at a time from a drop down list.


Disregard my last post. I found what I was doing wrong. Is there a way to may this apply to all columns in the worksheet? Again, thanks for your help.
 
Upvote 0
Disregard my last post. I found what I was doing wrong. Is there a way to may this apply to all columns in the worksheet? Again, thanks for your help.

Just modify these lines....

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngDV As Range
    Dim oldVal As String, newVal As String
    
    On Error GoTo exitHandler
    If Target.Count > 1 Or Target.Text = "" Then Exit Sub
    
[COLOR="Blue"]    If Target.SpecialCells(xlCellTypeSameValidation) _
        Is Nothing Then Exit Sub[/COLOR]
    
    Application.EnableEvents = False
    newVal = Target.Text
    Application.Undo
    oldVal = Target.Text
    Target.Value = newVal
    If oldVal = "" Then GoTo exitHandler
    
    If oldVal = newVal Then
        Target.Value = ""
    ElseIf InStr(1, oldVal, newVal) > 0 Then
        If Right(oldVal, Len(newVal)) = newVal Then
            Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
        Else
            Target.Value = Replace(oldVal, newVal & Chr(10), "")
        End If
    Else
        Target.Value = oldVal & Chr(10) & newVal
    End If
  
exitHandler:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Just modify these lines....

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngDV As Range
    Dim oldVal As String, newVal As String
    
    On Error GoTo exitHandler
    If Target.Count > 1 Or Target.Text = "" Then Exit Sub
    
[COLOR=Blue]    If Target.SpecialCells(xlCellTypeSameValidation) _
        Is Nothing Then Exit Sub[/COLOR]
    
    Application.EnableEvents = False
    newVal = Target.Text
    Application.Undo
    oldVal = Target.Text
    Target.Value = newVal
    If oldVal = "" Then GoTo exitHandler
    
    If oldVal = newVal Then
        Target.Value = ""
    ElseIf InStr(1, oldVal, newVal) > 0 Then
        If Right(oldVal, Len(newVal)) = newVal Then
            Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
        Else
            Target.Value = Replace(oldVal, newVal & Chr(10), "")
        End If
    Else
        Target.Value = oldVal & Chr(10) & newVal
    End If
  
exitHandler:
    Application.EnableEvents = True
End Sub
I used the above code successfully. I would like to apply multiple validation for 3 columns. i.e there are certain columns for which i would like to restrict with only one value and certain columns for which i can have multiple values.
 
Upvote 0
Hi aavins73,

Try using this version. Modify the code in blue font to reference the range in which you want any DV cells to allow multiple items.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As String, newVal As String

    
    On Error GoTo exitHandler

    
    '---Test to see if code should continue.
    '   Test statements could be combined, and are
    '      shown separately to simplify modification


    If Target.Count > 1 Or Target.Text = "" Then Exit Sub

    
    If Target.SpecialCells(xlCellTypeSameValidation) _
        Is Nothing Then Exit Sub

    
    '---modify to any valid range reference on activesheet
    If Intersect(Target, [COLOR="#0000CD"][B]Range("D:F")[/B][/COLOR]) _
        Is Nothing Then Exit Sub

    
    Application.EnableEvents = False
    newVal = Target.Text
    Application.Undo
    oldVal = Target.Text
    Target.Value = newVal
    If oldVal = "" Then GoTo exitHandler

    
    If oldVal = newVal Then
        Target.Value = ""
    ElseIf InStr(1, oldVal, newVal) > 0 Then
        If Right(oldVal, Len(newVal)) = newVal Then
            Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
        Else
            Target.Value = Replace(oldVal, newVal & Chr(10), "")
        End If
    Else
        Target.Value = oldVal & Chr(10) & newVal
    End If

  
exitHandler:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Hello everyone...hoping you can help.

I am truly a VB novice, but was using this same code in Excel 2010 a couple of weeks ago. I originally had it working fine in a test workbook, but had to disable the macros in order to post it on an internal, company forum. Now that I have re-enabled the macros, and re-pasted the code, it does not run at all. However, if I start with a blank workbook, I am able to copy the code and it works fine.

My existing workbook has 20 columns with data validation (I only want to allow multiple values in 3 of them), so I don't want to re-create the entire workbook from scratch. Any ideas on what might be preventing the original, working code from running??

Many thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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