Search code examples
vbagoto

On error goto inside an if, does it react to any error on the sub?


I have a question about the On Error GoTo. I have this line of code

    If CheckF = 0 Then
        On Error GoTo Error_handler
        .PivotItems(.PivotItems(pivotcount).name).Visible = False


    End If

My question is that if I have an error outside that if it trigger the On Error anyway

And if that is the case, I would like to have a way to avoid it, because I need a different kind of message for each different error.

I hope I explained myself.


Solution

  • In your example, anything executed after the On Error statement will go to the error handler label, even after the End If statement. If you want to turn off this error handler you need to reset it as follows:

    If CheckF = 0 Then
        On Error GoTo Error_handler
        .PivotItems(.PivotItems(pivotcount).name).Visible = False
        On Error GoTo 0
    End If