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 ?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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
    End If
Next
End Sub

Paste this at the workbook level in the VBE
 
Upvote 0
Cheers Blade that worked great.

Could one build in a essage box to say delete or not then take the appropriate action ?
 
Upvote 0
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Dim FoundTemp As Boolean
For Each ws In Worksheets
    If ws.Name = "Temp" Then
        If MsgBox("Would you like to delete the Temp sheet?", vbYesNo, "Delete Temp?") = vbYes Then
            Application.DisplayAlerts = False
            Sheets("Temp").Delete
            Application.DisplayAlerts = True
            End
        End If
    End If
Next
End Sub
 
Upvote 0
Hi Again

Turning it into a realtime situation, I indeed have several sheets called Temp, ie Temp01, Temp02, Temp03 etc.

So I changed the
If ws.Name = "Temp" Then

To

If ws.Name Like "Temp*" Then

Which worked great, but it errored when I tried to modify the
Sheets("Temp").Delete

What is the answer ?
 
Upvote 0
Maybe use ws.delete

BTW, consider checking before deleting that you are not about to delete the sole worksheet in a workbook.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,530
Messages
6,125,350
Members
449,220
Latest member
Edwin_SVRZ

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