Delete a sheet on closing the workbook

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I've written the following code to delete the sheet called ... Entry (2) .... when exiting the workbook ?

Code:
Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, _Cancel As Boolean)
For Each sht In Sheets
If sht.Name = "Entry (2)" Then
sht.Delete
End If
Next sht
Workbook.Save
End Sub

However, it doesn't seem to be deleting the sheet ... when I re-open the workbook, the sheet is still there.

Can anyone see what I've done incorrectly ?

Kind regards,

Chris
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
For Each Sht In Sheets
If Sht.Name = "Entry (2)" Then
Sht.Delete
End If
Next Sht
ThisWorkbook.Save

End Sub
 
Upvote 0
Code:
Sub sbDeleteASheet()'Stopping Application Alerts
Application.DisplayAlerts=FALSE
Sheet1.Delete
'OR You can mention the Sheet name
Sheets("Sheet2").Delete
'Enabling Application alerts once we are done with our task
Application.DisplayAlerts=TRUE
End Sub

Hi friend ,

Change the sheet name.

Regards
 
Upvote 0
Code:
Option Explicit


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Dim Workbook As Workbook
Application.DisplayAlerts = False


    For Each sht In Sheets
        If sht.Name = "Entry (2)" Then
            sht.Delete
        End If
    Next sht
    
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
 
Upvote 0
1) Check spelling.
2) put it in the BeforeClose Event in the this workbook module. I mention this because it's not clear what event you are using. App_WorkbookBeforeClose makes it appear you have created a custom workbook class but not at all sure what you are doing so you should make that clear.
 
Upvote 0
P.S. You better do some error handling to avoid the code crashing if the sheet doesn't exist or is already deleted.
 
Upvote 0
Hi all,

I ended up going with the following code ...

Code:
Option Explicit


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Dim Workbook As Workbook
Application.DisplayAlerts = False


    For Each sht In Sheets
        If sht.Name = "Entry (2)" Then
            sht.Delete
        End If
    Next sht
    
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub

However, I changed "Entry (2)" to "Entry (#)" as there might be more than one copy of the Entry worksheet that needs to be deleted.

Unfortunately, it doesn't seem to be deleting any of the copies of the Entry sheet.

Also, Upon closing the workbook, how would I have the code changed to force the Entry sheet to be the one that is displayed next time the user opens the workbook ?

Kind regards,

Chris
 
Upvote 0
.

Paste in ThisWorkbook :

Code:
Option Explicit


Private Sub Workbook_Open()
    Sheets("Entry").Select
End Sub
 
Upvote 0
To delete the sheets try
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Application.DisplayAlerts = False


    For Each sht In Sheets
        If sht.Name Like "Entry (*)" Then
            sht.Delete
        End If
    Next sht
    
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
 
Upvote 0
^^ looks good to me. I shouldn't have said that error handling would be needed to prevent crashes since this is in an If test.

I suppose another option would be to put this in a before save event? Then you could close without saving if you preferred to not save the workbook at all.
 
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

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