+ Reply to Thread
Results 1 to 8 of 8

VBA Web Query Looping multiple outcomes

  1. #1
    Registered User
    Join Date
    12-03-2009
    Location
    St. Louis
    MS-Off Ver
    2003
    Posts
    15

    VBA Web Query Looping multiple outcomes

    So I have a web query that I would like to loop to a specified range. The catch is there are two possible outcomes. There is an outcome (data) and an outcome that is useless (no such page is found, etc). I'm kind of stuck trying to get this thing to loop. I would like to reference a range (contains links for input), and input all the data from the range, and based on the outcome, input the useful webpage data into the same spreadsheet.

    Here is where I'm starting out.
    Please Login or Register  to view this content.
    Any help is greatly appreciated.

    -huey
    Last edited by Leith Ross; 12-09-2009 at 04:06 PM. Reason: Added Code Tags

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: VBA Web Query Looping multiple outcomes

    Try something like this:
    Please Login or Register  to view this content.
    The code above only handles the outcome when there is data retrieved from the web page: it assumes that if A1 on Sheet3 (the web query sheet) contains the text "Location Information" data then is available and is therefore copied. This assumption might be wrong.

    If you provide some example URLs for a 'useless' outcome (e.g. no such page found, you say), I can modify the code to handle this too. For such cases, if the web query fails (errors out), then an On Error handler would be needed, otherwise if it retrieves data then a simple 'If ... Then' test would be used.

    Note - for my testing, Sheet2 A1:A4 contain the same full URL:
    Please Login or Register  to view this content.
    Last edited by Chippy; 12-08-2009 at 06:32 PM.

  3. #3
    Registered User
    Join Date
    12-03-2009
    Location
    St. Louis
    MS-Off Ver
    2003
    Posts
    15

    Re: VBA Web Query Looping multiple outcomes

    I'm sorry for not being accurate in my description. The page actually says "Location with key "xxxxx" not found." Is there a way to advert the error through an if statement, or is it just easier to create an error handler?

    Example:

    http://store.publix.com/publix/cgi/s...pid=us&region=
    http://store.publix.com/publix/cgi/s...pid=us&region=

    Since this is going to be run on about 1500 cells, what lines should I use to avoid problems with timeouts?
    Last edited by huey; 12-09-2009 at 03:11 PM.

  4. #4
    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: VBA Web Query Looping multiple outcomes

    Hello huey,

    Welcome to the Forum!

    To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code
    1. Select all your code using the mouse.
    2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.
    3. To do it manually...
    Use the Bulletin Board Code Tags
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] at the end of the last line.


    To learn more about BB codes used in this forum, just click on the link below...

    Bulletin Board Codes Tags
    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!)

  5. #5
    Registered User
    Join Date
    12-03-2009
    Location
    St. Louis
    MS-Off Ver
    2003
    Posts
    15

    Re: VBA Web Query Looping multiple outcomes

    Quote Originally Posted by Leith Ross View Post
    Hello huey,

    Welcome to the Forum!

    To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code
    1. Select all your code using the mouse.
    2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.
    3. To do it manually...
    Use the Bulletin Board Code Tags
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] at the end of the last line.


    To learn more about BB codes used in this forum, just click on the link below...

    Bulletin Board Codes Tags
    Sorry and thanks.

  6. #6
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: VBA Web Query Looping multiple outcomes

    Try this code, which handles 'good' and 'bad' data outcomes. Put the code in a new workbook in a sheet module or a standard module (it should work in either) and run the Test macro.
    Please Login or Register  to view this content.
    The data input/output (location of URLs, columns, data copied etc.) might not be to your requirements, however it should be easy for you to modify.

  7. #7
    Registered User
    Join Date
    12-03-2009
    Location
    St. Louis
    MS-Off Ver
    2003
    Posts
    15

    Re: VBA Web Query Looping multiple outcomes

    The site has a limit on the # of queries per minute.
    Last edited by huey; 12-10-2009 at 07:17 PM.

  8. #8
    Registered User
    Join Date
    12-03-2009
    Location
    St. Louis
    MS-Off Ver
    2003
    Posts
    15

    Re: VBA Web Query Looping multiple outcomes

    Quote Originally Posted by Chippy View Post
    Try this code, which handles 'good' and 'bad' data outcomes. Put the code in a new workbook in a sheet module or a standard module (it should work in either) and run the Test macro.
    Please Login or Register  to view this content.
    The data input/output (location of URLs, columns, data copied etc.) might not be to your requirements, however it should be easy for you to modify.
    What's interesting is that the query is producing an error in every cell on every request, even though there is "good" data from the page.
    Last edited by huey; 12-10-2009 at 08:22 PM.

+ 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