+ Reply to Thread
Results 1 to 10 of 10

Error Handling in a Loop

  1. #1
    Registered User
    Join Date
    10-06-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    23

    Error Handling in a Loop

    Dear Gurus,

    I recently encountered this issue when writing an error handling section inside a For loop (VBA for Excel). It seems that the On Error status resets to GoTo 0 after the first time an error has been handled.

    To illustrate this, in the following situation, I expected 5 Message Boxes but I only get 1. After the error handler activates the next loop, the On Error GoTo NextFor seems to have lost its meaning. My purpose is to have a "On Error GoTo Next Loop" mechanism in place. Could you please help?

    Please Login or Register  to view this content.
    Last edited by amdk8800; 09-22-2010 at 07:28 PM. Reason: Problem solved

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Error Handling in a Loop

    When you branch on error the branch doesn't include a resume or exit statement, so the error handler is still active when you go into the next iteration of the loop and the on error statement doesn't handle errors in an active error handler routine.

    Try using this code instead...

    Please Login or Register  to view this content.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Error Handling in a Loop

    maybe
    Please Login or Register  to view this content.
    Without the xit sub line i got 6 messages
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Error Handling in a Loop

    Call me Captain Picky, but I think that as good practice you should always have your error handling routines separated from the main sub with an Exit Sub, i.e.

    Please Login or Register  to view this content.
    Anything else means that you run the risk that your sub will process the error handling routine without an error being present, leading to some sort of cludge to get around the issues that raises.

  5. #5
    Registered User
    Join Date
    10-06-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Error Handling in a Loop

    Hi Guys,

    Thanks for all your prompt inputs. However, I am still stuck as my purpose is to get the script to totally ignore the rest of the current loop as soon as an error has occurred, and jump to the next loop. The code below helps to explain my point.

    Please Login or Register  to view this content.
    Last edited by amdk8800; 09-22-2010 at 07:06 PM.

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

    Re: Error Handling in a Loop

    You can handle errors in line:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    10-06-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Error Handling in a Loop

    Thanks shg. I think you have solved my problem. With little modification from your code, I got it to skip the current loop if an error happens. Though this is only suitable if you know exactly where the error is likely to occur. For my purpose, this suffices. Thanks again everyone for your valuable contributions.

    Please Login or Register  to view this content.
    Last edited by amdk8800; 09-22-2010 at 07:26 PM.

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

    Re: Error Handling in a Loop

    There's no need for a GoTo to do that, but I'm happy if you're happy.

  9. #9
    Registered User
    Join Date
    04-28-2013
    Location
    New Delhi India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Error Handling in a Loop

    I have a similar Problem. I need to download the latest available zip file with stock prices for a trading day from an online resource. The files are stored online in the format eq[ddmmyy]_csv.zip where the ddmmyy is the date of the trading day that the data pertains to. There will be no files for non-trading days. I therefore created a loop to start with the current date and iterate down one day at a time till a valid file is found. As the URLDownloadToFile did not generate any error whether or not the file was available on line I used a On Error GoTo error handler to return to the loop if an error was generated when the file was unzipped.

    The problem is the code works if the available online file is of the current date or one day old. If more than that , on the second iteration I get a 1004 run-time error and the error handler does not seem to capture the error. Any Help would be much appreciated

    Please Login or Register  to view this content.
    Last edited by uniquelydeep; 04-28-2013 at 02:19 PM.

  10. #10
    Registered User
    Join Date
    04-28-2013
    Location
    New Delhi India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Error Handling in a Loop

    I found more issues with the code on further debugging and hence started a new thread... http://www.excelforum.com/excel-prog...00#post3217200

    THank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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