Password Protecting Individual Sheets

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
So, how would you be deciding who can see what sheet(s)?

Or, I guess more to the point, how would we know who to give access to what sheet(s)?
 
Upvote 0
each employee would get access to the main screen titled summary in this code and then each manager would have access to all sheets, which would be titled their name (Smith, John)
 
Upvote 0
... and then each manager would have access to all sheets, which would be titled their name (Smith, John)
This is unclear to me. Do managers get access to all sheets, or just the Summary and the one with their name?
 
Upvote 0
What I meant was:
  • each staff member would have access to the main sheet "summary" and to their personal sheet "Smith, John" for john smith
  • each Manager would have access to ALL sheets.
i hope that makes better sence...apparently my brain was a little slow the other day.
 
Upvote 0
What I meant was:
  • each staff member would have access to the main sheet "summary" and to their personal sheet "Smith, John" for john smith
  • each Manager would have access to ALL sheets.
i hope that makes better sence...apparently my brain was a little slow the other day.
So, modifying the code in the link both you and lenze provided, what about something like this. In the Workbook, apart from the 'Summary' sheet and the individual sheets (eg 'Smith, John') you also have a 'LogIn' sheet like this that identifies those users with their own 'sheetname' and who is a manager and who is not.

Excel Workbook
ABCD
1UsernameSheet NameManagerPassword
2JimJJones, JimYabc
3JohnSSmith, JohnYdef
4JenJJones, JenN
5KenKKirk, KenYkkk
6BobBBurns, BobN
7
LogIn


Then, in the 'ThisWorkbook' module you have the following code. When the WorkBook is opened, the intent is:

- If the user enters a Username that is not listed in the 'LogIn' sheet they will get access to the 'Summary' sheet only.

- If the user enters a Username that is in the 'LogIn' sheet but is not a manager, they get access to the 'Summary' sheet and the sheet whose name appears in Col B of the 'LogIn' sheet next to their Username.

- If the user enters a Username that is listed as Manager and they
i) enter the correct passwoed as shown in Col D of 'LogIn' then they will have access to all sheets (except 'LogIn')
ii) enter an incorrect password, they will have access to 'Summary' only.

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    If ws.Name <> "Summary" Then ws.Visible = xlSheetVeryHidden
  Next ws
  ThisWorkbook.Save
End Sub

Private Sub Workbook_Open()
  Dim user As String, pwd As String, Correctpwd As String, ShtName As String
  Dim ct As Long, LR As Long
  Dim C As Range
  Dim ws As Worksheet
  Dim Manager As Boolean
  
  LR = Sheets("LogIn").Cells(Rows.Count, "A").End(xlUp).Row
  user = InputBox("Enter your UserName")
  Set C = Worksheets("LogIn").Range("$A1:$A" & LR).Find(What:=user, LookIn:=xlValues, MatchCase:=False, SearchFormat:=False)
  If Not C Is Nothing Then
    Manager = (C.Offset(, 2) = "Y")
    If Manager Then
      Correctpwd = C.Offset(, 3)
      Do While ct < 3 And pwd <> Correctpwd
        pwd = InputBox("Enter Password: " & 3 - ct & " tries left")
        ct = ct + 1
      Loop
      If pwd = Correctpwd Then
        For Each ws In Worksheets
          If ws.Name <> "LogIn" Then
          ws.Visible = True
          End If
        Next ws
      Else
        MsgBox "Incorrect manager password, access to Summary only"
      End If
    Else
      Sheets(C.Offset(, 1).Value).Visible = True
    End If
  Else
    MsgBox "Not a valid user, access to Summary only"
  End If
End Sub
 
Last edited:
Upvote 0
- If the user enters a Username that is not listed in the 'LogIn' sheet they will get access to the 'Summary' sheet only.
this is fine.

- If the user enters a Username that is in the 'LogIn' sheet but is not a manager, they get access to the 'Summary' sheet and the sheet whose name appears in Col B of the 'LogIn' sheet next to their Username.
All users (managers and non-managers) should be required to input both a username and a password.

- If the user enters a Username that is listed as Manager and they
i) enter the correct passwoed as shown in Col D of 'LogIn' then they will have access to all sheets (except 'LogIn') I would need someone to be able to access 'LogIn' sheet to add/remove staffnames from list.
ii) enter an incorrect password, they will have access to 'Summary' only.
if they enter an incorrect username can the workbook close?

Also, is it possible to to have the sheet names be based on the staff names, for example
sheet login:
next to the passwords is a column called DISPLAY NAMES, display names would display "Smith, John" in the Tabs at the bottom for John Smith's sheet? I already have a code that inputs the sheet name into cell A1 of the worksheet, so I don't need that.
I hope this makes sense.
 
Upvote 0
I'm not sure what you mean by the last bit but the sheet names can be anything.

For the first part, try this. Change the LogIn sheet slightly:

Excel Workbook
ABCD
1UsernameSheet NameStatusPassword
2AdminAdminadmin
3JimJManagerabc
4JohnSManagerdef
5JenJJones, JenUserjen
6KenKKirk, KenUserkkk
7BobBBurns, BobUserbob
LogIn



.. and change the Workbook_Open code to:

VBA Code:
Private Sub Workbook_Open()
  Dim user As String, pwd As String, Correctpwd As String, ShtName As String
  Dim ct As Long, LR As Long
  Dim C As Range
  Dim ws As Worksheet
  Dim Status As String
  
  LR = Sheets("LogIn").Cells(Rows.Count, "A").End(xlUp).Row
  user = InputBox("Enter your UserName")
  Set C = Worksheets("LogIn").Range("$A1:$A" & LR).Find(What:=user, _
  LookIn:=xlValues, MatchCase:=False, SearchFormat:=False)
  If Not C Is Nothing Then
    Status = C.Offset(, 2)
    Correctpwd = C.Offset(, 3)
    Do While ct < 3 And pwd <> Correctpwd
      pwd = InputBox("Enter Password: " & 3 - ct & " tries left")
      ct = ct + 1
    Loop
    If pwd = Correctpwd Then
      Application.ScreenUpdating = False
      If Status = "User" Then
        Sheets(C.Offset(, 1).Value).Visible = True
      Else
        For Each ws In Worksheets
          ws.Visible = True
        Next ws
        If Status <> "Admin" Then
          Sheets("LogIn").Visible = xlVeryHidden
        End If
      End If
      Application.ScreenUpdating = True
    Else
      ThisWorkbook.Close
    End If
  Else
    MsgBox "Not a valid user, access to Summary only"
  End If
End Sub
 
Last edited:
Upvote 0
How can the code be adjusted so that the user would be able to see more than just one sheet? For example I have a speadsheet that will have two sheets that each user will need to see.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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