+ Reply to Thread
Results 1 to 4 of 4

Use of ON ERROR GOTO 0

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    316

    Use of ON ERROR GOTO 0

    I am beginning to divest myself of some bad programming habits I sort of picked up during my rookie days years ago. Towards this end, I am now questioning the use of ON ERROR GOTO TO 0 to follow and disable ON ERROR RESUME NEXT as in:

    Sub test()
    On Error Resume Next
    Cells.SpecialCells(xlCellTypeFormulas, 23).Interior.color=vbyellow
    On Error GoTo 0
    End Sub

    The above code (as in others I have tried) works perfectly with or WITHOUT "ON ERROR GOTO 0".

    Now my question. Is ON ERROR GOTO TO 0 another of Visual Basic moribund lines of code which just clutter up space? Can't we do away with these vestiges?

    David

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning DavidM

    It depends on what you're programming for. Try copying your code into a blank sheet, and turn your two error trapping routines into comments and run your code. You'll probably find it falls over. Now, enable the On Error Resume Next command, and run the code. This time it will appear that nothing happens. Your code has found a problem but ignored it. if you are programming for other people you might want to trap specific errors and return an appropriately worded msbox, but I find the On Error Resume Next an easy way to avoid the inelegant VBA "runtime Error" messages.

    HTH

    DominicB

  3. #3
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    316
    Diminic,

    Thanks for your reply. I appreciate the use of error trapping, not least the ON ERROR RESUME NEXT. What I need clarification on is the usefulness of ON ERROR GOTO 0. Put another way, is there any need to turn off the error trap before exiting the code? A case of superfluity?


    David

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi David

    I personally never use the On Error Goto 0 command (and can't recall seeing any other VBA coder use it frequently), because once the routine has run there is no need for error trapping, so it does not need to be used as a matter of course - only if you deliberately need to turn error trapping off for some reason.

    HTH

    DominicB

+ 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