Option Explicit
Dim bolInProcess As Boolean
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'// If the wb is saved, nothing needed. //
If Not ThisWorkbook.Saved Then
'// If not saved, substitute our own handling, by first cancelling (momentarily)//
'// the closing, then... //
Cancel = True
'// Ask user 'Save?' //
If MsgBox("The workbook is not saved. Save?", _
vbYesNo Or vbQuestion, _
"Save Workbook?" _
) = vbYes Then
'// Call the event to handle the save... //
Workbook_BeforeSave False, False
ThisWorkbook.Saved = True
ThisWorkbook.Close False
Else
'// ...or if user decided against saving, just mark the file as saved and //
'// close file, which will recurse to before_close, but will fail the IF //
'// and close without further ado. //
ThisWorkbook.Saved = True
ThisWorkbook.Close False
End If
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'// Set a flag to handle recurse. //
If Not bolInProcess Then
bolInProcess = True
'// Hide the sheet of interest. //
shtSheet2.Visible = xlSheetVeryHidden
'// Save the wb with the sheet hidden. This will breakout of the event, save //
'// wo/recursing to here (due to our flag), then... //
ThisWorkbook.Save
'// ...we'll Cancel the first called Save. //
Cancel = True
'// Check to see if its a user we want to see our 'secret squirrel' sheet. //
If Environ("UserName") = "stumpm" _
Or Environ("UserName") = "SomeoneElse" Then
'// If so, show the sheet and mark the file saved, to prevent unnecessary //
'// asking of the user if they later close w/no other changes. //
shtSheet2.Visible = xlSheetVisible
ThisWorkbook.Saved = True
End If
'// Reset our Bool //
bolInProcess = False
End If
' In gist - the above creates a pseudo After_Save event if you will. The workbook//
'// is never in a Saved state with the sheet showing. //
End Sub
Private Sub Workbook_Open()
'// See if we want to show the sheet. //
If Environ("UserName") = "stumpm" _
Or Environ("UserName") = "SomeoneElse" Then
shtSheet2.Visible = xlSheetVisible
ThisWorkbook.Saved = True
End If
End Sub