VBA code - protect and unprotect a sheet

J

Jeff

Can someone please provide code to protect and unprotect a
sheet. Assume sheet name is "Sam" and password
is "Malone". Thanks.
 
G

Gerrit

Jeff said:
Can someone please provide code to protect and unprotect a
sheet. Assume sheet name is "Sam" and password
is "Malone". Thanks.

Sheets("Sam").Unprotect password:="Malone"
Sheets("Sam").protect password:="Malone"
 
K

Ken Wright

Sub Protect()

Sheets("Sam").Protect Password:="Malone"
End Sub

---------------------------------------------

Sub UnProtect()

Sheets("Sam").UnProtect Password:="Malone"
End Sub

---------------------------------------------

If you just want a routine to toggle it, then the following from JE McGimpsey
will do that:-

Sub ToggleProtect()

Const PW As String = "Malone"

With Sheets("Sam")
If .ProtectContents = False Then
.Protect PW
Else
.UnProtect PW
End If
End With

End Sub
 
Top