Unhide Sheets based on UserName

jbfrank

Active Member
Joined
Oct 13, 2003
Messages
290
I've seen numerous posts on this topic and have tried to adapt each of them to suit my needs, but I'm still running into errors in my code. What I'm trying to accomplish is to have certain worksheets in my workbook unhide for a specific user. I have a worksheet called "Summary" and then I have 8 additional worksheets...1 for each member of our team. What I'd like to happen is that when a member opens the file, only the Summary sheet and that person's individual sheet are visible (all other sheets are hidden); however, when our manager opens the file, I'd like him to have visibility to all sheets.

Here's what I currently have:
Code:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim Sht As Worksheet
If Environ("UserName").Value = "N13802" Then
    For Each Sht In ThisWorkbook.Sheets
        Sht.Visible = True

Else

If Environ("UserName").Value <> "N13802" Then
    For Each Sht In ThisWorkbook.Sheets
        If Sht.Name <> "Summary" Then
            Sht.Visible = xlSheetHidden
        End If
Next

Sheets(Environ("UserName")).Visible = True
Sheets(Environ("UserName")).Activate
ActiveSheet.Range("E4").Select
End If

End Sub

I'm getting a compile error that reads, "Else without If". I've tried a number of variations to address this, but nothing seems to work. Any ideas on what I'm doing wrong? Feels like I'm overlooking something fairly simple.

Thanks in advance for the help!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Give this a try..

Code:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim Sht As Worksheet

If Environ("UserName") = "N13802" Then
    
    For Each Sht In ThisWorkbook.Sheets
        Sht.Visible = True
    Next

    Else

    For Each Sht In ThisWorkbook.Sheets
        If Sht.Name <> "Summary" Then
            Sht.Visible = xlSheetHidden
        End If
    Next

    Sheets(Environ("UserName")).Visible = True
    Sheets(Environ("UserName")).Activate
    ActiveSheet.Range("E4").Select

End If

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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