Hello!
I thought this was "Solved", but then I stuck the code I got from this forum into our workbook which has protected sheets, protected workbook, and Save disabled. I have the syntax or order or something messed up because I am getting Run Time Error 424: Object Required.
Here's the bad code which tries to unprotect the sheets and I would think you have to unprotect the workbook if it is going to delete sheets, but it isn't there yet:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = False Then
Cancel = True
MsgBox "You cannot save this workbook using ""Save"". Use ""Save As""."
GoTo EndMe
End If
If MsgBox("Do you want the program to delete the unused sheets prior to Save?", vbYesNo + vbQuestion, "Delete Unused Sheets?") = vbYes Then
Application.DisplayAlerts = False
For Each sh In Sheets
sh.Unprotect Password:="secret"
If Application.CountIf(sh.Range("d4:d6"), "DELETEME") = 3 Then sh.Delete
If Application.CountIf(sh.Range("d5:d7"), "DELETEME") = 3 Then sh.Delete
Next
sh.Protect Password:="secret"
End If
EndMe:
End Sub
Forgot to add: The sheets being deleted have DELETEME either in d4:d6 or d5:d7. Those cells are LName, FName, MName, and each sheet has that info in one of the two ranges. So some of the sheets use d4:d6 and others use d5:d7. That's why there those two lines in the code.
VR/Lost
Bookmarks