+ Reply to Thread
Results 1 to 4 of 4

Error Handling - Did i do it correct

  1. #1
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256

    Error Handling - Did i do it correct

    Hi All,

    I know error handling can be tricky.
    So i have this in some code i am working on. i just want to make sure i am doing it correctly.

    I have this at the very top:
    Please Login or Register  to view this content.
    This is at the very bottom"
    Please Login or Register  to view this content.
    This code is copying data from another open workbook, then pasting into this one.
    If the other workbook is not open, i want to exit sub and show message box.

    It seems to be working, but i wanted to get a guru's opinion.
    Thx
    Dave
    "The game is afoot Watson"

  2. #2
    witek
    Guest

    Re: Error Handling - Did i do it correct

    Desert Piranha wrote:
    > Hi All,
    >
    > I know error handling can be tricky.
    > So i have this in some code i am working on. i just want to make sure i
    > am doing it correctly.
    >
    > I have this at the very top:
    >
    > Code:
    > --------------------
    > On Error GoTo ErrHandler:
    > --------------------
    >
    >
    > This is at the very bottom"
    >
    > Code:
    > --------------------
    > Exit Sub
    > ErrHandler: ActiveSheet.Protect
    > MsgBox ("You did not open ''UCPSITE-06.xls''")
    > --------------------
    >
    > This code is copying data from another open workbook, then pasting into
    > this one.
    > If the other workbook is not open, i want to exit sub and show message
    > box.
    >
    > It seems to be working, but i wanted to get a guru's opinion.
    >
    >



    OK

  3. #3
    NickHK
    Guest

    Re: Error Handling - Did i do it correct

    The line:
    On Error GoTo ErrHandler:
    creates an Active error handler. Errors "bubble up" through the stack until
    such an active error handler is found. If none is found, that when you get
    the VBA msgbox saying "Error: Subscript out range" or whatever and your code
    abruptly stops.
    See if the example below helps see the possiblities:

    Private Sub CommandButton1_Click()
    Call StartMacro
    End Sub

    Sub StartMacro()
    On Error GoTo Handler:
    Call Sub1
    Exit Sub
    Handler:
    'Select case Err.Number...etc
    MsgBox "Error handled in StartMacro" & vbNewLine & Err.Description
    End Sub

    Sub Sub1()
    On Error GoTo Handler
    Call Func1
    Worksheets(0).Select 'Create an Error
    Exit Sub
    Handler:
    Select Case Err.Number
    Case 11 'Division by 0
    MsgBox "Error handled in Sub1"
    Resume Next
    Case Else
    Dim errnum As Long
    MsgBox "Passing error up the stack"
    errnum = Err.Number
    Err.Raise errnum, "Sub1", "Not 1/0 error"
    End Select
    End Sub

    Function Func1() As Long
    'No error handling in this function
    Func1 = 1 / 0 'Create an Error
    End Function

    NickHK

    "Desert Piranha"
    <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi All,
    >
    > I know error handling can be tricky.
    > So i have this in some code i am working on. i just want to make sure i
    > am doing it correctly.
    >
    > I have this at the very top:
    >
    > Code:
    > --------------------
    > On Error GoTo ErrHandler:
    > --------------------
    >
    >
    > This is at the very bottom"
    >
    > Code:
    > --------------------
    > Exit Sub
    > ErrHandler: ActiveSheet.Protect
    > MsgBox ("You did not open ''UCPSITE-06.xls''")
    > --------------------
    >
    > This code is copying data from another open workbook, then pasting into
    > this one.
    > If the other workbook is not open, i want to exit sub and show message
    > box.
    >
    > It seems to be working, but i wanted to get a guru's opinion.
    >
    >
    > --
    > Desert Piranha
    >
    >
    > ------------------------------------------------------------------------
    > Desert Piranha's Profile:

    http://www.excelforum.com/member.php...o&userid=28934
    > View this thread: http://www.excelforum.com/showthread...hreadid=560960
    >




  4. #4
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256
    Quote Originally Posted by NickHK
    The line:
    On Error GoTo ErrHandler:
    creates an Active error handler. Errors "bubble up" through the stack until
    such an active error handler is found. If none is found, that when you get
    the VBA msgbox saying "Error: Subscript out range" or whatever and your code
    abruptly stops.
    See if the example below helps see the possiblities:

    Private Sub CommandButton1_Click()
    Call StartMacro
    End Sub

    Sub StartMacro()
    On Error GoTo Handler:
    Call Sub1
    Exit Sub
    Handler:
    'Select case Err.Number...etc
    MsgBox "Error handled in StartMacro" & vbNewLine & Err.Description
    End Sub

    Sub Sub1()
    On Error GoTo Handler
    Call Func1
    Worksheets(0).Select 'Create an Error
    Exit Sub
    Handler:
    Select Case Err.Number
    Case 11 'Division by 0
    MsgBox "Error handled in Sub1"
    Resume Next
    Case Else
    Dim errnum As Long
    MsgBox "Passing error up the stack"
    errnum = Err.Number
    Err.Raise errnum, "Sub1", "Not 1/0 error"
    End Select
    End Sub

    Function Func1() As Long
    'No error handling in this function
    Func1 = 1 / 0 'Create an Error
    End Function

    NickHK
    Hi NickHK,

    WOW

    Thank you so much for this. Very cool.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1