Hi all,
I'm trying to only show specific sheets per user using the environ variable and this code seems to work for the single user / sheet but the master user does not function correctly i.e. the code does not show all sheets, this is the code I am using:
Option Explicit
Private Sub Workbook_Open()
Dim UserList As Variant
Dim SheetList As Variant
Dim i As Integer, ShowSheet As Integer, j As Integer
Dim MasterUser As String, CurrentUser As String
UserList = Array("John", "Joe")
SheetList = Array("US", "Combined")
'this user can see all ** the sheets
MasterUser = "Jane"
'see who the user is
CurrentUser = Environ("username")
'set ShowSheet to -1 so no sheets are shown if the user isn't found
ShowSheet = -1
'find the index ** the correct sheet for this user
For i = LBound(UserList) To UBound(UserList)
If UCase(UserList(i)) = UCase(CurrentUser) Then
ShowSheet = i
Exit For
End If
Next i
'hide all sheets except the one the user should see
With ThisWorkbook
For j = LBound(SheetList) To UBound(SheetList)
If j = ShowSheet Or CurrentUser = UCase(MasterUser) Then
.Sheets(SheetList(j)).Visible = xlSheetVisible
Else
.Sheets(SheetList(j)).Visible = xlSheetVeryHidden
End If
Next j
End With
End Sub
Does anyone know why the above code does not respect the Master User "Jane" should be able to see all sheets?
Original source for this code was found here:
Bookmarks