Combining multiple worksheet change events

SP321

New Member
Joined
Jan 27, 2023
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I found (maybe on this website) this code which works perfectly on its own. I need to block users in shared excel to copy paste values that arent in the drop down menu.
For one column it works just fine but I need to prevent this in multiple columns and the data is different for each one. I tried removing Exit Subs and continuing with the next change, but I am a complete beginner, I must be doing something wrong.

The Range and Values change eg:

Set isect = Intersect(Range("O9:O30"), Target)
dd = Array("BLUE", "RED", "")

Set isect = Intersect(Range("N9:N30"), Target)
dd = Array("BLACK", "YELLOW", "GRAY", "")

Set isect = Intersect(Range("M9:M30"), Target)
dd = Array("GOLD", "GREEN","PINK", "WHITE", "")

This is just an example, I cant post the real data. Here is the code:




Private Sub Worksheet_Change(ByVal Target As Range)

Dim isect As Range
Dim cell As Range
Dim dd As Variant
Dim i As Long
Dim mtch As Boolean
Dim msg As String
Dim myEntries As String

Set isect = Intersect(Range("O9:O30"), Target)

If isect Is Nothing Then Exit Sub

Application.EnableEvents = False

dd = Array("BLUE", "RED", "")

For Each cell In isect

mtch = False
For i = LBound(dd) To UBound(dd)
If cell.Value = dd(i) Then
mtch = True
Exit For
End If
Next i

If mtch = False Then
cell.ClearContents
msg = msg & cell.Address(0, 0) & ","
End If
Next cell


For i = LBound(dd) To UBound(dd)
myEntries = myEntries & dd(i) & ","
Next i
myEntries = Left(myEntries, Len(myEntries) - 1)


With Range("O9:O30").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=myEntries
' .IgnoreBlank = True
' .InCellDropdown = True
' .InputTitle = ""
' .ErrorTitle = ""
' .InputMessage = ""
' .ErrorMessage = ""
' .ShowInput = True
' .ShowError = True
End With

If Len(msg) > 0 Then
MsgBox "Invalid data in cells: " & vbCrLf & Left(msg, Len(msg) - 1), vbOKOnly, "Error"
End If

Application.EnableEvents = True

End Sub



I appreciate any help, thank you very much!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi all,

I found (maybe on this website) this code which works perfectly on its own. I need to block users in shared excel to copy paste values that arent in the drop down menu.
For one column it works just fine but I need to prevent this in multiple columns and the data is different for each one. I tried removing Exit Subs and continuing with the next change, but I am a complete beginner, I must be doing something wrong.

The Range and Values change eg:

Set isect = Intersect(Range("O9:O30"), Target)
dd = Array("BLUE", "RED", "")

Set isect = Intersect(Range("N9:N30"), Target)
dd = Array("BLACK", "YELLOW", "GRAY", "")

Set isect = Intersect(Range("M9:M30"), Target)
dd = Array("GOLD", "GREEN","PINK", "WHITE", "")

This is just an example, I cant post the real data. Here is the code:




Private Sub Worksheet_Change(ByVal Target As Range)

Dim isect As Range
Dim cell As Range
Dim dd As Variant
Dim i As Long
Dim mtch As Boolean
Dim msg As String
Dim myEntries As String

Set isect = Intersect(Range("O9:O30"), Target)

If isect Is Nothing Then Exit Sub

Application.EnableEvents = False

dd = Array("BLUE", "RED", "")

For Each cell In isect

mtch = False
For i = LBound(dd) To UBound(dd)
If cell.Value = dd(i) Then
mtch = True
Exit For
End If
Next i

If mtch = False Then
cell.ClearContents
msg = msg & cell.Address(0, 0) & ","
End If
Next cell


For i = LBound(dd) To UBound(dd)
myEntries = myEntries & dd(i) & ","
Next i
myEntries = Left(myEntries, Len(myEntries) - 1)


With Range("O9:O30").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=myEntries
' .IgnoreBlank = True
' .InCellDropdown = True
' .InputTitle = ""
' .ErrorTitle = ""
' .InputMessage = ""
' .ErrorMessage = ""
' .ShowInput = True
' .ShowError = True
End With

If Len(msg) > 0 Then
MsgBox "Invalid data in cells: " & vbCrLf & Left(msg, Len(msg) - 1), vbOKOnly, "Error"
End If

Application.EnableEvents = True

End Sub



I appreciate any help, thank you very much!
found the solution in here: 2 Private Sub Worksheet_Change(ByVal Target As Range) ON SAME SHEET
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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