Excel - Can't delete sheets
Asked By John Smith on 03-Feb-12 11:01 AM
Hi,
I am trying to delete two sheets from a workbook that were added during
a Workbook_Open event. In the close procedure I have the following
code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
DoEvents
On Error Resume Next
If ActiveWorkbook.Worksheets(Sheets.Count).Name = "Summary" Then
ActiveWorkbook.Worksheets("Summary").Visible = True
ActiveWorkbook.Worksheets("Summary").Select
ActiveWorkbook.Worksheets("Summary").Delete
End If
On Error GoTo 0
Worksheets(Sheets.Count).Visible = True
Worksheets(Sheets.Count).Select
Worksheets(Sheets.Count).Delete
Application.Caption = Empty
ThisWorkbook.Save
ThisWorkbook.Close
Application.DisplayAlerts = True
Application.Quit
End Sub
The problem is that when I open the workbook, I get a '1004' error
because the summary sheet already exists. I put a counter in the
Workbook_Open procedure to verify that it was only firing once and
message box at the tail end of the Workbook_BeforeClose procedure
asking for the last sheet name, which was correct. So, why cannot I
delete these two sheets?
Thanks.
James
Don Guillett replied to John Smith on 03-Feb-12 11:42 AM
Try this. No need to unhide or select or check or close if quitting
excel.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ActiveWorkbook
Application.DisplayAlerts =3D False
On Error Resume Next
.Sheets("Summary").Delete
.Worksheets(Sheets.Count).Delete
.Save
End With
Application.Quit
End Sub
John Smith replied to Don Guillett on 03-Feb-12 12:04 PM
Thanks, Don, unfortunately that did not work, so I assume that the
problem really is in the Workbook_Open procedure, even though my
counter only said it ran once. Here is the code that I use to add the
two sheets:
Private Sub Workbook_Open()
Application.EnableEvents =3D True
With ThisWorkbook.Worksheets
.Add After:=3DWorksheets(Sheets.Count)
Worksheets(2).Range("E2:S2").Copy
Destination:=3DWorksheets(Sheets.Count).Cells(1, 1)
Worksheets(Sheets.Count).Visible =3D False
End With
Application.CutCopyMode =3D False
With ThisWorkbook.Worksheets
.Add After:=3DWorksheets(Sheets.Count)
Worksheets(Sheets.Count).Visible =3D False
End With
Worksheets(Sheets.Count).Name =3D "Summary"
End Sub
Don Guillett replied to John Smith on 03-Feb-12 12:49 PM
This code tested fine in xl2003 to add the two sheets on open and
delete on close. If you do not want the sheets created when you open
the assign to a button/shape. Or, send file to me at dguillett1
@gmail.com
Private Sub Workbook_Open()
ThisWorkbook.Worksheets.Add After:=3DWorksheets(Sheets.Count)
Worksheets(2).Range("E2:S2").Copy Cells(1, 1)
ActiveSheet.Visible =3D False
ThisWorkbook.Worksheets.Add After:=3DWorksheets(Sheets.Count)
ActiveSheet.Name =3D "Summary"
ActiveSheet.Visible =3D False
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ActiveWorkbook
Application.DisplayAlerts =3D False
On Error Resume Next
.Sheets("Summary").Delete
.Worksheets(Sheets.Count).Delete
.Save
End With
Application.Quit
End Sub
g
John Smith replied to Don Guillett on 03-Feb-12 12:50 PM
ing
I can send you the file, but it is over 5.5MB. Is that okay?
John Smith replied to Don Guillett on 03-Feb-12 02:05 PM
ing
Sorry, I was wrong. it is still a problem.
John Smith replied to Don Guillett on 03-Feb-12 01:58 PM
ing
Don, I finally got this to work! I had to manually delete the sheets
before the code would delete the sheets - even though the
workbook_open procedure created them. Thank you for your help, because
I really appreciate the time and effort that everyone gives me.
James
Don Guillett replied to John Smith on 03-Feb-12 06:28 PM
uring
g
n
r
d
I
I do not understand. Worked perfectly here. Sure, send the file
dguillett1 @gmail.com
John Smith replied to Don Guillett on 03-Feb-12 08:46 PM
ng
during
ing
hen
ror
e
and
re
t I
he
Sorry, Don, it worked perfectly for me, too, after I decided to stop
being stupid.