Deleting a sheet if it exists?

D

drucey

Me again!

Flying along nicely, apart from...

In a macro i have:

Sheets("Completed Orders").Delete
Sheets("Partially Arrived Orders").Delete
Sheets("Draft Orders").Delete
Sheets("Placed Orders").Delete

But coming from another macro that's already deleted them (in som
cases only)..

How would i say

IF sheet "completed orders" exists then delete else not to worry

Thanks al
 
D

Don Guillett

try

Sub deletesheets()
Application.DisplayAlerts = False
On Error Resume Next
myarray = Array("sheet1", "sheet4", "sheetetc")
Sheets(myarray).Delete
End Sub
 
S

S. I. Becker

drucey said:
Me again!

Flying along nicely, apart from...

In a macro i have:

Sheets("Completed Orders").Delete
Sheets("Partially Arrived Orders").Delete
Sheets("Draft Orders").Delete
Sheets("Placed Orders").Delete

But coming from another macro that's already deleted them (in some
cases only)..

How would i say

IF sheet "completed orders" exists then delete else not to worry

Thanks all

By wrapping the code in an error trapping block, for instance

Sub DeleteIfExists(ByVal SheetName as String)
On Error Resume Next
Sheets(SheetName).Delete
Err.Clear
End Sub
 
D

Dave Peterson

Just a word of warning. If sheet4 doesn't exist, then the .delete will fail and
none of those worksheets will be deleted.

I think I'd just use:

Sub deletesheets()
Application.DisplayAlerts = False
On Error Resume Next
sheets("sheet1").delete
sheets("sheet2").delete
sheets("sheetetc").delete
on error goto 0
application.displayalerts = true
End Sub

ps. to the OP: remember that there has to be at least one visible sheet when
you're done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top