VBA to have password open all sheets or only one

megera716

Board Regular
Joined
Jan 3, 2013
Messages
140
Office Version
  1. 365
Platform
  1. Windows
Years ago, someone in this forum wrote me some VBA code to protect a workbook so when you open it, if you enter password "abc123" all sheets are visible and if you enter password "welcome", it opens with only one sheet visible and the other ones can't be unhidden (well they probably can but only if you know VBA and I can assure you, the people at my organization I'm protecting this from do NOT know VBA ?).

I've since left the first organization where I used that code or I would just go copy it but it would REALLY come in handy at my current employer.

Can someone do that for me? The one sheet that should open with "welcome" is named "Case List". I'm using Excel 365.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Probably the link below is to your original post and the code @DanteAmor wrote for you

 
Upvote 0
Probably the link below is to your original post

Well looky there, that's the one! I certainly thought it was older than 2019 as well!

OK, just wondering, if you know -- this workbook that I'm trying to protect now has like 17 sheets vs the old one having 4. Do I need to name all 17 sheets like I did with the 4 in the original code? Or there is some sort of "all sheets" shortcut for the Visible and VeryHidden parts of the code? Clearly I don't know VBA either ? but I do love it!
 
Upvote 0
You can loop through the worksheets, for example for the unhide part

Rich (BB code):
Private Sub Workbook_Open()
    Dim pass As Variant, ws As Worksheet
    pass = InputBox("Please enter password", "START")
 
    If pass = "" Then
        Exit Sub
    End If
 
    Select Case pass
    Case "Masterpass"
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next
    Case "Limitedpass"
        Sheets("Accounts").Visible = xlSheetVisible
        Sheets("Finance").Visible = xlSheetVisible
    Case Else
        MsgBox "Invalid Pasword"
        Exit Sub
    End Select
End Sub
 
Upvote 0
Maybe this way, you can add more Case statements to suit
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> ("Master") Then ws.Visible = xlSheetVeryHidden
Next ws
ThisWorkbook.Save
End Sub

Private Sub Workbook_Open()
  Dim pass As Variant, ws As Worksheet

  pass = InputBox("Please enter password", "START")
 
  If pass = "" Then
    Exit Sub
  End If
 
  Select Case pass
    Case "Masterpass"
    For Each ws In Worksheets
        If ws.Name <> ("Master") Then ws.Visible = xlSheetVisible
    Next ws
    Case "Limitedpass"
      Sheets("Accounts").Visible = xlSheetVisible
      Sheets("Finance").Visible = xlSheetVisible
    Case Else
      MsgBox "Invalid Pasword"
      Exit Sub
  End Select
End Sub
 
Upvote 0
Maybe this way, you can add more Case statements to suit
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> ("Master") Then ws.Visible = xlSheetVeryHidden
Next ws
ThisWorkbook.Save
End Sub

Private Sub Workbook_Open()
  Dim pass As Variant, ws As Worksheet

  pass = InputBox("Please enter password", "START")
 
  If pass = "" Then
    Exit Sub
  End If
 
  Select Case pass
    Case "Masterpass"
    For Each ws In Worksheets
        If ws.Name <> ("Master") Then ws.Visible = xlSheetVisible
    Next ws
    Case "Limitedpass"
      Sheets("Accounts").Visible = xlSheetVisible
      Sheets("Finance").Visible = xlSheetVisible
    Case Else
      MsgBox "Invalid Pasword"
      Exit Sub
  End Select
End Sub


I think that this one actually works as intended for the most part!

When I open the file in OneDrive (so, in the browser), it does not prompt me for a password and only the Case List sheet is visible (I replaced "Master" in your original code with "Case List").

When I open in Desktop app, it then prompts for password and if I enter the master password, all sheets are visible, and if I enter the limited password, only the Case List is visible. HURRAH.

But is there a way to enter the password while I'm viewing the workbook in the browser to view the rest of the sheets without opening in desktop app?
 
Upvote 0
@megera716
Sorry, can't help you with that one...hopefully someone elkse can jump in and assist
 
Upvote 0

Forum statistics

Threads
1,215,711
Messages
6,126,401
Members
449,312
Latest member
sweetfriend9

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