Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy paste with vba protection on

Posted on 2012-03-10
15
Medium Priority
?
393 Views
Last Modified: 2012-03-11
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
0
Comment
Question by:rsen1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 37705853
You might get some ideas here.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37707654
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
0
 

Author Comment

by:rsen1
ID: 37708214
Dave, Thank  you for your response, please see the attached sample from my workbook.

Robert
protect.xlsm
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 42

Expert Comment

by:dlmille
ID: 37708217
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
0
 

Author Comment

by:rsen1
ID: 37708223
Dave could you please send the correct code for that

Thank you
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37708225
do you want to do it for all sheets on open?

Dave
0
 

Author Comment

by:rsen1
ID: 37708227
all but 2 sheets
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37708229
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
0
 

Author Comment

by:rsen1
ID: 37708234
1-20 Labels
Blank Labels
0
 

Author Comment

by:rsen1
ID: 37708251
Thank you very, very much
0
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37708255
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
0
 

Author Comment

by:rsen1
ID: 37708256
I don't see your post so that I can accept it
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37708261
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
0
 

Author Comment

by:rsen1
ID: 37708272
Dave thank you that code works great.

Should I repost, I also have some pages with password protect and EnableAutoFilter = True
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37708274
Sure
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question