Restricting Paste on multiple columns with different Data Validations

LostinVA

New Member
Joined
May 23, 2018
Messages
43
Hi everyone. I have a worksheet that includes roughly 20/25 different columns. Five of those columns contain Data Validation drop-downs to different data lists. I'm having an issue with users of this worksheet copy/pasting incorrect data over the top of the columns with Data Validation vs. utilizing the drop-downs. Is there a way to restrict or 'lock' the columns with Data Validation so the user is not able to paste and instead has to select a drop-down value?

Hope that made sense. Appreciate any help you can provide!

Regards,
LinVA
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This should prevent pasting, auto filling and drag & Dropping on cells with Data Validation:

Place code in the Worksheet Module:
Code:
Option Explicit


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call MonitorDataValidationRange
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
    Call MonitorCellChange(Target:=Target, NotiFyUser:=True)
End Sub


Private Sub MonitorCellChange(ByVal Target As Range, Optional NotiFyUser As Boolean = True)

    Dim sUndoList As String
    
    On Error Resume Next
    If Not Intersect(Target, Range(CommandBars.FindControl(ID:=128).Tag)) Is Nothing Then
        sUndoList = CommandBars.FindControl(ID:=128).List(1)
        If Left(sUndoList, 5) = "Paste" Or sUndoList = "Auto Fill" Or sUndoList = "Drag and Drop" Then
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
            If NotiFyUser Then MsgBox "'" & sUndoList & "' On DV cells is not allowed", vbCritical
        End If
    End If

End Sub


Private Sub MonitorDataValidationRange()

    On Error GoTo errHandler
    
    Application.EnableEvents = False
    
        With CommandBars.FindControl(ID:=128)
            If .Tag = "" Then
                .Tag = Cells.SpecialCells(xlCellTypeAllValidation).Address
            End If
            
            If Cells.SpecialCells(xlCellTypeAllValidation).Address <> .Tag Then
                .Tag = ""
            End If
        End With
    
errHandler:
    Application.EnableEvents = True

End Sub
 
Upvote 0
This should prevent pasting, auto filling and drag & Dropping on cells with Data Validation:

Place code in the Worksheet Module:
Code:
Option Explicit


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call MonitorDataValidationRange
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
    Call MonitorCellChange(Target:=Target, NotiFyUser:=True)
End Sub


Private Sub MonitorCellChange(ByVal Target As Range, Optional NotiFyUser As Boolean = True)

    Dim sUndoList As String
    
    On Error Resume Next
    If Not Intersect(Target, Range(CommandBars.FindControl(ID:=128).Tag)) Is Nothing Then
        sUndoList = CommandBars.FindControl(ID:=128).List(1)
        If Left(sUndoList, 5) = "Paste" Or sUndoList = "Auto Fill" Or sUndoList = "Drag and Drop" Then
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
            If NotiFyUser Then MsgBox "'" & sUndoList & "' On DV cells is not allowed", vbCritical
        End If
    End If

End Sub


Private Sub MonitorDataValidationRange()

    On Error GoTo errHandler
    
    Application.EnableEvents = False
    
        With CommandBars.FindControl(ID:=128)
            If .Tag = "" Then
                .Tag = Cells.SpecialCells(xlCellTypeAllValidation).Address
            End If
            
            If Cells.SpecialCells(xlCellTypeAllValidation).Address <> .Tag Then
                .Tag = ""
            End If
        End With
    
errHandler:
    Application.EnableEvents = True

End Sub


Thank you so much for your help! I'm receiving a 'Compile error: Sub or Function not defined' when trying to click anywhere within the Worksheet?
 
Upvote 0
On which line does the error occur ? And do you have any other code in the worksheet module ?

Regards.
 
Upvote 0
On which line does the error occur ? And do you have any other code in the worksheet module ?

Regards.

Hi there.. It's giving an error from the below. The 'Private Sub ...' row is highlighted yellow and the 'Call MonitorDataValidationRange' line is highlighted as an error.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call MonitorDataValidationRange
End Sub
 
Upvote 0
That is very strange!

The compile error you are getting means that the sub called 'MonitorDataValidationRange' doesn't exist which is obviously not the case.

Here is a workbook example

Cells in green are the cells with Data Validation.
 
Last edited:
Upvote 0
Have you copied the entire code in Post#2 ... I suspect you mistankenly only copied the top part of the code.
 
Upvote 0
You were absolutely correct! I neglected to copy the code at the bottom. It works! Thank you thank you! I really appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,224,742
Messages
6,180,685
Members
452,993
Latest member
FDARYABEE

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