|
Microsoft Office Application Development
|
|
Using Parameters With Web QueriesIntroductionExcel provides a very useful option to gather data from websites, called web queries. These have been introduced with Excel 97 and have been further enhanced in the versions released after Excel 97. This article shows how you can setup a web query so that you can make the worksheet dynamically update based on values in so called parameter cells. Setting up the web querySetting up a web query is as simple as choosing Data, Get External data, New
Web Query. In the next dialog, you enter the URL of the site you want to extract data from. For example: To make part of the url work as a parameter, replace that part of the url with some text between quotes and square brackets (this will only work when using the URL would open a normal page in a web browser): Excel will interpret the part between the square brackets as a parameter and prompt you for a value once you click the Import button or when you refresh the query table. The string you entered will be used as both the name of the parameter and the prompt:
The fun thing is, that you can either fill in a value or you can select a cell as an input for the parameter:
By checking the two checkboxes shown above, you ensure that Excel will automatically update the query table when you change the content of the cell(s) you selected. Working With Web Query Parameters In VBAExcel VBA offers the programmer access to web query parameters through the Parameters collection of Parameter objects. Unlike "normal" database queries, it is not possible to add a parameter to the Parameters collection using the Add method (though even for that type of query, doing so is an awkward process). Instead one has to add all parameters one needs to the Connection string. Excel will determine the number of parameters from that string once it has been applied to the Connection property and then the Parameters collection becomes available to VBA. In the example below, a web query is added to a worksheet. The message box shows, that there are no parameters: Sub Demo() Result: If the code shown above is changed to add a parameter in the connection string, things change: Sub Demo() Now the messagebox shows:
Note that in the code sample the parameter has been tied to a cell (SetParam method) and that the query is set to update when that cell changes (RefreshOnChange property set to True). The SetParam method is the only way to change the setting of the parameter object from e.g. prompt to Range and to change the range the query parameter uses as its source. ConclusionAs this article shows, adding a parameter to a web query is relatively easy. The tricky part is the fact that you need to know that parameters can only be added through the connect string (the URL) by using a very specific syntax and that parameters can only be added by changing the connection string. FeedbackSince you have managed to reach the end of this article, maybe you would care to write me a small message, expressing your opinion on this article? Click here to write an email message to me. You may also rate this article: (Rated: 351 times, average rating: 6.3)![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
CommentsShowing last 15 comments of 54 in total (Show All Comments):Comment by: Jan Karel Pieterse (9/11/2007 10:08:24 AM)Hi Surya, I have built a solution for a customer once which is very similar to what you are asking here. Get back to mme if you're interested in me developing that for you commercially. Comment by: Omar (9/25/2007 7:28:55 PM)Is there any similar aplication on MS Access, without using VBA??? Even MS Access is more powerful than MS Excel, the last one is pretty friendly and easy to use! Comment by: Kumar (12/3/2007 9:34:40 AM)I think I'll benefit from the setParam method. Will visit your site again and refer others. Thanks! Comment by: Pete (12/11/2007 1:44:02 PM)Please help me on this. Working off of a list of zip codes in an excel 2007 column, I want a web query that can take go to the usps.com website and return the state (or city and state) for each of the zip codes. Thanks. Comment by: Jan Karel Pieterse (12/11/2007 10:22:45 PM)Hi Pete, The only way is by setting up a separate web query for each cell. Once you've set up one web query as I've shown on this page, you CAN copy that web query down to other cells. You must however change the parameter cell of each copied query manually by selecting the query cell and clicking the parameters button on the external data toolbar. Comment by: Pete (12/19/2007 1:02:57 PM)I'm not sure how to set up even one web query in this case, since the URL does not include the zip code. Please look at the website before answering. Can you show me specifically how to set this us. Thanks. Comment by: Jan Karel Pieterse (12/20/2007 11:00:04 AM)Hi Pete, You did not include the url? Comment by: Mike Carroll (1/2/2008 3:27:22 PM)Hi Jan, Your comment on 12/11/2007 agrees with what I have found so far: "the only way is by setting up a web query for each cell." This is strange if true. It means that each time you have a different parameter (or set of parameters), you need a different query. In other programming contexts, you can use the same query with different parameters. It seems as though web queries with parameters are not quite finished yet, in Excel 2007. Comment by: swapnil (1/18/2008 9:29:01 AM)I am trying to setup a web query using a parameter. The web url is http://in.finance.yahoo.com/q?s=INFY&m=NS . This works fine when I import into the sheet. Now how to change the stock code INFY to a parameter. If I try to edit the query and enter ["INFY"] then the import does not work. Thanks a lot! Comment by: Jan Karel Pieterse (1/18/2008 10:04:02 AM)Hi swapnil, These steps do it: - Insert your query using the fixed URL. - Select a cell within the resulting table - Open the visual basic editor - hit control+g - paste this line: activecell.QueryTable.Connection="URL;http://in.finance.yahoo.com/q?s=[""Param""]&m=NS - put cursor in that line and hit enter. - go back to Excel - now the parameters button on the external data toolbar should be visible - click it and set the parameter to get its value from a worksheet cell. Comment by: swapnil (1/18/2008 11:23:43 AM)Thank you very much! It worked perfectly. Comment by: Juan (3/5/2008 12:32:06 PM)the history data that i down to my excel query are on CVS. How can i converter this datas for what excel read as a real number and to CSV Thanks for you help Comment by: pauloulu (4/9/2008 3:31:22 AM)I am new to this forum and I think you all will help me to enhance my knowledge. I have been here many times and have always found this to be a great place for information and advice about a wide range of topics Paul Comment by: Don (4/29/2008 11:00:55 AM)Great information! How do you query a website with a URL address that does not change when selecting options (drop down buttons) within the page? The page content changes according to the selection but the URL remains the same? Thanks in advance. Comment by: Jan Karel Pieterse (5/4/2008 11:59:59 AM)Hi Don, I'm afraid you cannot do that using this technique. Add a comment too!!! | ||||||||||||||
Use the contact page to issue
questions or comments about this website. |