Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling when deleting a worksheet

Status
Not open for further replies.

pkirkley

Programmer
Sep 6, 2001
12
0
0
US
I am atttempting to delete a sheet from a workbook using this code:


Sub deleteSheet(oldSheet)

' Deletes the work space sheet
Sheets(oldSheet).Select
ActiveWindow.SelectedSheets.Delete
End If

End Sub

This works great if the sheet exists but if it is not there it deletes the active sheet that I do not want to delete. Any suggestions on how to use the On Error logic to skip the delete and allow the code to continue?

I have tried this but it did not work properly.


Sub deleteSheet(oldSheet)
On Error Resume Next
' Deletes the work space sheet
Sheets(oldSheet).Select
ActiveWindow.SelectedSheets.Delete
End If

End Sub
 
Try this:

on error resume next
Application.DisplayAlerts = False
sheets(oldsheet).delete
Application.DisplayAlerts = True
on error goto 0

Setting DisplayAlerts = False keeps Excel from asking if you want to delete it.

Peter Richardson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top