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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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