Scope of 'On Error Resume Next'?

Discussion in 'Microsoft Excel Programming' started by Matt Jensen, Jan 7, 2005.

  1. Matt Jensen

    Matt Jensen Guest

    What is the 'scope' of On Error Resume Next?
    Is it for a sub, for the next line after the statement only, for a module,
    or for a workbook/project?
    If more than one of these, how does one specify the different ones?
    Thanks
    Matt
     
    Matt Jensen, Jan 7, 2005
    #1
    1. Advertisements

  2. Matt Jensen

    Sharad Naik Guest

    The scope is the for that Sub.
    After exiting the sub it is - say - reset.
    As to specify different ones within the sub,
    you can do On Error GoTo 0

    '0' is not a label, it is just used to reset the Resume.

    Thus if

    On Error Resume Next
    'line one of code
    'line two of code
    On Error GoTo 0

    The resume next part will work only for the lines between Resume Next and
    GoTo 0, After the GoTo 0 .

    You can again use Resume Next "after the GoTo 0" on
    any further down lines. (and use again GoTo 0)

    Sharad

    "Matt Jensen" <> wrote in message
    news:...
    > What is the 'scope' of On Error Resume Next?
    > Is it for a sub, for the next line after the statement only, for a module,
    > or for a workbook/project?
    > If more than one of these, how does one specify the different ones?
    > Thanks
    > Matt
    >
    >
     
    Sharad Naik, Jan 7, 2005
    #2
    1. Advertisements

  3. Matt Jensen

    Bob Phillips Guest

    Matt,

    Try this code

    Sub test()
    testa
    testb
    End Sub

    Sub testa()
    Dim rng As Range
    On Error Resume Next
    rng = Range("A1")
    End Sub

    Sub testb()
    Dim rng As Range
    rng = Range("A1")
    End Sub

    What exactly do you mean by the secnd part?

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Matt Jensen" <> wrote in message
    news:...
    > What is the 'scope' of On Error Resume Next?
    > Is it for a sub, for the next line after the statement only, for a module,
    > or for a workbook/project?
    > If more than one of these, how does one specify the different ones?
    > Thanks
    > Matt
    >
    >
     
    Bob Phillips, Jan 7, 2005
    #3
  4. Matt Jensen

    Matt Jensen Guest

    Thanks Bob
    So it's applicable to a procedure only then.
    Cool, that answers the second part then thanks
    Matt

    "Bob Phillips" <> wrote in message
    news:...
    > Matt,
    >
    > Try this code
    >
    > Sub test()
    > testa
    > testb
    > End Sub
    >
    > Sub testa()
    > Dim rng As Range
    > On Error Resume Next
    > rng = Range("A1")
    > End Sub
    >
    > Sub testb()
    > Dim rng As Range
    > rng = Range("A1")
    > End Sub
    >
    > What exactly do you mean by the secnd part?
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Matt Jensen" <> wrote in message
    > news:...
    > > What is the 'scope' of On Error Resume Next?
    > > Is it for a sub, for the next line after the statement only, for a

    module,
    > > or for a workbook/project?
    > > If more than one of these, how does one specify the different ones?
    > > Thanks
    > > Matt
    > >
    > >

    >
    >
     
    Matt Jensen, Jan 7, 2005
    #4
  5. Matt Jensen

    Matt Jensen Guest

    Ahh, lovely thanks Sharad
    Cheers
    Excellent in fact!
    Matt

    "Sharad Naik" <> wrote in message
    news:...
    > The scope is the for that Sub.
    > After exiting the sub it is - say - reset.
    > As to specify different ones within the sub,
    > you can do On Error GoTo 0
    >
    > '0' is not a label, it is just used to reset the Resume.
    >
    > Thus if
    >
    > On Error Resume Next
    > 'line one of code
    > 'line two of code
    > On Error GoTo 0
    >
    > The resume next part will work only for the lines between Resume Next and
    > GoTo 0, After the GoTo 0 .
    >
    > You can again use Resume Next "after the GoTo 0" on
    > any further down lines. (and use again GoTo 0)
    >
    > Sharad
    >
    > "Matt Jensen" <> wrote in message
    > news:...
    > > What is the 'scope' of On Error Resume Next?
    > > Is it for a sub, for the next line after the statement only, for a

    module,
    > > or for a workbook/project?
    > > If more than one of these, how does one specify the different ones?
    > > Thanks
    > > Matt
    > >
    > >

    >
    >
     
    Matt Jensen, Jan 7, 2005
    #5
  6. Matt Jensen

    Jake Marx Guest

    Hi Matt,

    Matt Jensen wrote:
    > What is the 'scope' of On Error Resume Next?
    > Is it for a sub, for the next line after the statement only, for a
    > module, or for a workbook/project?
    > If more than one of these, how does one specify the different ones?
    > Thanks
    > Matt


    There is one more thing worth mentioning here. Yes, error handling set in a
    routine will be reset when that routine has finished executing. However,
    any procedures called *by that routine* that do not have their own error
    handling will inherit the error handling of the calling routine. This can
    cause unexpected results if you aren't prepared for it.

    For example:

    Sub Demo()
    On Error Resume Next

    SubA
    SubB

    On Error Goto 0
    End Sub

    Sub SubA()
    Dim rng As Range

    rng = Range("A1")
    End Sub

    Sub SubB()
    Dim l As Long

    l = CLng("ABC")
    End Sub


    Even though runtime errors are generated by both SubA and SubB, no error
    message will be displayed. That is because error handling rolls "uphill" -
    since SubA has no error handling, the error is raised back up the call stack
    to the Demo subroutine, which then handles the error by Resume Next and
    continues on to call SubB. Since SubB has no error handling defined, the
    same thing happens when it encounters a runtime error. This behavior
    extends all the way through the call stack, so if you have SubA that calls
    SubB, which in turn calls SubC, and SubB and SubC have no error handling
    defined, the error handler in SubA would catch any errors from SubA, SubB,
    or SubC.

    Just thought I'd mention this behavior in case you weren't aware of it.

    --
    Regards,

    Jake Marx
    MS MVP - Excel
    www.longhead.com

    [please keep replies in the newsgroup - email address unmonitored]
     
    Jake Marx, Jan 7, 2005
    #6
  7. Matt Jensen

    Tom Ogilvy Guest

    An error handler handles errors in subordinate routine if they don't have
    their own error handler. When the error is encounterd, the subordinate
    routine exits up to the next routine with an error handler. that is why the
    last msgbox in each subordinate routine is not executed:

    Sub test()
    On Error Resume Next
    MsgBox "test Before A err:" & Err.Number
    testa
    MsgBox "test After A err:" & Err.Number
    testb
    MsgBox "ending err:" & Err.Number
    End Sub

    Sub testa()
    Dim rng As Range
    MsgBox "In TextA Line1 err: " & Err.Number
    Err.Raise 5000
    MsgBox "In TestA Last Line err:" & Err.Number
    End Sub

    Sub testb()
    Dim rng As Range
    MsgBox "In TextB Line1 err: " & Err.Number
    Err.Raise 6000
    MsgBox "In Testb Last Line err: " & Err.Number
    End Sub

    --
    Regards,
    Tom Ogilvy

    "Matt Jensen" <> wrote in message
    news:...
    > What is the 'scope' of On Error Resume Next?
    > Is it for a sub, for the next line after the statement only, for a module,
    > or for a workbook/project?
    > If more than one of these, how does one specify the different ones?
    > Thanks
    > Matt
    >
    >
     
    Tom Ogilvy, Jan 7, 2005
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim

    "On Error Resume Next" Question

    Jim, Sep 20, 2003, in forum: Microsoft Excel Programming
    Replies:
    1
    Views:
    233
    J.E. McGimpsey
    Sep 20, 2003
  2. Peter

    VBA ... On Error Resume Next ... Pivot Table

    Peter, Oct 15, 2003, in forum: Microsoft Excel Programming
    Replies:
    0
    Views:
    372
    Peter
    Oct 15, 2003
  3. Peter

    VBA ... On Error Resume Next ... Pivot Table

    Peter, Oct 15, 2003, in forum: Microsoft Excel Programming
    Replies:
    2
    Views:
    789
    Peter
    Oct 15, 2003
  4. Mike

    On Error Resume Next

    Mike, Nov 21, 2003, in forum: Microsoft Excel Programming
    Replies:
    3
    Views:
    246
    Otto Moehrbach
    Nov 23, 2003
  5. D.S.

    On Error Resume Next

    D.S., Nov 28, 2003, in forum: Microsoft Excel Programming
    Replies:
    1
    Views:
    187
    Michael
    Nov 28, 2003
Loading...

Share This Page