Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
If vbYes <> MsgBox("Delete all Temp sheet?", vbYesNo + vbQuestion) Then Exit Sub
On Error Resume Next
Application.DisplayAlerts = False
For Each ws In Sheets
If LCase(ws.Name) Like "temp*" Then ws.Delete
Next
Application.DisplayAlerts = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Dim UserCancelled As Boolean
UserCancelled = False
For Each ws In Worksheets
If UCase(Left(ws.Name, 4)) = "TEMP" Then
If Not UserCancelled Then
If MsgBox("Would you like to delete the sheet: " & ws.Name & "?", vbYesNo, "Delete sheet?") = vbYes Then UserCancelled = True
End If
If UserCancelled Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next
End Sub
Try this
Code:Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim ws As Worksheet For Each ws In Worksheets If ws.Name = "Temp" Then Application.DisplayAlerts = False Sheets("Temp").Delete Application.DisplayAlerts = True End <<< fyi I'm pretty sure this is an error. this dumps out of the procedure. End If Next End Sub
Paste this at the workbook level in the VBE