+ Reply to Thread
Results 1 to 4 of 4

Using Error handling within a Loop

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

    Using Error handling within a Loop

    I had posted this already in another thread but realised after further debugging that the problems in my code were multiple so am starting a fresh thread.Link to the original post is given below. My requirement is this
    1. The most recent trading data is posted as a single zip file on an online resource. The file name is in the format "eq290413_csv.zip" where the numbers are the date of the trading day that data pertains to in ddmmyy format. No data is published on non-trading days.
    2. I need to download this data and update my excel sheet. As the mane of the file is dynamic, I use a loop to start with the current date and iterate down one day at a time till I find a matching file to download.
    3. I need to unzip this file and extract the csv file.
    4.I need to update my excel sheet with the data in the CSV file.

    The code works fine if the current file is the same as the current date or one day old. If older I get a run-time error 1004 with the msg "excel cannot find the text file to refresh this external data range". During Debugging the problems that I encountered in the code are as given below

    1. I expect the code "iRet = URLDownloadToFile(0, StrURL, strPath, 0, 0)" to return a value of 0 only if the required zip file is available on the online resource and therefore the loop should continue at this level till the correct file is downloaded. This doesn't happen and a dummy zip file with no data is created even when there is no such file available at the online resource. Can anybody help so that the looping can occur at this stage.

    2. In order to circumvent I added the "On Error GoTo" error handler to bring the execution back to the loop when the extraction of the zipped file returns an error. However during the debugging process I noticed that there seemed to be no error generated at this stage also and the execution moves out of the loop without having downloaded a valid file.

    3. In the updating of the worksheet step, during the first iteration the error handler takes it to the error handling label and the loop is restarted. However on the second iteration a run-time error 1004 with the msg "excel cannot find the text file to refresh this external data range" is generated and the error handler does not take it to the error handling label. The break point is at the ".Refresh BackgroundQuery:=False" code line of the DoFileQuery Function.

    Any help on why this unexpected behaviour would be helpful. I would like the loop to finish at the file download level so that once the correct zip file is downloaded the rest of the extraction and updating the worksheet part of the code need run only once.


    Please Login or Register  to view this content.
    http://www.excelforum.com/excel-prog...in-a-loop.html

  2. #2
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Using Error handling within a Loop

    1. Test the length of the downloaded file and don't unzip if short. Then no error (the error is when you unzip, correct?)
    2. If there still is error handling, Resume Next looks okay. Eek, a goto other than in On Error statement!
    3. I don't know why it gives a different outcome on the 2nd try, but hopefully you avert the error to begin with now.
    Chip, if you can read this, you're STILL making the world a better place with your immeasurably valuable gifts. Every week, you continue to help me (and untold numbers of others) tremendously. Practically no alternative outlets can match your effectiveness, even over the vast number of topics you demystify and empower usage of. You were, and still are, amazing.

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

    Re: Using Error handling within a Loop

    Thank you oppressed. I have managed to get the code running by changing the <GoTo Reloop:> in the <Download Error:> Label to <Resume Reloop:> It was probably that after the first On Error GoTo statement was activated, the code continued to be in an unresolved error state and hence the error in the next iteration was not handled.
    It however takes a long time as it goes through the process of download, unzip and trying to import from the dummy CSV files before the error handler captures the error and it goes back to reloop for the next iteration. Is there anyway that I can loop if the file that I am trying to download is not available at the online resource. I don't understand how a dummy file is created if no such file exists online. I am reposting the loop code as I originally conceived it which if I can get to work would speed up and unclutter the code. What I want is if the current date is Apr 29 and if the file on the online resource is of Apr 26 (eq260413_CSV.zip), then my loop should go through 4 iterations (3 no file msgs and one file downloaded msg) and download the file eq260413_CSV.zip. Whereas in the current form the <URLDownloadToFile> function just creates a dummy file eq290413_CSV.zip with no data in the first pass and returns a value of 0 to iRet whereby exiting the loop. Any thoughts....

    Please Login or Register  to view this content.

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

    Re: Using Error handling within a Loop

    Found the solution to my problem posting the link...
    http://stackoverflow.com/questions/1...=votes#tab-top

+ 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