Delete Worksheet if it exists, if not ignore

leeksleeks

Board Regular
Joined
Oct 31, 2013
Messages
96
Hi,

I am trying to write an ErrHandler code but am having a few issues. Basically I want the Err message to delete a worksheet called "Scorecard" and one called "Comparison" if they exist. If they don't then I would like the macro to continue. The macro I have written scrapes a website and takes the id from the web address eg www.website/101. It is controlled by a popup box thats asks for a start number (101) and an end number (105). The website however sometimes has gaps between the numbers so a logical sequence of 101 - 105 may be missing number 103 which is why I have the ErrHandler to skip the macro. I would then like the macro to continue with the next webpage (104) after the Errhandler has dealt with the webpage that doesn't exist

Here is what I have so far:

ErrHandler:
Application.DisplayAlerts = False
Sheets("Scorecard").Delete
Sheets("Comparison").Delete
Application.DisplayAlerts = True

The problem I have is that I get a "Run-time error '9': Subscript out of range" when the website I am scraping data off doesn't exist. I have tried adding On Error resume Next and On Error GoTo 0 messages before the code but that hasn't worked.

Any help you can provide will be greatly appreciated.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi,
see if this helps:

Code:
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets(Array("Scorecard", "Comparison")).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

Dave
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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