VBA Delete if sheet exists

Peter100

Well-known Member
Joined
Apr 21, 2002
Messages
765
Hi
Before closing a workbook I want to check if a sheet called temp exists. If it does then I want to delete it.

Can you help ?
 
Hi

Just tested this out and it only deletes one instance of TEMP, but not other sheets named TEMP01, 02 etc.
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Cheers Fazza
I thought that would delete all sheets but it worked a treat as long as the sheets weren't hidden as these are. Is there a way to delete them even if hidden ?

Forgive me keep coming back but I thought I'd keep the main question brief nut hadn't realised the other problems.

This is because you are testing and only moving ahead with the delete after you verified it starts with temp.

Remove the
Code:
END
from the code and it will fix your problem of only working on one sheet. Basically it currently quits after it deletes a sheet called temp.

Just a note on requesting help. It is a lot easier for us to code something up for you if you tell us your complete requirements from the start. It's more time consuming to keep adding things and making changes if the requirements keep changing.

Cheers

Dan
 
Upvote 0
Try this

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Dim FoundTemp As Boolean
For Each ws In Worksheets
    If UCase(Left(ws.Name, 4)) = "TEMP" Then
        If MsgBox("Would you like to delete the sheet: " & ws.Name & "?", vbYesNo, "Delete sheet?") = vbYes Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    End If
Next
End Sub


Edit: Yes I can confirm that it WILL delete hidden sheets too.

Cheers

Dan
 
Last edited:
Upvote 0
Hi it's me again

Spent the last 2 hours testing it in real time and it works superbly. But as there are a lot of the TEMP files created, it is a bit annoying to have to keep clickng yes to delete the individual sheets.

Could it be made to only prompt once to remove all TEMP worksheets ?
 
Upvote 0
try
Code:
Sub test()
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
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,883
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top