Excel Drop down list, Multiple Selections

ManUBlueJay

Active Member
Joined
Aug 30, 2012
Messages
302
Office Version
  1. 2016
Platform
  1. Windows
I am trying to run a macro that would allow multiple selections in a excel data validation list.
I would have thought this code would work but it doesnt. It exits the sub before adding in the line with the comma.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("J3:J10000")) Is Nothing Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Seems to work for me however, if there are no cells with validation in the specified range then it does nothing.

I am guessing that the yellow cell below is the intended result:
Book1
J
1
2
3
4look
5took
64, 7, 8
7Hi
8Ukraine for the win
Sheet1
Cells with Data Validation
CellAllowCriteria
J6List1,2,3,4,5,6,7,8,9
 
Upvote 0
It was meant to do nothing if there are no validations, As I am making my target larger for growth
This would be my intended result but for me I get a runtime error on the Application.undo, and because the code exits on error nothing happens
1682067265703.png
 
Upvote 0
I have discovered because I have a Worksheet Selection change action on the same sheet it fails the application.undo.
When I remove the Worksheet Selection change action it works.
Is there any way they can co-exist or is their another way to accomplish what I am trying to do
 
Upvote 0
I solved it by moving the Application.EnableEvents = False to the first line of the code
 
Upvote 0
Sweet, thanks for the update. Glad you got it sorted.
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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