+ Reply to Thread
Results 1 to 11 of 11

Handling error in VBA

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Handling error in VBA

    Hello everyone

    When I am using this line [On Error GoTo ErrHandler] to navigate to a specific point

    Please Login or Register  to view this content.
    I can move into the point just once but if the error occurs again, I got an error and the code stops. How can I make the code moves the point `ErrHandler` every time the error occurs?

    The question posted here too
    https://eileenslounge.com/viewtopic.php?f=30&t=39437
    Last edited by YasserKhalil; 03-16-2023 at 02:04 PM.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    Using Resume Next will jump back to next code after the code that cause error, if it is in a loop then it always back to error
    handler, if VBA found an error again.
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    Or you can also use Resume somelabel , to jump at any line that you want, but be careful with this, you need to fix some variable (for example in the code below, we need to increment i manually, or the code will run infinitely). You see, by jumping to LBL_JumpHere: we skip the rest of code after the error occurred (skip Here3 and Here4), and jump to the beginning of loop.

    Please Login or Register  to view this content.
    Last edited by karedog; 03-16-2023 at 02:28 PM. Reason: better illustration

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Handling error in VBA

    Thank you very much, my tutor.
    Best Regards

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    You are welcome Yasser, thanks for marking the thread as solved and rep.points given.

    Regards

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Handling error in VBA

    As for the second code, I tried to change a little but I encountered an error at the part of ErrHandler
    Please Login or Register  to view this content.

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    You are right, because we increment i and don't check for the ubound of the array, can fixed by adding this line :
    Please Login or Register  to view this content.

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    Or if there is some other code after the loop that must be run, we can make another label line after this For..Next block and jump to this point
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Handling error in VBA

    Thank you very much.

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    You are welcome

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Handling error in VBA

    You can also put the label to jump at the nearest point before the Next code line, this way the code still continue to process of the loop so we don't need to increment the variable manually. This could be useful for loop that don't use index (e.g. For Each..Next, Do..While, etc)

    Well, we have many variations to do this, All Roads Lead To Rome

    Please Login or Register  to view this content.

+ 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. Error Handling: Need simple code for Run-time Error
    By romsky26 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-28-2021, 08:59 AM
  2. [SOLVED] Odd Error Handling Behaviour; goes to error handler even when no error?
    By kalikj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-04-2018, 10:04 AM
  3. Error handling inside error handling
    By grantastley in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2015, 03:43 AM
  4. [SOLVED] Error Handling: Creating code to display error messages
    By Student1990 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-01-2013, 02:21 PM
  5. Error Handling: Can it report line # where error occurred?
    By PingPing in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2010, 03:47 AM
  6. 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
  7. Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM

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