Link to home
Start Free TrialLog in
Avatar of hypercube
hypercubeFlag for United States of America

asked on

Excel QueryTable from WebTable

From:
http://msdn.microsoft.com/en-us/library/office/aa203721(v=office.11).aspx#odc_xlwebquery_retrievingdatafromurls

"You can copy the third line from any Web query [.iqy] file and use it as the URL."

But I can't find any .iqy files on my computer after creating a Get External Data / From Web query that gets a table.

I'm trying to construct vba code that will do the same thing exactly when run.

If I copy the URL from the Connection that's created then instead of getting the table intended, it appears to grab stuff from the entire page.  

e.g. https://finance.yahoo.com/q?s=aapl&uhb=uhb2&type=2button&fr=uh3_finance_vert_gs
The table that's grabbed with the Get External Data connection is the table of 7 items in the mid-left part of the page starting with Prev. Close.
Avatar of Qlemo
Qlemo
Flag of Germany image

Instead of trying to do all manually, start with inserting a Web Query result manually, including marking the correct table, and then read the properties of the QueryTable via VBA debugger.
The case you describe results from not having selected any specific table in the HTML stream, so the whole page is used.
Avatar of hypercube

ASKER

Qlemo: That sounds like a good idea but I've already tried that.  And just confirmed with a blank spreadsheet by adding an External Source Web connection.  There is *no* vba generated as a result.
Also, I followed verbatim the instructions for vba code on the cited msdn link that I originally posted.  The result was supposed to be the MSFT stock quote with 3 items only.  Instead, doing that results in a similar outcome, I get tables from the page instead of the intended table.  
Now, I believe that some of that was due to the fact that the URL had changed, etc. but it remains an obscure connection between those query structures and the vba code I need.
SOLUTION
Avatar of hypercube
hypercube
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks I'll try it.
OK.  So now I'm looking at the code you offered and I see something that I've certainly seen before but now want to understand better:
The "With" is followed by a series of things that start with "dot"; like
.webtables = 1

Open in new window

What is the vba language terminology for a list like this?
I imagine that all of these are objects of the QueryTable but where do you find all these things and what they mean?  Another example:
.RefreshStyle = xlOverwriteCells

Open in new window

I have a couple of good books on vba (which are terrible re: QueryTables) and I can access the MSDN but this level of detail escapes any documentation I've found.  So a pointer or two in that direction would be very helpful.
*****
OK.  So now I'm getting some success by modifying the .WebTables value with 1,2,3,4, etc. and am able to get the data I'm looking for.
THANKS!
But, it appears that each time the worksheet is updated using this vba code, a *new* connection is created.  That seems a very bad idea so how might I prevent that?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!