Protect Sheet Macro

sitelbanat

Board Regular
Joined
Oct 17, 2005
Messages
152
I recorded a macro to protect 2 sheets with a password, but when I click on the macro to protect the sheet, then go to tools unprotect sheet, it does not promt me for a password. Why is that? This is the macro am useing

Sub Protect_sheet()
'
' Protect_sheet Macro
'
'

'
Sheets("LABOR INPUT").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("BUDGET SHEET").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
try this
Code:
Sheets("LABOR INPUT").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
change "password" to any password you like
 

sitelbanat

Board Regular
Joined
Oct 17, 2005
Messages
152
Hi eric, thank you very much for replying. For unprotect is it?

Code:
Sheets("LABOR INPUT").Protect Password:="password", DrawingObjects:=False, Contents:=False, Scenarios:=False

And what about Protecting the workbook, What would the code me for that?
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
when you protect a sheet you can set some arguments
when unprotecting this is not the case

Sheets("LABOR INPUT").UnProtect Password:="password"
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
it's impossible to protect the workbook within code
to my knowledge there is really no syntax available :(

no I'm kitting :LOL:
simply record a macro to know the syntax
Code:
    ActiveWorkbook.Protect Structure:=True, Windows:=False
    ActiveWorkbook.Unprotect
then click in the word "protect" and hit function key F1
help is right on it's way :->
syntax
expression.Protect(Password, Structure, Windows)
this gets us to something like this
Code:
Const PW As String = "1234"
    ActiveWorkbook.Unprotect Password:=PW
    ActiveWorkbook.Protect Password:=PW, Structure:=True, Windows:=False
 

Watch MrExcel Video

Forum statistics

Threads
1,118,081
Messages
5,570,088
Members
412,310
Latest member
mark884
Top