question

MrMJ-5923 avatar image
0 Votes"
MrMJ-5923 asked emilyhua-msft answered

How to delete worksheet when closing the workbook

Hi

How to automatically delete a specific sheet (the sheet name containing with (.delete) ) in a workbook via VBA when closing it.

Many thanks

J.

office-vba-devoffice-excel-itprooffice-scripts-excel-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

emilyhua-msft avatar image
0 Votes"
emilyhua-msft answered

Hi @MrMJ-5923

Welcome to Q&A forum ~
Please note, "office-excel-itpro" tag focues more on general issue, for issues related to VBA code, it's best wait to the VBA experts' suggestions.

According to the same thread "Delete a sheet on closing the workbook", I suggest you try following code. (Please Note: Since the web site is not hosted by Microsoft, the link may changed without notice. Microsoft does not guarantee the accuracy of this information.)

 Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Dim sht As Worksheet
 Application.DisplayAlerts = False
    
    
     For Each sht In Sheets
         If sht.Name Like "*.delete" Then
             sht.Delete
         End If
     Next sht
        
 ThisWorkbook.Save
 Application.DisplayAlerts = True
 End Sub

Hope the information could be helpful.


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.