View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
NickHK[_3_] NickHK[_3_] is offline
external usenet poster
 
Posts: 415
Default Use VBA to Apply various Security Levels in Workbooks

Tom,
Do you think those passwords would stand up to a dictionary attack ? <g

NickHK

"Tom Ogilvy" ...
Private Sub Workbook_Open() ' in the thisworkbook module
Dim sh as worksheet, pword as String
pword = inputBox("Enter Password or click Cancel")
if pword = "" then
for each sh in thisworkbook.worksheets
sh.Visible = True
sh.Activate
sh.Protect Password:="StrongLikeABull"
next
elseif lcase(pword) = "huggybear" then
thisworkbook.Worksheets("Sheet 1").Visible = xlSheetvisible
for each sh in thisworkbook.Worksheets
if sh.Name < "Sheet 1" then
sh.Visible = xlSheetHidden
end if
Next
With thisworkbook.Worksheets("Sheet 1")
if .ProtectContents then
.Unprotect "StrongLikeABull"
end if
end With
elseif lcase(pword) = "toiletpaper" then
thisworkbook.Worksheets("Sheet 2").Visible = xlSheetvisible
for each sh in thisworkbook.Worksheets
if sh.Name < "Sheet 2" then
sh.Visible = xlSheetHidden
end if
Next
With thisworkbook.Worksheets("Sheet 2")
if .ProtectContents then
.Unprotect "StrongLikeABull"
end if
end With
elseif lcase(pword) = "triciayoung" then
thisworkbook.Worksheets("Sheet 3").Visible = xlSheetvisible
for each sh in thisworkbook.Worksheets
if sh.Name < "Sheet 3" then
sh.Visible = xlSheetHidden
end if
Next
With thisworkbook.Worksheets("Sheet 3")
if .ProtectContents then
.Unprotect "StrongLikeABull"
end if
end With
End if
End Sub

You can play with that.

http://www.cpearson.com/excel/events.htm
for an overview of events (Chip Pearson's site)

--
Regards,
Tom Ogilvy


"Tricia Young" wrote:

I have a workbook with three worksheets. i would like to protect the
worksheets from editting. But it's complicated.

upon opening there should be an option to type in one of three passwords.
1st PW will open the workbook and only show Sheet 1 - with no
restrictions
2nd PW will open the workbook and only show Sheet 2 - with no
restrictions
3rd PW will open the workbook and only show Sheet 3 - with no
restrictions
No PW will open the workbook and show all Sheets - with restrictions;
users can on view the info and use AutoFilter.

How can I do this with VBA code?

Thanks