TechRepublic : A ZDNet Tech Community

Pull data into Microsoft Excel with Web queries

Tags: Channel management, Justin James, Microsoft Corp., Web Query, Web, Microsoft Excel

  • Save
  • Print
  • Recommend
  • 11

Takeaway: Web queries offer a handy way to import data from selected tables into a worksheet--but Excel isn't as accommodating as it should be, especially when it comes to parameters. These tricks will let you work around Excel's limitations so that you can get the data you need.

An Excel Web query allows you to bring data from a Web site into an Excel worksheet. It will find any tables on the Web page and let you select the ones containing data you want to put into your worksheet, allowing for dynamic updates from the Web page. Web queries are not just useful for pulling information from standard HTML pages. They can also be used quite nicely in situations where a standard ODBC connection would be difficult or impossible to create or maintain, such as a worksheet that's used by salespeople around the country.

We're going to work through a basic example to get a feel for how Web queries operate. Then, we'll look at a couple of tricks that allow you to work around some Excel limitations and see how a little VBA code can give you the results you want. You can download this Excel workbook to see a sample implementation of these techniques.

Getting started


To demonstrate the process, we're going to start with a simple Web query using Yahoo! Finance historical stock prices. This is a great example because the data we're interested in is presented in a plain, tabular format. Another advantage is that the URL contains the stock symbol (GOOG, in this case), so it will be easy to manipulate via a VBA macro, and it has little confusing information in it. Finally, this Web query doesn't put important information in images or through links.

To create the Web query:

  1. Select the first cell in which you want results to appear.
  2. Choose Data | Import External Data | New Web Query to open the dialog box shown in Figure A.

Figure A

 
  1. Enter the URL to query in the Address area and click the Go button (Figure B).

Figure B

 
  1. Select the table you want to use for the query (Figure C).

Figure C

 
  1. Click the Import button.

That's it. The data is now in your worksheet (Figure D).

Figure D

 

Customizing the query


After you create a Web query, you can customize it to meet your needs. To access Web query properties, right-click on a cell in the query results and choose Edit Query. (You can also click Edit Query on the External Data toolbar or choose Data | Import External Data | Edit Query.) When the Web page you're querying appears, click the Options button in the upper-right corner of the window to open the dialog box shown in Figure E. The options here allow you change how the query interacts with the Web page itself.

Figure E

 

In addition, you have the same choice of Data Range options that you have with other external data queries, such as ODBC queries. Just right-click on a cell in your query results and choose Data Range Properties (or click Edit Query on the External Data toolbar or choose Data | Import External Data | Data Range Properties) to open the dialog box shown in Figure F. You'll probably want to change the Data Range's name from the default to a name you can easily access through a macro. In this example, we'll rename the Data Range to Stock Prices.

Figure F

 

Parameters


Unfortunately, working with parameters is not as straightforward as it could be. The wizard that created the sample query above doesn't allow you to put in parameters. When it fetches the Web page, it will escape the parameter identifiers in the URL and return a page without the right results. For many Web pages, this prevents you from selecting the table you want to use for the import, so you'll need to work around this Excel limitation. The Web query parameters let you use set values, get the values from a worksheet range, prompt the user for input, or set values programmatically via VBA macros.

One workaround is to create your query as outlined above and then run a VBA macro to dynamically change the Connection property of the query to provide the correct URL for Web pages that use the GET data. In this example, we could use code similar to Listing A .

For Web pages that use GET data, this is a perfectly fine solution. For Web pages that use POST data, this is not a solution. Those scenarios will require a little bit of manual labor. You'll need to open a text editor and create an IQY (Internet Query) file as a plain text file to use as the basis for your data import. The IQY file should have four lines:

Type of Query
Query Version
URL
POST Parameters

The Query Type line should just be WEB and the Query Version line can be whatever you want (1 is just fine). The URL line should be the URL itself. The parameters should be in the following format:

Parameter1=Value1&Parameter2=["Value 2", >"Please input a value for Parameter 2: "]

This is where you can work a couple of tricks. With that sample parameter line, "Value1" will be passed as the value for Parameter1, but the user will be prompted with the phrase "Please input a value for Parameter 2:" to provide the value for Parameter2. This works with GET as well as POST queries. The prompt text is optional.

In this example, we just need a single parameter--the stock symbol. So our Web Query file looks like this:

WEB
1
http://finance.yahoo.com/q/hp
s=["Stock Symbol"]

Once you've created the query file, you can use it in the worksheet. Choose Data | Import External Data | Import Data and point the Open dialog box to the query file you created. You'll be prompted to specify the stock symbol to be used. Your query results will then show up in the worksheet.

One final trick is to use this query file to fool the import wizard into allowing you to select the exact table of desired data and still use parameters. Once the query file has been loaded, you can edit the query in Excel. The query won't work because the editor won't pass our values along properly. It will, however, show you the Web page, and from there you can navigate or search or whatever you need to do to see the page with the table so you can select it. Once you've selected the table (just like creating a Web query through the wizard), you can edit the parameters to use the desired methodology (prompts, hard-coded values, or data from a worksheet range). You can also now set these values through VBA code.

Once the Web Query has been created from the file, you no longer need the file. It doesn't have to be distributed with the Excel worksheet.

Working with the query through VBA


The DataRange gets added to the QueryTables collection, which is a member of the Worksheet object, and can be referenced by index number or by the DataRange name. In our example, ThisWorkbook.Sheets("Web Query").QueryTables("Stock Prices") refers to our Web query. The most common use of VBA with the Web Query is to have its Refresh() method force the data to be refreshed from the source and reloaded into the worksheet. You can also use the SetParam() method on the Parameter property of the QueryTable object to manually set (or prompt the user to input) the value of the parameter (the Value property is read only). The sample piece of code in Listing B takes the stock symbol as a string, puts it into the parameter, and then updates the query's data.

  • Save
  • Print
  • Recommend
  • 11

Print/View all Posts Comments on this article

Pull data into Microsoft Excel with Web queries JodyGilbertTechrepublic Moderator | 09/15/06
No way to enter username and password babycody@... | 09/27/06
Edited sinopec_contact | 05/07/07
Passwords and web queries gbkrause@... | 07/21/07
historical chart of daily values stock prices venkat1926@... | 09/27/06
download historical data markamprimo@... | 11/23/06
And if the table is a few pages back? kennethscottbarry@... | 01/12/07
The article explains that Justin James | 01/12/07
Yes, I have ..... the question mark in URLs mike@... | 03/27/07
Problem with question mark also. davesayers75@... | 04/08/07
Exact code? Justin James | 04/08/07
That question mark - was it all in the mind? mike@... | 04/09/07
Glad it worked! Justin James | 04/09/07
Unconsisten Table Querry Errors FeldmanWill@... | 12/12/07
Question mark still a problem davesayers75@... | 04/09/07
That is really odd Justin James | 04/09/07
Yep. Any hints on AV? davesayers75@... | 04/10/07
No idea, sorry Justin James | 04/10/07
WebQuery URL table download problem cogen@... | 06/10/07
Solution to the "file could not be accessed" problem Leon Tribe | 08/13/07
Better solution Leon Tribe | 08/15/07
Accessing websites for which I have passwords mike@... | 04/09/07
That's a tough one Justin James | 04/09/07
My workaround BCVolkert | 11/30/08
Excel Stock - DIY Market Analysis William D | 04/16/07
Excel Macro susan_cecala@... | 04/29/07
Web Query Issues salesunlimited@... | 08/17/07
Cache memory = IE cache? Leon Tribe | 08/17/07
I have done that salesunlimited@... | 08/17/07
Help with web query PB-1 | 11/18/07
Ongoing Dynamic Web Query Dilemma PB-1 | 11/20/07
Code and responses do not match Justin James | 11/24/07
Reply to web query issue-ongoing PB-1 | 11/26/07
POST version Leon Tribe | 09/24/06
Correct, but there is a workaround Justin James | 09/24/06
It doesn't work for me unfortunately Leon Tribe | 09/24/06
You need to use the workaround Justin James | 09/24/06
Cracked it! Leon Tribe | 09/24/06
Better code Leon Tribe | 09/24/06
Good job! Justin James | 09/25/06
same problem- need a solution PB-1 | 11/18/07
Stop using Excel... Justin James | 11/19/07
Would like to use workaround - not macro - dynamic dandupras@... | 03/22/07
You really need it Justin James | 03/23/07
When Edit Query to obtain correct table - the IQY query is broken dandupras@... | 03/23/07
You might want to try... Justin James | 03/23/07
You might want to try... Justin James | 03/23/07
recreate msnmoney on yahoo mac_____daddy@... | 03/05/07
Batch processing Leon Tribe | 03/06/07
Batch processing mac_____daddy@... | 03/10/07
VB basics for Yahoo Leon Tribe | 03/10/07
Listing A & B abdu.elnagheeb@... | 03/22/07
Click the link Justin James | 03/22/07
Thanks, Justin abdu.elnagheeb@... | 03/23/07
Create MACD Indicator with EMAs Using VBA? jtreble@... | 05/01/07
MACD formula William D | 05/23/07
MACD Written in VBA? jtreble@... | 05/25/07
MACD Written in VBA William D | 06/01/07
but how to find first ema maahir.com@... | 06/01/08
First EMA William D | 06/24/08
RE: Pull data into Microsoft Excel with Web queries stewart.barker@... | 07/19/07
Pull data into Microsoft Excel with Web queries William D | 07/30/07
RE: Pull data into Microsoft Excel with Web queries dbyrt@... | 12/22/07
Splitting a string Leon Tribe | 12/23/07
RE: Pull data into Microsoft Excel with Web queries vijayasrani@... | 04/02/09
Nice Financial Analysis Template - HELP with update querries pipoman | 08/04/09

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

SmartPlanet

Click Here