Specific sheet access by user/password

sampson32

Active Member
Joined
Jul 16, 2003
Messages
312
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook with 10 sheets.

Each sheet needs to be protected by a different password.

When a user logs in I would like them to be directed to a specific sheet and all other sheets hidden

I found some references to this on the forum but they had no code that I could try.

How could this be accomplished?

Thank you,

Sam
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
In addition to Iain's link, here's something that might help get you started.
It requires you to add a sheet and name it LogIn.
Make SURE this sheet is at the beginning of your sheets. (ie. the furthest one to the left.)
Next, protect sheet2 with the password Bob's Password
Protect sheet3 with the password John's Password
Lastly, try this code in the Thisworkbook module.
Code:
Private Sub Workbook_Open()
Dim WSCount As Integer
Dim ws As Integer
WSCount = ActiveWorkbook.Worksheets.Count

Sheets("LogIn").Select
For ws = 2 To WSCount
      Sheets(ws).Visible = False
Next ws

StartSequence:
uName = InputBox("Please enter your user name.", "Enter User Name")
pWord = InputBox("Please enter your password.", "Enter Password")

Select Case uName
    Case "Bob"
        If pWord = "Bob's Password" Then
            With Sheets("Sheet2")
                .Visible = True
                .Select
                .Unprotect "Bob's Password"
            End With
          Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
            If Ans = vbNo Then
                Exit Sub
              Else: GoTo StartSequence
            End If
        End If
    
    Case "John"
        If pWord = "John's Password" Then
            With Sheets("Sheet3")
                .Visible = True
                .Select
                .Unprotect "John's Password"
            End With
          Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
            If Ans = vbNo Then
                Exit Sub
              Else: GoTo StartSequence
            End If
        End If
    
    'Continue with the rest of your usernames and their passwords
    
    End Select
End Sub

Save the workbook and close/reopen it
The User name to get to sheet2 is Bob, password = Bob's Password
The User name to get to sheet3 is John, password = John's Password

If you need help tweaking the code to suit your own requirements, by all means post back with the changes you'd like to make.

Hope it helps,
Dan
 
Upvote 0
Half Ace – using your code above… I have it in place for all 10 sheets

Is it possible to add; one user and password that makes all sheets visable?

How would that code be added?

Sam
 
Upvote 0
This is how I added an open all workbooks user/password to HalfAce's supplied code:

*******************************
Private Sub Workbook_Open()
Dim WSCount As Integer
Dim ws As Integer
WSCount = ActiveWorkbook.Worksheets.Count

Sheets("House Account").Select
For ws = 2 To WSCount
Sheets(ws).Visible = False
Next ws

StartSequence:
uName = InputBox("Please enter your user name.", "Enter User Name")
pWord = InputBox("Please enter your password.", "Enter Password")

Select Case uName
Case "central"
If pWord = "sony32" Then
With Sheets("Central")
.Visible = True
.Select
.Unprotect "sony32"
End With
Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
If Ans = vbNo Then
Exit Sub
Else: GoTo StartSequence
End If
End If

Case "north"
If pWord = "zeno84" Then
With Sheets("North")
.Visible = True
.Select
.Unprotect "zeno84"
End With
Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
If Ans = vbNo Then
Exit Sub
Else: GoTo StartSequence
End If
End If

Case "south"
If pWord = "nec100" Then
With Sheets("South")
.Visible = True
.Select
.Unprotect "nec100"
End With
Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
If Ans = vbNo Then
Exit Sub
Else: GoTo StartSequence
End If
End If

Case "west"
If pWord = "brother62" Then
With Sheets("West")
.Visible = True
.Select
.Unprotect "brother62"
End With
Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
If Ans = vbNo Then
Exit Sub
Else: GoTo StartSequence
End If
End If

Case "sunCoast"
If pWord = "victor30" Then
With Sheets("Suncoast")
.Visible = True
.Select
.Unprotect "victor30"
End With
Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
If Ans = vbNo Then
Exit Sub
Else: GoTo StartSequence
End If
End If

Case "all"
If pWord = "openall" Then
Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
If Ans = vbNo Then
Exit Sub
Else: GoTo StartSequence
End If
End If

With Sheets("Central")
.Visible = True
.Select
.Unprotect "sony32"
End With

With Sheets("North")
.Visible = True
.Select
.Unprotect "zeno84"
End With

With Sheets("South")
.Visible = True
.Select
.Unprotect "nec100"
End With

With Sheets("West")
.Visible = True
.Select
.Unprotect "brother62"
End With

With Sheets("SunCoast")
.Visible = True
.Select
.Unprotect "victor30"
End With

End Select

End Sub
**************************************

For my future reference, is their a better way to accomplish the same thing?

Sam
 
Upvote 0
New to VB. Trying to tweak code for my benefit

In addition to Iain's link, here's something that might help get you started.
It requires you to add a sheet and name it LogIn.
Make SURE this sheet is at the beginning of your sheets. (ie. the furthest one to the left.)
Next, protect sheet2 with the password Bob's Password
Protect sheet3 with the password John's Password
Lastly, try this code in the Thisworkbook module.
Code:
Private Sub Workbook_Open()
Dim WSCount As Integer
Dim ws As Integer
WSCount = ActiveWorkbook.Worksheets.Count

Sheets("LogIn").Select
For ws = 2 To WSCount
      Sheets(ws).Visible = False
Next ws

StartSequence:
uName = InputBox("Please enter your user name.", "Enter User Name")
pWord = InputBox("Please enter your password.", "Enter Password")

Select Case uName
    Case "Bob"
        If pWord = "Bob's Password" Then
            With Sheets("Sheet2")
                .Visible = True
                .Select
                .Unprotect "Bob's Password"
            End With
          Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
            If Ans = vbNo Then
                Exit Sub
              Else: GoTo StartSequence
            End If
        End If
    
    Case "John"
        If pWord = "John's Password" Then
            With Sheets("Sheet3")
                .Visible = True
                .Select
                .Unprotect "John's Password"
            End With
          Else: Ans = MsgBox("Incorrect Password. Care to try again?", vbYesNo, "Invalid Password")
            If Ans = vbNo Then
                Exit Sub
              Else: GoTo StartSequence
            End If
        End If
    
    'Continue with the rest of your usernames and their passwords
    
    End Select
End Sub

Save the workbook and close/reopen it
The User name to get to sheet2 is Bob, password = Bob's Password
The User name to get to sheet3 is John, password = John's Password

If you need help tweaking the code to suit your own requirements, by all means post back with the changes you'd like to make.

Hope it helps,
Dan
 
Upvote 0
Welcome to the Board!

So what's your question?

How do you need the code tweaked?

Smitty
 
Upvote 0
sorry that i was not more thorough in my post... did not mean to submit incomplete. anyhoo...

i wanted to test the code on just a couple of sheets. I need to protect 15 tabs

I inserted the new sheet "LogIn" and moved to the beginning. on my assumption of HalfAce instructions I protected two of the sheets via tools -->protection -->protect worksheet. i copied the code into my workbook.

this particular workbook has 19 sheets with the sheet with the testing being done on sheet 1 and sheet4.

After i saved the workbook and reopen it, i am prompt for my user name and password. Even though i the correct uid and pw i get the following error message "Run-time error '9': Subcript out of range. I hit the debug button and i am taken into VB window with the following "With Sheets("Sheet1") highlighted and the message "Sheets("Sheet1") = <Subscript ouf of range>.

fyi - i am using XP pro w/XlC 2002 When i say that i am new to VB... i mean i just started trying to learn this stuff last week. i am a total newbie.

any help is greatly appreciated.
 
Upvote 0
No problem.

Sheets("Sheet1")

Where "Sheet1" refers to the actual sheet name. Just change it to reflect your sheet name.

Smitty
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top