Unprotect doesn't work with VBA code

VIorica000

New Member
Joined
May 2, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I have the following code which that does not allow the user to paste values in a Data Validation List Column.

My problem is that I have columns (without data validation) that have to stay protected,

columns With Data Validation List have no locked option from format cell, but when i enable the protect sheet my VBA code doesn't work, i try everything with worsheet.unprotect password but doesn't work.

Also if I try to copy a cell from a column with Data Validation and paste on another column that has Data Validation the VBA doesn't work.

With unprotected sheet and value which does not come from Data Validation the VBA works!.

I don’t know where I’m wrong, I really need help

thank you very much


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim xValue As String
Dim xCheck1 As String
Dim xCheck2 As String

If Target.Count > 1 Then

Exit Sub
End If
Application.EnableEvents = False
xValue = Target.Value
On Error Resume Next
xCheck1 = Target.Validation.InCellDropdown
On Error GoTo 0
Application.Undo
On Error Resume Next
xCheck2 = Target.Validation.InCellDropdown
On Error GoTo 0
If xCheck1 = xCheck2 Then
Target = xValue
Else
MsgBox "It is not allowed to paste!"
End If
Application.EnableEvents = True

End Sub
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The problem is that VBA Code cannot be executed on protected Workbooks or Worksheets.

Try to add the following line at the beginning of your code:
VBA Code:
Me.Protect UserInterfaceOnly := True
 
Upvote 0
The problem is that VBA Code cannot be executed on protected Workbooks or Worksheets.

Try to add the following line at the beginning of your code:
VBA Code:
Me.Protect UserInterfaceOnly := True
Hi, thanks for the answer,

but it doesn’t work, it allows the paste copy without giving me the alert

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,780
Members
449,049
Latest member
greyangel23

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