View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Preventing people from viewing specific worksheets

Dim sLast As Object


Private Sub Workbook_Open()
'Ensure Sheet1 is not the active sheet upon opening.
If Sheet1.Name = ActiveSheet.Name Then Sheet3.Select
End Sub


Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim strPass As String

Dim lCount As Long


If Sh.CodeName < "Sheet1" and _
sh.CodeName < "Sheet2" Then
Set sLast = Sh
Elseif sh.CodeName = "Sheet1" Then
'Hide Columns
Sheet1.Columns.Hidden = True
'Allow 3 attempts at password
For lCount = 1 To 3
strPass = InputBox( _
Prompt:="Insert Password", _
Title:="PASSWORD REQUIRED")
If strPass = vbNullString Then 'Cancelled
sLast.Select
Exit Sub
ElseIf strPass < "Secret" Then 'InCorrect password
MsgBox "Password incorrect", vbCritical,"Ozgrid.com"
Else 'Correct Password
Exit For
End If
Next lCount


If lCount = 4 Then 'They use up their 3 attempts
sLast.Select
Exit Sub
Else 'Allow viewing
Sheet1.Columns.Hidden = False
End If
Elseif sh.CodeName = "Sheet2" then
'Hide Columns
Sheet2.Columns.Hidden = True
'Allow 3 attempts at password
For lCount = 1 To 3
strPass = InputBox( _
Prompt:="Insert Password", _
Title:="PASSWORD REQUIRED")
If strPass = vbNullString Then 'Cancelled
sLast.Select
Exit Sub
ElseIf strPass < "Secret" Then 'InCorrect password
MsgBox "Password incorrect", vbCritical,"Ozgrid.com"
Else 'Correct Password
Exit For
End If
Next lCount


If lCount = 4 Then 'They use up their 3 attempts
sLast.Select
Exit Sub
Else 'Allow viewing
Sheet2.Columns.Hidden = False
End If


End If


End Sub

Untested, but if you say you have it working, then this modification should
work.

--
Regards,
Tom Ogilvy

"k1ckn1ck" wrote in message
ups.com...
right i now have it working.

Dim sLast As Object


Private Sub Workbook_Open()
'Ensure Sheet1 is not the active sheet upon opening.
If Sheet1.Name = ActiveSheet.Name Then Sheet3.Select
End Sub


Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim strPass As String

Dim lCount As Long


If Sh.CodeName < "Sheet1" Then
Set sLast = Sh
Else
'Hide Columns
Sheet1.Columns.Hidden = True
'Allow 3 attempts at password
For lCount = 1 To 3
strPass = InputBox(Prompt:="Insert Password",
Title:="PASSWORD REQUIRED")
If strPass = vbNullString Then 'Cancelled
sLast.Select
Exit Sub
ElseIf strPass < dollars "Secret" Then 'InCorrect
password
MsgBox "Password incorrect", vbCritical,
"Ozgrid.com"
Else 'Correct Password
Exit For
End If
Next lCount


If lCount = 4 Then 'They use up their 3 attempts
sLast.Select
Exit Sub
Else 'Allow viewing
Sheet1.Columns.Hidden = False
End If
End If


End Sub

How would i edit this code to password protect sheet1 and sheet2

Many thanks

Nick