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 Mike Lewis 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 1

Status
Not open for further replies.

pkirkley

Programmer
Sep 6, 2001
12
0
0
US
I am attempting 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
 
Don't select the sheet first - just do:

on error resume next
sheets(oldSheet).delete
on error goto 0

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top