Protecting Sheets

  • Thread starter Thread starter PeteD
  • Start date Start date
P

PeteD

Hi,

Is it possible to protect/unprotect a sheet with a
password in VBA?. Example please if any.

Thanks
PeteD
 
Pete,

Here is a sample that I use.
At the start of the code I unprotect the sheets so the
rest of my code can run:

Sheets("Raw Data").Select
ActiveSheet.Unprotect Password:="yourpassword"
Sheets("Output Page").Select
ActiveSheet.Unprotect Password:="yourpassword"

Then I password protect the sheets again:

Sheets("Raw Data").Select
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True, Password:="yourpassword"
Sheets("Output Page").Select
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True, Password:="yourpassword"

Make sure you also password protect the code.

jill
 
Yes you can. Here is sample code. This uses the active
sheet but you can do a select of any sheet also.

Sub ProtectWorksheet()
ActiveSheet.Protect password:="password"
End Sub

Sub UnprotectWorksheet()
ActiveSheet.Unprotect password:="password"
End Sub
 
Thanks Jill

PeteD

-----Original Message-----
Pete,

Here is a sample that I use.
At the start of the code I unprotect the sheets so the
rest of my code can run:

Sheets("Raw Data").Select
ActiveSheet.Unprotect Password:="yourpassword"
Sheets("Output Page").Select
ActiveSheet.Unprotect Password:="yourpassword"

Then I password protect the sheets again:

Sheets("Raw Data").Select
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True, Password:="yourpassword"
Sheets("Output Page").Select
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True, Password:="yourpassword"

Make sure you also password protect the code.

jill
.
 
Thanks Tim.

Pete


-----Original Message-----
Yes you can. Here is sample code. This uses the active
sheet but you can do a select of any sheet also.

Sub ProtectWorksheet()
ActiveSheet.Protect password:="password"
End Sub

Sub UnprotectWorksheet()
ActiveSheet.Unprotect password:="password"
End Sub



.
 
Back
Top