Tutorial 11 Connecting to External Data Excel EX 707 Retrieving Live Stock Quotes from the Internet Many financial applications need up-to-the-minute market data. Excel workbooks that display stock data need a way of downloading this information in a timely fashion. Many websites provide this service for a fee. There are also websites that provide stock market data for free, but slightly delayed from the fee-based services. One such website is Yahoo Finance located at the URL http://finance.yahoo.com. Excel can retrieve data from these kinds of web services uses the following function: WEBSERVICE(url) where url is the URL of the Web service from which you want to retrieve data. The URL needs to include tags that indicate what kind of data needs to be retrieved from the website. The actual tags are supplied by the website and will vary from one website to another. For example the following URL retrieves the most current value of the Hewlett- Packard stock (HPQ) from the Yahoo Finance website: http://finance.yahoo.com/d/?s=hpq&f=l1 To display this data in a worksheet cell, you place the URL within the following Excel function: =WEBSERVICE("http://finance.yahoo.com/d/?s=HPQ&f=l1") A more general approach would be to use a stock symbol entered into a worksheet cell rather than entered directly into the text of the URL. If the ticker symbol is entered into cell A5, you can reference that cell by making sure to enclose the beginning and end of the URL string in quotation marks and combining the text strings using the & symbol. The revised formula to return the last trading price of the stock symbol listed in cell A5 is, therefore: =WEBSERVICE("http://finance.yahoo.com/d/?s="&A5&"&f=l1") Stock values retrieved through the WEBSERVICE function do not act like web queries and are not treated as data connections. For example, they are not refreshed automatically when the workbook is reopened. The only way to refresh the values returned by the function is to recalculate the worksheet by selecting a worksheet cell and pressing the Enter key or by pressing the F9 key. Rafael wants to use the WEBSERVICE function to retrieve current stock values for the stocks listed in the Chalcedony Fund portfolio. He would like to display the company name, the current stock price, the change in price, the percent change in price, and the volume of shares traded. You can retrieve this information from Yahoo Finance by using the URLs shown in Figure 11-41 (assuming that the ticker symbol is entered into cell A5). C8367 Tut_11.indd 707
EX 708 Excel Tutorial 11 Connecting to External Data Figure 11-41 WEBSERVICE functions to retrieve stock quotes You ll use the WEBSERVICE function now to retrieve timely data on the stocks in the Chalcedony Fund. First you ll set up the worksheet. To enter the ticker symbols and column titles: 1. Go to the Portfolio worksheet and copy the column heading and the ticker symbols in the range A4:A19. 2. Go to the Current Values worksheet, click cell A4, click the Paste arrow in the Clipboard group on the HOME tab, and then click Paste Values in the list of paste options. Excel pastes the column heading and the ticker symbols with no formatting. 3. Enter Company Name in cell B4, Current Price in cell C4, Change in Price in cell D4, % Change in Price in cell E4, and Volume in cell F4. 4. Resize the column widths to accommodate the text of the column headings, as shown in Figure 11-42. C8367 Tut_11.indd 708
Tutorial 11 Connecting to External Data Excel EX 709 Figure 11-42 Headings in the Current Values worksheet column headings entered Next you ll insert the WEBSERVICE function in the first row of the table to retrieve stock data for the ticker symbol in cell A5. You can then copy the formulas from row 5 into the rest of the worksheet table. To enter the WEBSERVICE function: 1. Click cell B5 and enter the following formula to retrieve the company name from Yahoo Finance: =WEBSERVICE("http://finance.yahoo.com/d/?s="&A5&"&f=n") 2. In cell C5 enter the following formula to retrieve the current stock price: =WEBSERVICE("http://finance.yahoo.com/d/?s="&A5&"&f=l1") 3. In cell D5 enter the following formula to retrieve the change in the stock price: =WEBSERVICE("http://finance.yahoo.com/d/?s="&A5&"&f=c1") 4. In cell E5 enter the following formula to retrieve the percent change in the stock price: =WEBSERVICE("http://finance.yahoo.com/d/?s="&A5&"&f=p2") 5. In cell F5 enter the following formula to retrieve the volume of shares traded: =WEBSERVICE("http://finance.yahoo.com/d/?s="&A5&"&f=v") Figure 11-43 shows the stock values for the first stock listed in the table. C8367 Tut_11.indd 709 6/5/14 1:57 PM
EX 710 Excel Tutorial 11 Connecting to External Data Figure 11-43 Current stock quotes for Hewlett-Packard stock (HPQ) the values in your worksheet will be different Trouble? If Excel returns an error message, check your formulas and make sure that you have not missed a quotation mark in the URL. Note that your values will not match the ones shown in Figure 11-43 because your quoted values will be retrieved at a different point in time. 6. Display the current values for the rest of the stocks by copying the formulas in the range B5:F5 into the range B6:F19. Deselect the range by clicking cell A4. Figure 11-44 shows the current stock quotes for all of the stocks listed in the Chalcedony Fund. Figure 11-44 Current stock quotes for stocks in the fund C8367 Tut_11.indd 710 6/5/14 1:57 PM
Tutorial 11 Connecting to External Data Excel EX 711 The WEBSERVICE function provides important information to Rafael about the stocks in the portfolio. For example, he can see that the value of the Hewlett-Packard stock has declined by 0.28% since the market closed the previous day. On the other hand, the value of the Boeing Company stock has increased by 0.17% from the previous day. More than 90 different stock statistics can be retrieved from the Yahoo Finance website. To view the description of these different statistics and the URLs required to retrieve them, you can view documentation on the Yahoo Finance website. Retrieving Data Using Office Apps Another way to retrieve live data in your Excel workbook is through an Office app, which is a small specialized program that enhances the features of Excel, Word, PowerPoint, and other programs in the Office suite. There are apps to retrieve stock market data, weather information, and maps among other data sources. Apps are downloaded from the Microsoft Office Store. Most apps are free. To download and use an Office app, you need an account with Microsoft Office. The account is free, requiring only a username and password to set it up. Once you have an account, you can add an app to your account and then download it to Excel. You can sign up and retrieve an app by clicking the Store button in the Apps group on the INSERT tab. You can view a list of your apps by clicking the My Apps button in the Apps group. Be aware that apps are always being added and removed from the Microsoft Office Store, and Microsoft is not responsible for the quality and performance of third-party apps. Importing Data from XML Another important data source for Excel workbooks is XML documents. XML (the Extensible Markup Language) is a language used to create structured documents using only text. Because an XML document is a simple text file, a wide variety of programs and applications can read it. In fact, the internal code for Microsoft Office documents is saved in an XML format called the Office Open XML. XML documents are also easy to transfer over the Internet, which makes them ideal for web-based applications. Figure 11-45 shows an example of an XML document named Documentation created by a programmer at Chesterton Financial. C8367 Tut_11.indd 711