We help IT Professionals succeed at work.

Copy paste with vba protection on

Hi Guys, I have a worksheet with the protection code below

ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True

It will not allow me to copy paste on the sheet.

What if anything can be added to the code to allow copy paste in the unlocked cells.

Thank you,
Robret
Comment
Watch Question

Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You might get some ideas here.
Most Valuable Expert 2012
Top Expert 2012

Commented:
Copy/Paste works fine in the unlocked cells.  Are you sure you're testing your paste in unlocked cells?  unlock the sheet, select the cells and right click then FORMAT->Protection and make sure the Locked checkbox is NOT checked.

See attached, the yellow shaded cells are unlocked.  On workbook open, Sheet1 is protected with your command.

Try copying from cell B4 and paste to another yellow range.  Works, correct?

You can also copy from protected area to this yellow range as well.  Works, correct?

Dave
copyPasteProtected-r1.xls

Author

Commented:
Dave, Thank  you for your response, please see the attached sample from my workbook.

Robert
protect.xlsm
Most Valuable Expert 2012
Top Expert 2012

Commented:
You're not being allowed to copy/paste because of your worksheet_selectionChange() event.  It is setting protection on every change and as a result, the clipboard is being cleared.

Any reason you're not setting protection on workbook open or close as perhaps a better alternative?

Dave

Author

Commented:
Dave could you please send the correct code for that

Thank you
Most Valuable Expert 2012
Top Expert 2012

Commented:
do you want to do it for all sheets on open?

Dave

Author

Commented:
all but 2 sheets
Most Valuable Expert 2012
Top Expert 2012

Commented:
ok - what sheets?  Also, perhaps workbook close is more appropriate as thats when the "owner" of the changes ensures everything is locked down.  No need to do it on open if the sheets are already protected.

Dave

Author

Commented:
1-20 Labels
Blank Labels

Author

Commented:
Thank you very, very much
Most Valuable Expert 2012
Top Expert 2012
Commented:
Here's your code.  Note the constant at the top of the code.  Just include more sheet names to exclude, separated by commas.  The code iterates through and determines the sheet can or can't be excluded, then does the protect operation, accordingly.

Const excludeSheets = "1-20 Labels,Blank Labels" '<- put the sheets to exclude/not protect, here

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim wkb As Workbook
Dim wks As Worksheet
Dim strExcludeSheets As String
Dim vExcludeSheets As Variant
Dim myDict As Object 'Dictionary holds unique names

    vExcludeSheets = Split(excludeSheets, ",")
    
    Set myDict = CreateObject("Scripting.Dictionary")
    For i = LBound(vExcludeSheets) To UBound(vExcludeSheets)
        myDict.Add vExcludeSheets(i), Nothing 'sheet names are unique so no need to test for existance
    Next i
    
    Set wkb = ThisWorkbook
    For Each wks In wkb.Worksheets
        If Not myDict.exists(wks.Name) Then 'didn't find it, so protect the sheet
            wks.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True
        End If
    Next wks

    myDict.RemoveAll
    Set myDict = Nothing
End Sub

Open in new window


See attached.

Dave
protect-r1.xlsm

Author

Commented:
I don't see your post so that I can accept it
Most Valuable Expert 2012
Top Expert 2012

Commented:
Sorry - that code goes in the ThisWorkbook codepage, not the sheet's codepage.  Just copy/paste it in.

Let me know if it works alright for you!

Hope this helps!

Dave

Author

Commented:
Dave thank you that code works great.

Should I repost, I also have some pages with password protect and EnableAutoFilter = True
Most Valuable Expert 2012
Top Expert 2012

Commented:
Sure

Explore More ContentExplore courses, solutions, and other research materials related to this topic.