Restricting Paste on different Data Validations columns in the same worksheet

Retrorock

New Member
Joined
Dec 18, 2017
Messages
3
Hi all,

I found this code off
HTML:
https://superuser.com/questions/870926/restrict-paste-into-dropdown-cells-in-excel?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
in order to do what i wanted, which is to restrict people from pasting over and thus deleting my data validation in a shared workbook, however an error comes up if I tried to set the DataValidationRange to include multiple column with different data validation (some validations are list, some are date and so on).

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    'Does the validation range still have validation?
    If HasValidation(Range("DataValidationRange")) Then
        Exit Sub
    Else
        Application.Undo
        MsgBox "Error: You cannot paste data into these cells." & _
        "Please use the drop-down to enter data instead.", vbCritical
    End If
End Sub


Private Function HasValidation(r) As Boolean
    'Returns True if every cell in Range r uses Data Validation
    On Error Resume Next
    x = r.Validation.Type
    If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function

So I tried to set up a separate instant for each column I have (Name column A as DataValidationRange1, Column C with a different data validation as DataValidationRange2) and edited the code as so to include two If scenario, but an error still comes up, not sure why. Can Anyone help advise me on this?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    'Does the validation range still have validation?
    If HasValidation(Range("DataValidationRange1")) Then
        Exit Sub
    Else
        Application.Undo
        MsgBox "Error: You cannot paste data into these cells." & _
        "Please use the drop-down to enter data instead.", vbCritical
    End If

    If HasValidation(Range("DataValidationRange2")) Then
        Exit Sub
    Else
        Application.Undo
        MsgBox "Error: You cannot paste data into these cells." & _
        "Please use the drop-down to enter data instead.", vbCritical
    End If


End Sub


Private Function HasValidation(r) As Boolean
    'Returns True if every cell in Range r uses Data Validation
    On Error Resume Next
    x = r.Validation.Type
    If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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