VBA: Delete sheet if exist

Laurens1

New Member
Joined
Dec 9, 2016
Messages
31
Hi Guys,

I have this code for handling errors in my VBA code:

Code:
w00t:
    Beep
        MsgBox "Error in Task"
     Worksheets("Input").Activate
    Rows("1:" & Rows.Count).ClearContents
     
 
ThisWorkbook.Sheets("Export Journal").Delete
ThisWorkbook.Sheets("Fin (2)").Delete

    
    Sheets("Fin").Visible = False
    Sheets("Input").Visible = False
  
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
      Worksheets("Converter").Activate
End Sub

But the sheets "Export Journal" and "Fin (2)" aren't always in the workbook. It depends where the error is. When I handle the error like this, i'll get a error in my error handling. That can't be good :)

I was fiddling around with On error resume next, but i wasn't able to get it working. Can you guys help me out to handle the error correct?

Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You could try this:

Code:
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Sheets
    Select Case sh.Name
        Case "Export Journal", "Fin (2)"
            sh.Delete
    End Select
Next
Application.DisplayAlerts = True
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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