+ Reply to Thread
Results 1 to 15 of 15

Coding Convention Questions #1 (On Error Goto 0 before End Sub)

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Question Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    I'm mainly self taught at VBA so I make notes on various coding conventions/practices etc. that I pick up from books & websites.
    I've noticed that some of these conventions conflict with one another or don't appear to have a logical rationale upon closer inspection.
    So I figure that I would pose some of these to the Forum so I can be taught by others far more skilled and experienced than I.

    Here's the first one:

    I've come across a coding convention where you place
    Please Login or Register  to view this content.
    before the procedure exit/end.

    I started to take this practice up but when I recently questioned the logic for doing so, I couldn't work out why you would do this.

    Error Handling takes place at a procedure level. When a procedure ends, the error handling for that procedure resets so why would you need to reset it?

    Or am I overlooking something big?
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Have a read of Error Handling in VBA by Chip Pearson - it might clear up any confusion...

    While there, have a look at the Topic Index, you should find a few interesting pages.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Quote Originally Posted by cytop View Post
    Have a read of Error Handling in VBA by Chip Pearson - it might clear up any confusion...

    While there, have a look at the Topic Index, you should find a few interesting pages.
    I had a look at the page and I can't see any mention of using
    Please Login or Register  to view this content.
    before
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Correct me if my understanding is wrong.
    1. Error handling takes place at a procedure level.
    2. If a procedure doesn't contain error handling, it throws back to the calling procedure before it.
    3. This repeats until it reaches a calling procedure that does have error handling - and then uses that.
    4. If the entire chain doesn't contain error handling or if the procedure was 'stand alone' and didn't contain error handling then you get the standard VBA Error Msgbox.

    If I have the above correct then why would you want/need to place
    Please Login or Register  to view this content.
    before exiting a procedure?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    It resets any current error.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    And disables the enabled error handler for that procedure.

    But is this necessary if the next line is Exit/End Sub?

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Hello mc84excel,

    If your error handler is not jumping back to a particular line in the procedure and will exit the procedure when finished, the On Error GoTo 0 is redundant and not needed.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Quote Originally Posted by Leith Ross View Post
    If your error handler is not jumping back to a particular line in the procedure and will exit the procedure when finished, the On Error GoTo 0 is redundant and not needed.
    Thanks. That's what I suspected.


    On a side note: If the error handler DOES jump back to the procedure, (rough example below)
    Please Login or Register  to view this content.
    then I take it I would need an
    Please Login or Register  to view this content.
    before
    Please Login or Register  to view this content.
    in the ErrHandler? (Assuming I want to keep using the same error handler for that procedure)
    Last edited by mc84excel; 02-04-2014 at 09:28 PM.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Hello mc84excel,,

    You're welcome.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Hello mc84excel,

    You can use either Err.Clear or On Error GoTo 0 to clear the error. You only need to clear the error if you intended to trap the error again during code execution. There may be times were you expect a certain error to occur and it can be ignored. In this case, you can resume without clearing the error.

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Thanks again.


    (Since I want to reuse the error handler in the procedure, I will use
    Please Login or Register  to view this content.
    rather than
    Please Login or Register  to view this content.
    )

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Please Login or Register  to view this content.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    Hello mc84excel,

    You're welcome.

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    @ shg. If I get what you are saying - Errors are not automatically cleared at procedure exit/end (although the error handler itself is cleared at procedure exit/end). If there was a calling procedure to the procedure that encountered the error, the error number will 'throw back' up the call stack. But if you had
    Please Login or Register  to view this content.
    before the procedure exit/end, then not only is the procedure err handler disabled (which isn't necessary as we are leaving the procedure), it also resets the error to 0. But why do we want to do this?

    If you are of the "I prefer error throw back to top level procedure" school (which I'm not) then using this before the exit point erases the error so the top level will never capture what went wrong.

    If you are of the "handle the error in the procedure that encounters it" then we would have already run our ErrHandler code.


    Please Login or Register  to view this content.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Coding Convention Questions #1 (On Error Goto 0 before End Sub)

    I wasn't expressing an opinion, just answering the question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Color coding certain rows based on answers to questions?
    By brittany182 in forum Excel General
    Replies: 1
    Last Post: 02-22-2013, 04:22 PM
  2. vba coding... IF, Then, Goto "multi workbooks"
    By Domintor2992 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-25-2009, 10:45 AM
  3. [SOLVED] Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM
  4. Coding questions about UserForm TextBox
    By excelnut1954 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-13-2006, 11:10 AM
  5. [SOLVED] Questions concerning VBA coding
    By Sloth in forum Excel General
    Replies: 5
    Last Post: 12-20-2005, 12:10 PM

Tags for this Thread

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