PDA

View Full Version : Protect a worksheet at startup with VBA



simora
01-24-2010, 02:45 AM
I am trying to protect a sheet (!) at startup. I've tried doing so using the Private Sub Workbook_Open() to place the code, and also using Private Sub Workbook_BeforeClose . Either way, I keep getting a
Run-time error '9' Subscript out of range error.

I have a button on sheet (1) which brings up an input box for a password. When I try to use

Worksheets(1).Protect
OR
Worksheets("Home-Page").Cells.Locked = True
after the password is resolved, I still get an error.

How can I resolve this?

Thanks

Bob Phillips
01-24-2010, 03:49 AM
This should work ine



Worksheets(1).Protect Password:=""

simora
01-24-2010, 04:33 AM
Thanks:
I used this code to surpress the errors and the sheet does what its supposed to do. I was just too fustrated. I had been fooling with this way too long.



On Error Resume Next

Worksheets("Home-Page").Select
If ActiveSheet.Protect = False Then
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
Application.DisplayAlerts = False
End If