View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
p45cal[_160_] p45cal[_160_] is offline
external usenet poster
 
Posts: 1
Default Code to Lock and Unlock a sheet


Drew;526102 Wrote:
I'm sure that this is very basic code, but I am a newby to VBA.
Currently I
have some code that is running that will update my pivot table when I
click
on the tab where the pivot table resides (Sales). However, when I
protect
this sheet, the code fails and I get an error. Can someone help me
with the
code that I need to unprotect the sheet prior to the code that runs the
pivot
table update and protect the sheet once the code is done running.

Thanks,
--
Drew


There are two things tyou can do:
1. Unprotect the sheet before updating and re-protect after with the
likes of:
Sheets("Sheets1").Unprotect
'update code here
Sheets("Sheets1").Protect
of if passwords are
involved:Sheets("Sheets1").Unprotect "secretpassword"
'update code here
Sheets("Sheets1").Protect "secretpassword"

2.Protect the sheet only as far as the user is concerned, allowing code
to alter it:
Sheets("Sheets1").Protect
UserInterfaceOnly:=Truewith
passwords:Sheets("Sheets1").Protect "secretpassword",
UserInterfaceOnly:=TrueYou can run these lines even if the
sheet is already protected. Be aware that according to the help file,
this last only lasts as long as the file is open. Save and Close the
file, then re-open it and the protection will be full protection,
necessitating the execution of that line (Sheets("Sheets1").Protect
"secretpassword", UserInterfaceOnly:=True) at least once somewhere in
the code to enable code to update the sheet.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=144455