1. Add a BeforeClose event to ThisWorkbook
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
sUserID = "Unknown"
ShowRightWorksheets
End Sub
Private Sub Workbook_Open()
Set wsUsers = Worksheets("Users")
Set rUsers = wsUsers.Cells(1, 1).CurrentRegion
Set wsIntro = Worksheets("Sheet1")
sUserID = Environ("UserName")
sUserName = Application.UserName
ShowRightWorksheets
wsIntro.Select
End Sub
2. I removed the Intro idea, but there were a few compatibility changes to the other macro, so
Option Explicit
Option Private Module
Public wsUsers As Worksheet, wsIntro As Worksheet
Public sUserID As String, sUserName As String
Public rUsers As Range
Sub ShowRightWorksheets()
Dim iUser As Long, iWS As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
'if user not in list, use USER=Unknown in col B
iUser = 2
On Error Resume Next
iUser = Application.WorksheetFunction.Match(sUserID, rUsers.Rows(1), 0)
On Error GoTo 0
'find the WS row
For Each ws In ActiveWorkbook.Worksheets
ws.Visible = xlSheetVisible
iWS = 0
On Error Resume Next
iWS = Application.WorksheetFunction.Match(ws.Name, rUsers.Columns(1), 0)
On Error GoTo 0
'if the WS is in the list
If iWS > 0 Then
'if the row/col is empty then VeryHide the WS
If Len(rUsers.Cells(iWS, iUser).Value) = 0 Then
ws.Visible = xlSheetVeryHidden
End If
'if WS NOT in list, then VeryHide it
Else
ws.Visible = xlSheetVeryHidden
End If
Next
Application.ScreenUpdating = True
End Sub
3. I made some changes to the User database worksheet to always have Sheet1 visible