VBA to unprotect all sheets run my code and then protect.

pete4monc

Board Regular
Joined
Jan 12, 2014
Messages
85
Office Version
  1. 365
Platform
  1. Windows
Hi guys

I'm hoping someone can help me, I've got some code which runs perfect (from button click) but I need to password protect the sheet so no one fiddles oops I mean changes anything.
Problem is when the sheets are protected the vba doesn't run? VBA on one sheet will populate cells on another.

I have done this vba for one sheet and it works
VBA Code:
Sheets("CustomerService").Unprotect "Pete"
MY CODE HERE
Sheets("CustomerService").Protect "Pete"

So I modified it to run on all sheet names and it fails? Any ideas?
VBA Code:
Sheets("PCD" , "Sales", "PCDPlanning",  "CustomerService1").Unprotect "Pete"
MY CODE HERE
Sheets("PCD" & "Sales" & "PCDPlanning" & "CustomerService1").Protect "Pete"

Thanks in advance guys.
 
Delete your 2 lines of code that protect/unprotect and try:
VBA Code:
With ws
    .Unprotect Password:="Pete"
    Range("A" & x & ":G" & x).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    .Protect Password:="Pete"
End With
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Delete your 2 lines of code that protect/unprotect and try:
VBA Code:
With ws
    .Unprotect Password:="Pete"
    Range("A" & x & ":G" & x).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    .Protect Password:="Pete"
End With

Thanks Mumps, that worked a treat!!

Thanks to everyone who tried to help. @mumps @dmt32 @mikerickson
 
Upvote 0
Hi,
I used this code for protecting of all Worksheets, but now if I tried to click on the button in Tools for Unprotect of sheet, then the sheet was unprotected without password. If I click on the button for unprotecting of Workbook, then the system wants password. Thanks

1670401515886.png
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,307
Members
449,151
Latest member
JOOJ

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