Enable copy paste in protected worksheet

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Enable copy paste in protected worksheet

Post by YasserKhalil »

Hello everyone
I have a code in worksheet event like that

Code: Select all

Private Sub Worksheet_Activate()
    With ActiveSheet
        .Unprotect SPASS
        .Range("C2:C4").Locked = False
        .Range("A8:A" & Rows.Count).Locked = False
        .Range("H8:H" & Rows.Count).Locked = False
        .Protect SPASS
    End With
End Sub
The code enables the user to input values in specific ranges as illustrated in the code. But when the user copy some data from another worksheet to paste in range A8 for example, the paste command is not enabled at all.
When I comment out the previous code, everything is OK. How can I enable the paste and keep the copied contents into the clipboard after the previous code.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Enable copy paste in protected worksheet

Post by HansV »

You unlock the same cells each time the sheet is activated. Why is that necessary?
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Enable copy paste in protected worksheet

Post by YasserKhalil »

It is like reset as the user sometimes corrupt the unlocked cells.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Enable copy paste in protected worksheet

Post by HansV »

All the actions in the code clear the clipboard.
I'd create a macro in a standard module to unlock the cells, and make it available via a custom keyboard shortcut and/or a Quick Access Toolbar button.

(You can use Windows API code to avoid clearing the clipboard, but it doesn't preserve the full clipboard contents)
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Enable copy paste in protected worksheet

Post by YasserKhalil »

Thanks a lot my tutor. I did that as you recommended