VBA Code Help!!

Maebus

New Member
Joined
Jun 25, 2024
Messages
24
Office Version
  1. 365
Platform
  1. Windows
I am creating a workbook to display employee performance stats with a separate sheet for each supervisor. I want it to work that each supervisor can only view their individual sheet and all others are very hidden so they cannot view other supervisors employees. I have code but when I have people test they are still able to view and select all worksheets. What am I missing or doing wrong? Here is what I have. Thanks in advance for any help.

Private Sub Workbook_Open()
Dim currentUser As String

' Get the current user
currentUser = Environ("USERNAME")

' Allow access to specific worksheets for specific users
Select Case currentUser
Case "JR", "A"
' Allow access to the worksheet named "A" for specific users
Sheets("A").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("B").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("D").Visible = xlSheetVeryHidden
Sheets("I").Visible = xlSheetVeryHidden
Sheets("R").Visible = xlSheetVeryHidden
Sheets("S").Visible = xlSheetVeryHidden
Sheets("M").Visible = xlSheetVeryHidden
Case "B", "JR"
' Allow access to the worksheet named "B" for a specific user
Sheets("B").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("A").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("D").Visible = xlSheetVeryHidden
Sheets("I").Visible = xlSheetVeryHidden
Sheets("R").Visible = xlSheetVeryHidden
Sheets("S").Visible = xlSheetVeryHidden
Sheets("M").Visible = xlSheetVeryHidden
Case "D", "JR"
' Allow access to the worksheet named "D" for a specific user
Sheets("D").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("A").Visible = xlSheetVeryHidden
Sheets("B").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("I").Visible = xlSheetVeryHidden
Sheets("R").Visible = xlSheetVeryHidden
Sheets("S").Visible = xlSheetVeryHidden
Sheets("M").Visible = xlSheetVeryHidden
Case "I", "JR"
' Allow access to the worksheet named "I" for a specific user
Sheets("I").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("A").Visible = xlSheetVeryHidden
Sheets("B").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("D").Visible = xlSheetVeryHidden
Sheets("R").Visible = xlSheetVeryHidden
Sheets("S").Visible = xlSheetVeryHidden
Sheets("M").Visible = xlSheetVeryHidden
Case "R", "JR"
' Allow access to the worksheet named "R" for a specific user
Sheets("R").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("A").Visible = xlSheetVeryHidden
Sheets("B").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("D").Visible = xlSheetVeryHidden
Sheets("I").Visible = xlSheetVeryHidden
Sheets("S").Visible = xlSheetVeryHidden
Sheets("M").Visible = xlSheetVeryHidden
Case "S", "JR"
' Allow access to the worksheet named "S" for a specific user
Sheets("S").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("A").Visible = xlSheetVeryHidden
Sheets("B").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("D").Visible = xlSheetVeryHidden
Sheets("I").Visible = xlSheetVeryHidden
Sheets("R").Visible = xlSheetVeryHidden
Sheets("M").Visible = xlSheetVeryHidden
Case "M", "JR"
' Allow access to the worksheet named "M" for a specific user
Sheets("M").Visible = xlSheetVisible
' Hide specific worksheets very hidden for selected users
Sheets("A").Visible = xlSheetVeryHidden
Sheets("B").Visible = xlSheetVeryHidden
Sheets("C").Visible = xlSheetVeryHidden
Sheets("D").Visible = xlSheetVeryHidden
Sheets("I").Visible = xlSheetVeryHidden
Sheets("R").Visible = xlSheetVeryHidden
Sheets("S").Visible = xlSheetVeryHidden
End Select
End Sub
 
What if you change the code to this?
VBA Code:
Private Sub Workbook_Open()
    Dim currentUser As String
    Dim allowedSheet As String
    Dim ws As Worksheet

    ' Get the current user
    currentUser = Environ("USERNAME")

    ' Check if current user is "JR"
    If currentUser = "JR" Then
        ' "JR" can access all sheets, make all sheets visible and exit sub
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next ws
        Exit Sub
    Else
        ' Define allowed sheet based on user
        allowedSheet = currentUser
    End If

    ' Loop through all sheets and set visibility
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = allowedSheet Then
            ws.Visible = xlSheetVisible
        Else
            ws.Visible = xlSheetVeryHidden
        End If
    Next ws
End Sub
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
What if you change the code to this?
VBA Code:
Private Sub Workbook_Open()
    Dim currentUser As String
    Dim allowedSheet As String
    Dim ws As Worksheet

    ' Get the current user
    currentUser = Environ("USERNAME")

    ' Check if current user is "JR"
    If currentUser = "JR" Then
        ' "JR" can access all sheets, make all sheets visible and exit sub
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next ws
        Exit Sub
    Else
        ' Define allowed sheet based on user
        allowedSheet = currentUser
    End If

    ' Loop through all sheets and set visibility
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = allowedSheet Then
            ws.Visible = xlSheetVisible
        Else
            ws.Visible = xlSheetVeryHidden
        End If
    Next ws
End Sub
Same error same line
 
Upvote 0
It's most likely because
1) no sheet name matches the user name. In the immediate window, type Debug.Print Environ("USERNAME") and hit enter. This should show you the user name. Make sure that there's a sheet that matches the user name. Often it has a period separating the first and last name so your sheet name must be as well.

2) When the names do not match, it'll try to hide all of the sheets, however, there must be at least one sheet visible. You can't hide all of the sheets.
 
Upvote 0
It's most likely because
1) no sheet name matches the user name. In the immediate window, type Debug.Print Environ("USERNAME") and hit enter. This should show you the user name. Make sure that there's a sheet that matches the user name. Often it has a period separating the first and last name so your sheet name must be as well.

2) When the names do not match, it'll try to hide all of the sheets, however, there must be at least one sheet visible. You can't hide all of the sheets.
I’m confused as to where I should be typing this. Also, as of right now, I’m the only user accessing it.
 
Upvote 0
It's most likely because
1) no sheet name matches the user name. In the immediate window, type Debug.Print Environ("USERNAME") and hit enter. This should show you the user name. Make sure that there's a sheet that matches the user name. Often it has a period separating the first and last name so your sheet name must be as well.

2) When the names do not match, it'll try to hide all of the sheets, however, there must be at least one sheet visible. You can't hide all of the sheets.
Also the sheets are as follows:
HomePage
Alan
Beth
Charlie
Danny
Ira
Randy
Simon
Matt
 
Upvote 0
The Immediate Window is located below the code editor. Sometimes it's minimized, you'll have to make it bigger or it's it's not visible at all try Ctrl + G.
As I mentioned the Environ("USERNAME") would return something like "alan.ross", thus it's looking for a sheet named "alan.ross", but if your sheet name is "Alan" it will throw an error. The returned string from Environ("USERNAME") must match exactly with the sheet name.
1719412483459.png
 
Upvote 0
The Immediate Window is located below the code editor. Sometimes it's minimized, you'll have to make it bigger or it's it's not visible at all try Ctrl + G.
As I mentioned the Environ("USERNAME") would return something like "alan.ross", thus it's looking for a sheet named "alan.ross", but if your sheet name is "Alan" it will throw an error. The returned string from Environ("USERNAME") must match exactly with the sheet name.
View attachment 113272
So it returned my username but I won’t have a worksheet named for me as I’m the super user of the entire document
 
Upvote 0
Did you change this part to your user name?
VBA Code:
Private Sub Workbook_Open()
...
    If currentUser = "JR" Then '<- Change "JR" to your name
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next ws
        Exit Sub
....
 
Upvote 0
The Immediate Window is located below the code editor. Sometimes it's minimized, you'll have to make it bigger or it's it's not visible at all try Ctrl + G.
As I mentioned the Environ("USERNAME") would return something like "alan.ross", thus it's looking for a sheet named "alan.ross", but if your sheet name is "Alan" it will throw an error. The returned string from Environ("USERNAME") must match exactly with the sheet name.
View attachment 113272
And I want each user to be able to see the HomePage and their individual sheet.
 
Upvote 0
Did you change this part to your user name?
VBA Code:
Private Sub Workbook_Open()
...
    If currentUser = "JR" Then '<- Change "JR" to your name
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next ws
        Exit Sub
....
I just did. Reopened and no error.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,592
Members
452,653
Latest member
craigje92

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