[FONT=Courier New][SIZE=1]Option Explicit
Option Compare Text[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Private Sub Workbook_Open()[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1] Dim ws As Worksheet
Dim reply As Integer
Dim usr As String
If Environ("username") = "[COLOR=red][B]Paul-H[/B][/COLOR]" Then
reply = MsgBox("You are logged in as Administrator" & Space(15) & vbCrLf & vbCrLf _
& Space(5) & "Click 'Yes' to run the security script" & Space(15) & vbCrLf & vbCrLf _
& Space(5) & "Click 'No' to display all worksheets" & Space(15), vbYesNo + vbQuestion)
If reply = vbNo Then
For Each ws In Worksheets
ws.Visible = True
Next ws
ThisWorkbook.Save
Exit Sub
End If
End If
Application.ScreenUpdating = False
On Error Resume Next
Sheets("Main").Visible = True
Set ws = Sheets("Main")
On Error GoTo 0
If ws Is Nothing Then Sheets.Add.Name = "Main"
usr = Environ("username")
Set ws = Nothing
On Error Resume Next
Set ws = Sheets(usr)
ws.Visible = True
On Error GoTo 0
If ws Is Nothing Then
[/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green] ' user worksheet not found - try to create a new one using the template worksheet
[/COLOR] On Error Resume Next
Sheets("Template").Visible = True
Set ws = Sheets("Template")
On Error GoTo 0
If Not ws Is Nothing Then
[/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green] ' template worksheet found - create a new user worksheet
[/COLOR] Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = usr
Set ws = Sheets(usr)
Sheets("Template").Visible = xlVeryHidden
Else
[/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green] ' template worksheet not found - create blank sheet
[/COLOR] Sheets.Add.Name = usr
End If
End If
[/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green] ' shuffle Main & Template worksheets to the front
[/COLOR] For Each ws In Worksheets
ws.Visible = True
If ws.Name = "Main" Then ws.Move Before:=Sheets(1)
If ws.Name = "Template" Then ws.Move After:=Sheets(1)
Next ws
For Each ws In Worksheets
If ws.Name <> usr Then ws.Visible = xlVeryHidden
Next ws
ThisWorkbook.Save
Application.ScreenUpdating = False
End Sub[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Private Sub Workbook_BeforeClose(Cancel As Boolean)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1] Dim ws As Worksheet
Dim rep As Integer
If ThisWorkbook.Saved = False Then
rep = MsgBox("You must save this workbook if you want your worksheet to remain hidden." _
& vbCrLf & vbCrLf _
& "Do you want to save the changes you made to '" & ThisWorkbook.Name & "'?", vbYesNoCancel)
If rep = vbCancel Then Cancel = True: Exit Sub
If rep = vbNo Then ThisWorkbook.Saved = True: ThisWorkbook.Close
End If
On Error Resume Next
Sheets("Main").Visible = True
Set ws = Sheets("Main")
On Error GoTo 0
If ws Is Nothing Then Sheets.Add.Name = "Main"
For Each ws In Worksheets
If ws.Name <> "Main" Then ws.Visible = xlVeryHidden
Next ws
Sheets("Main").Visible = True
ThisWorkbook.Save
End Sub[/SIZE][/FONT]