VBA Disable Paste on Cells with Data Validation

chasingunicorns

New Member
Joined
Feb 4, 2019
Messages
2
Hello,

I have this macro that basically checks whether the cells have data validation and whether the pasted value is one of the value in the validation list. My problem arises when the sheet is locked, a '1004' run time error pops up that states that the command cannot run because the sheet is locked. I have put a unprotect and protect command but the error still shows up. When debugging, the error is on the bold and underlined line. Any help or suggestion on why this is happening and how to solve it would be appreciated.

here is ithe code:

Private Sub worksheet_change(ByVal target As Range)
'
'

Dim Result As Variant
Dim UndoList As String
Dim Validate As Range
Dim Selection As Range
Dim InvalidMsg As String

Set Validate = Cells.SpecialCells(xlCellTypeAllValidation)

InvalidMsg = "Value is invalid. Please choose from the dropdown list."

For Each Selection In target.Cells

If Application.CommandBars("Standard").Controls("&Undo").Enabled = True Then
UndoList = Application.CommandBars("standard").Controls("&Undo").List(1)
If Not Intersect(Selection, Range("DatavalidationCat")) Is Nothing Then
If Left(UndoList, 5) = "Paste" Then
Result = Application.Match(Selection, Range("ValidationlistCat"), 0)
On Error Resume Next
If Result = False Then
MsgBox InvalidMsg
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
Else
If Intersect(Validate, Selection) Is Nothing Then
ActiveSheet.Unprotect "Password"
With target.Validation
.Add Type:=xlValidateList, Formula1:="=ValidationlistCat"
ActiveSheet.Protect "Password", True, True
End With
Exit Sub
End If
End If
End If
End If
End If

Next

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You need to put the unprotect line at the beginning of the sub & the protect line a the end.
I would also recommend you change the name of your variable "Selection" as that is a VBA keyword & could cause problems.
 
Upvote 0
would this cause my excel to slow down as it needs to run the locking and unlocking of the sheet everytime something is changed in the sheet?
and thanks for the tip on the "Suggestion" variable
 
Upvote 0
It will, yes.
Another option, untested is
Code:
Private Sub worksheet_change(ByVal target As Range)
'
'

Dim Result As Variant
Dim UndoList As String
Dim Validate As Range
Dim Rng As Range
Dim InvalidMsg As String


InvalidMsg = "Value is invalid. Please choose from the dropdown list."

For Each Rng In target.Cells
   
   If Application.CommandBars("Standard").Controls("&Undo").Enabled = True Then
      UndoList = Application.CommandBars("standard").Controls("&Undo").List(1)
      If Not Intersect(Rng, Range("DatavalidationCat")) Is Nothing Then
         If Left(UndoList, 5) = "Paste" Then
            Result = Application.match(Rng, Range("ValidationlistCat"), 0)
            On Error Resume Next
            If Result = False Then
               MsgBox InvalidMsg
               Application.EnableEvents = False
               Application.Undo
               Application.EnableEvents = True
            Else
               ActiveSheet.Unprotect "Password"
               Set Validate = Cells.SpecialCells(xlCellTypeAllValidation)
               If Intersect(Validate, Rng) Is Nothing Then
                  With target.Validation
                     .Add Type:=xlValidateList, Formula1:="=ValidationlistCat"
                  End With
                  Exit Sub
               End If
               ActiveSheet.Protect "Password", True, True
            End If
         End If
      End If
   End If

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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