none
Help with Error Handler RRS feed

  • Question

  • Greetings -

    I am trying to figure out what I am doing wrong with my Error Handler. The error handler actually works when there is an error as should. The problem is, the error handler also works when there is no error, if that makes sense. Meaning, it applies all the time, error or not.

    Can anyone see what I may be doing wrong that could cause this?

    Here is me code:

    Option Explicit
    Public LastSheet As String

    Public Sub Select_Last()

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Global Back Button
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Application.ScreenUpdating = False   

    On Error GoTo Err

        Application.Sheets(LastSheet).Select
        Application.Sheets(LastSheet).Visible = False
        
    Err:
        MsgBox "Error encountered. Returning to Main page: " & Err.Description
        Sheets("Commission Pool").Select
        Application.Sheets(LastSheet).Visible = False
        Exit Sub
        Resume Next
        
    End Sub

    Couple of notes:

    • The error description (Err.Description) at the end of the MsgBox only appears when there is an actual error. Otherwise just the text in quotes appears...again even without an error.
    • I have also added the below in ThisWorkbook:              

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
            Application.ScreenUpdating = False
            LastSheet = Sh.Name
    End Sub  

    Any ideas are always much appreciated

    Thanks in advance for your time


    • Edited by rstreets2 Wednesday, March 28, 2018 10:35 PM typo
    Wednesday, March 28, 2018 10:33 PM

Answers

  • I suppose "Exit Sub" is needed just before "Err:".
    Otherwise, every process will reach "Err:", even if no error occurs.

    Ashidacchi -- http://hokusosha.com/

    • Marked as answer by rstreets2 Wednesday, March 28, 2018 11:10 PM
    Wednesday, March 28, 2018 10:45 PM

All replies

  • I suppose "Exit Sub" is needed just before "Err:".
    Otherwise, every process will reach "Err:", even if no error occurs.

    Ashidacchi -- http://hokusosha.com/

    • Marked as answer by rstreets2 Wednesday, March 28, 2018 11:10 PM
    Wednesday, March 28, 2018 10:45 PM
  • That indeed did the trick! Works like a charm now. Thank you so much for your input. Very much appreciated!
    Wednesday, March 28, 2018 11:10 PM