Show only one sheet in a workbook

SeventyOne

New Member
Joined
Aug 31, 2009
Messages
3
I have multiple sheets within a workbook, but I only want one to be shown based on a user ID/password that the user enters. I have heard that creating a user form and then a case statement is the easiest way to do this, but am having a difficult time setting it up. Any help would be appreciated. Thanks.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi and welcome to the board!!!
You can do this based on the log-in name of each user. You will need, however, a dummy sheet that is always visible (Say Sheet1). Now, in the ThisWorkBook module (RightClick the Excel Icon to the left of the word "File" on the menu bar and choose "View Code")
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
For Each ws In Sheets
  If ws.Name <> "Sheet1" Then ws.Visible = xlSheetHidden
  Next ws
End Sub
 
Private Sub Workbook_Open()
Select Case Environ("UserName")
Case "User1": Worksheets("User1").Visible = xlSheetVisible
Case "User2": Worksheets("User2").Visible = xlSheetVisible
"etc
Case Else:
End Select
End Sub
Change UserNames and Sheet Names as needed.

HTH
lenze
 
Upvote 0
Thanks! Another question is when I go to save the file without closing, all sheets are hidden from view, even though my user name allows me to view all sheets. Is there a way to prevent this from happening?
 
Upvote 0
Maybe
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
If Environ("UserName") <> "YourName" Then
   For Each ws In Sheets
      If ws.Name <> "Sheet1" Then ws.Visible = xlSheetHidden
      Next ws
End If
End Sub
lenze
 
Upvote 0
This might be better!!
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
If Environ("UserName") = "YourName" Then
   rspn = MsgBox ("Hide Sheets and Close?",vbYesNo, "Save")
   If rspn = vbNo Then Exit Sub 
End If
For Each ws In Sheets
If ws.Name <> "Sheet1" Then ws.Visible = xlSheetHidden
Next ws
End If
End Sub
Have to think about it! Trying to avoid pitfalls!
You also might want to change each instance of
Code:
ws.Visible = xlSheetHidden
to
Code:
ws.Visible = xlSheetVeryHidden
This will make the sheets available only via code, so the user can't bypass the macros to view forbidden sheets.

lenze
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

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