Exit Sub Only if Sheet Not Found

jacobrcotton

Board Regular
Joined
Jan 28, 2017
Messages
51
Hi All,

I've never been let down here, and today will be no exception.

The Goal:
I have a macro running that will Save a workbook, Save As a workbook with a unique file name in the same path, then roll through each worksheet to copy & paste values, and then roll through each worksheet to prompt the user if they wish to delete said worksheet. It then finds the "Experience" worksheet and activates it so that when opened next, it opens to the "Experience" tab in cell A1. If a user accidentally deletes the "Experience" tab, i wish for the macro to prompt a MsgBox that states something to the tune of "Experience tab deleted", and then exit the Sub

The Problem:
Everything is working until the On Error GoTo ExitSub code.

Any assistance would be helpful. Thank you so much!

Code:
Sub ReadyForClient()


    Dim relativePath As String


    Select Case MsgBox("You Can't Undo This Action. " & "Save Workbook First?", vbYesNoCancel, "Alert")
        Case Is = vbYes
            ThisWorkbook.Save
            relativePath = ThisWorkbook.Worksheets("Documentation").Range("B1").Value & ThisWorkbook.Worksheets("Documentation").Range("B2").Value & "_cc.xlsm"
            ThisWorkbook.SaveAs Filename:=relativePath
        Case Is = vbCancel
            Exit Sub
    End Select


    Call UnhideAllSheets
    Call CopyPasteValuesAllSheets
    Call DeleteUnnecessaryTabs
    Call FindHomeAllSheets


    On Error GoTo ExitSub
    ThisWorkbook.Worksheets("Experience").Activate
    
ExitSub:
    MsgBox "Experience tab was deleted. Please review."
    Exit Sub


End Sub
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Code:
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets("Experience")
If Err <> 0 Then
    MsgBox "Experience tab was deleted. Please review."
    Exit Sub
Else
    Sheets("Experience").Activate
End If
 
Upvote 0
The Exit Sub should come before the ExitSub: line. In your code, the MsgBox line will always be executed even if there was no error. Putting Exit Sub before the ExitSub: line skips the MsgBox if the activation was successful.

Also, the closeness of ExitSub with Exit Sub could be confusing. You might consider changing ExitSub to something else.

Code:
    On Error GoTo ExitSub
    ThisWorkbook.Worksheets("Experience").Activate
    Exit Sub
ExitSub:
    MsgBox "Experience tab was deleted. Please review."
End Sub
 
Upvote 0
shknbk2, this worked perfectly. and yes, i think you are correct. I will change the same of "ExitSub" to something more distinct. Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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