Macro to delete a worksheet before Save As

Keith24249

New Member
Joined
Jun 18, 2011
Messages
21
Hi,
I would like to write a piece of code that would run when I click "Save As". I need it to delete a worksheet called "DATA" when I click Save As. I have searched many forums and googled it but nothing seems to work. The workbook is compiled in Excel 2007. Can anyone help. If you would like a copy of the workbook please let me know.
Regards
Keith
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Not sure if it is possible to specify SaveAS, is Save okay? That will catch both Save and Save As:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.DisplayAlerts = False
Worksheets("DATA").Delete
Application.DisplayAlerts = True
End Sub

Note- there is no error handling, so if there is no sheet named "DATA", then the macro will error out
 
Upvote 0
This will catch only Save As
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If SaveAsUI Then
        Application.DisplayAlerts = False
        On Error Resume Next
        Sheets("Data").Delete
        Application.DisplayAlerts = True
    End If
End Sub

Put this code in the ThisWorkbook module.
 
Upvote 0
Hi AlphaFrog,

Many thanks for your reply and support. This code works great except that when I want to open the saved as copy I get an error message which says
Runtime Error 9
Subscript Out Of Range
Is there anything that you can recommend?
Would a possibility be to delete the code instead of the whole page titled DATA?
Hope you can helpRegards
Keith
 
Upvote 0
When you open the saved as copy and get the error, click on the Debug button. What line of code is highlighted?
 
Upvote 0
Hi,

The line of code highlighted is as follows:

Code:
Worksheets("QUOTE").Range("H2") = Worksheets("DATA").Cells(2, "E").Value

M
Thanks
Keith
 
Upvote 0
Hi,

Sorry! Yes there is some code that increments the unique number by 1 when I open the workbook template. The code is as follows:

Code:
 Private Sub Workbook_Open()
    Worksheets("QUOTE").Range("H2") = Worksheets("DATA").Cells(2, "E").Value
    Worksheets("DATA").Cells(2, "E").Value = Worksheets("DATA").Cells(2, "E").Value + 1
End Sub

Regards
Keith
 
Upvote 0
This will ignore the error if worksheet Data is deleted.

Code:
Private Sub Workbook_Open()
    [COLOR="Red"]On Error Resume Next[/COLOR]
    Worksheets("QUOTE").Range("H2") = Worksheets("DATA").Cells(2, "E").Value
    Worksheets("DATA").Cells(2, "E").Value = Worksheets("DATA").Cells(2, "E").Value + 1
End Sub
 
Upvote 0
Hi AlphaFrog,

This has got rid of the error message and allows me to re-open the saved as copy as many times as I like without the increment. However when I re-open the template it hasn't incremented to the next number and opens with the last saved as number. Hope I have explained this concisely enough
Regards
Keith
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

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