In this chapter
Connecting to a Worksheet in Another Workbook 800
Connecting to Data on a Web Page 805
Setting Up a Connection to a Text File 809
Setting Up a Connection to an Access Database 812
Setting Up SQL Server, XML, OLE DB, and ODBC Connections 813
In Chapters 20, “Understanding Formulas,” and 21, “Controlling Formulas,” you learned how to set up formulas that calculate based on values within one worksheet. It is also very easy to connect a worksheet to several other worksheets or to connect various workbooks. Excel 2007 offers easier-than-ever ways to connect a worksheet to data from the Web, data from text files, or data from databases such as Access.
In this chapter, you’ll learn how to do the following:
Although Excel 2007 offer 17 billion cells on every worksheet, it is fairly common to separate any model onto several different worksheets. You might choose to have one worksheet for each month in a year or to have one worksheet for each functional area of a business. For example, Figure 28.1 shows a workbook with worksheets for revenue and expenses. Because different departments might be responsible for the functional areas, it makes sense to separate them into different worksheets. Eventually, though, you will want to pull information from the various worksheets into a single summary worksheet.
To create a formula that pulls data from another worksheet, you enter an equals sign to begin the formula, as shown in Figure 28.2.
Rather than try to remember the exact syntax, you can simply point to the correct cell. So after you type the equals sign, click the desired worksheet tab. Using the mouse, click on a cell to get the value from that cell. In Figure 28.3, Excel builds the formula =Revenue!F6 in the formula bar. Excel waits for you to either press the Enter key to accept the formula or press another operator key in order to add other cells to the formula.
When you press the Enter key to accept the formula, Excel jumps back to the starting worksheet. The desired figure is carried through to the worksheet.
Note
The formula that Excel builds is a relative formula. You can easily copy B5 to B6 in order to retrieve the 2008 budget for revenue.
You continue this process until you have built a formula for each cell on the Summary worksheet. Note that it is okay to include an external cell as the argument for a function. Figure 28.4 shows the formula for each cell on the Summary worksheet.
In the examples in the previous section, the syntax is as follows:
=sheetname!celladdress
This syntax changes when one of the worksheet names contains a space. In this case, you must surround the worksheet name in apostrophes, like this (see Figure 28.5):
='sheet name'!celladdress
It is possible to set up links from a worksheet in one workbook to a worksheet in another workbook. Excel does an excellent job of managing these links and is even able to update values in the destination workbook when the source workbook is closed. It is easiest to create these links while both workbooks are open.
Say that a co-worker in another department maintains a workbook. You need to pull the sum of a few numbers from that workbook into your workbook. Your co-worker’s workbook is referred to as the source workbook and your workbook is referred to as the destination workbook. To set up a formula in the destination workbook, which pulls data from the source workbook, follow these steps:
Note
When you build a link from one worksheet to another worksheet in the same workbook, Excel defaults to using a relative reference for the cell. In Figure 28.8, note that Excel defaults to entering an absolute reference when the selected reference is in another workbook. At the point shown in the figure, you can press F4 three times to return to a relative reference.
There are some key syntax variations you must understand when creating links between worksheets and/or workbooks:
Build a formula that links to a source workbook where the source workbook has not been saved. This formula might point to Book1 or Book3 or a workbook such as that. Attempt to save the destination workbook. Figure 28.9 shows a link to a new Book3 workbook. When you attempt to save the destination workbook, Excel presents a dialog that asks Is It OK to Save with References to Unsaved Documents? In general, you should cancel the save, switch to the unsaved source workbook, and then select File, Save As to save the file with a permanent name. Then you can come back to save the linking workbook.
By default, Excel applies security settings that frustrate your attempts to pull values from closed workbooks. Consider the following scenario using two workbooks labeled Workbook A and Workbook B:
If you click Enable Content, a new Trust in Office dialog box offers you the security options to either leave the content disabled or to enable the content. It is a little unclear how updating the link to an external cell can make your document unsecure. If you want to see the current values from the closed workbook, you have no choice other than to enable this content, as shown in Figure 28.11.
Even more annoying, choosing Enable This Content is a temporary setting. The next time you open the workbook, Excel will again disable the automatic update of links.
To permanently allow the updating of links, you need to click the Trust Center button, choose the External Content section, and then select Enable All Workbooks Links, as shown in Figure 28.12.
Caution
If you choose to disable all links, Excel does not even warn you that you are seeing the wrong numbers. In Microsoft’s attempt to prevent some obscure chance of a virus, these new settings will cause enough inadvertent errors to cause a Sarbanes-Oxley auditor to sweat.
There is a simple way to prevent the Trust Center from getting in your way. For example, if you generally store the budget documents in a c:Budget folder on your hard drive, you can add this entire folder to your trusted locations. When the linking file is stored in a trusted location, the links automatically update, no matter what settings you have in the Links section of the Trust Center.
To add a folder to your trusted locations, you follow these steps:
Note
Curiously, having the linking workbook in a trusted location is sufficient. Even if this workbook is linking to a file in a nontrusted location, Excel happily updates the links.
Say that you have saved and closed the linking workbook. You update numbers in the linked workbook. You save and close the linked workbook. Later, when you open the linking workbook, Excel asks if you want to update the links to the other workbook. If you created both workbooks and you have possession of both workbooks, it is fine to allow the workbooks to update.
If you received a linking workbook via email and do not have access to the linked workbooks, Excel alerts you that the workbook contains one or more links that cannot be updated. In this case, you should click Continue in the dialog box shown in Figure 28.13.
You also get this message if the linked workbook was renamed, moved, or deleted. In that case, you should click the Edit Links button to display the Edit Links dialog (see Figure 28.14). Then you should click the Change Source button to tell Excel that the linked workbook has a new name or location. Finally, you need to click the Break Link button to change all linked formulas to their current values.
Say that you need to send a linking workbook to a co-worker. You want your co-worker to see the current values of the linking formulas without having the linked workbook. In this case, you want the co-worker to click Continue in Figure 28.13. However, some newer Excel customers think that every warning box is a disaster, so you might prefer to suppress that box for your co-worker. To do so, you follow these steps:
Of course, after emailing the workbook to your co-worker, you need to redisplay the Startup Prompt dialog and change it back so that you will get the updated links.
Many Web pages comprise many tables of data. Any time you see columns of numbers or columns of data, it is very likely that you are seeing the results of a table. Usually the only things not in a table are paragraphs of body copy. And usually, there would be no need to update this information on a daily basis. Excel 2007 makes it even easier than past versions of Excel to link your Excel worksheet to a table on any Web page.
To set up a connection between a worksheet and a Web page, you follow these steps:
Note in Figure 28.20 that all the Get External Data options in the ribbon are disabled. This happens when your cell pointer is located in external data. To set up a new Web query on the same worksheet, you simply move the cell pointer to a cell outside the retrieved data; for example, Cell A9 would be safe in the worksheet shown in Figure 28.20.
After you have retrieved a Web query, you can select a single cell in the query and choose Properties from the Manage Connections group on the Data ribbon. The External Data Range Properties dialog appears. As shown in Figure 28.21, this legacy dialog box includes additional properties for the query. In the Data Formatting and Layout section, you can choose options to preserve cell formatting and adjust column widths. Most importantly, you can specify that if the query returns more rows tomorrow, any formulas adjacent to the Web query should be expanded.
It is possible to load data from a text file into Excel using the connection group. Follow these steps:
Although Excel can now handle 1.1 million rows, you might encounter larger datasets that need to be stored in Access. You can connect to these larger datasets. You can create a connection to any table in an Access database. Here’s what you do:
Note
Note that if you select a query in Access, there might be a delay as the query is calculated. Excel displays a Getting Data... message in the table while the calculation is in process.
→ To learn more about pivot tables, see Chapter 11, “Formatting Pivot Table,” page 215.
Although Excel 2007 offers icons for Access, Web, and text connections, you can actually connect to a wide variety of other data sources. You access all these sources by clicking the From Other Sources icon on the Data ribbon. When you choose this option, you are presented with five choices, as shown in Figure 28.31.
SQL Server is Microsoft’s structured query language database. Typically, when applications get too big to run smoothly in Microsoft Access, they will be migrated to the more robust SQL Server platform. Because SQL Server is a Microsoft product, connecting to SQL Server is easy. To connect, you will need the Server name, a userid, and a password.
Analysis Services is Microsoft’s cube functionality, currently marketed as SQL Server Analysis Services. A cube database represents data along three or more dimensions. To connect, you will need the Server name, a userid, and a password.
XML stands for Extensible Markup Language. This is a simple text file that includes both data and tags used to identify the data. An example illustrating a connection to XML data is shown in the next section.
OLE-DB stands for Object Linking and Embedding for Databases. This is a Microsoft interface written in the COM environment that allows Windows-based applications to access a wide variety of database types. Consult your database administer for required settings to connect through OLE-DB. Microsoft Query is an older technology that uses Open Database Connectivity (ODBC). If your company has implemented a non-Microsoft platform, ODBC is the interface that allows other programs such as Excel to connect to the database. Typically, the administrator of the system will be able to provide you with a connect string that you will use to access the other system’s data. Microsoft Query also provides a method for Excel to build SQL queries against Access databases. For an example, see “Connecting Using Microsoft Query” later in the chapter.
In the future, you will find more and more datasets based on XML. In reality, XML is like a CSV (comma-separated value) file on steroids. You can create or edit XML files by using Notepad. The difference between a CSV file and an XML file is that each field in XML contains a field identifier. This allows you to intelligently import only certain fields into a spreadsheet. Figure 28.32 shows a simple XML file that has two records.
<fieldname>
and </fieldname>
tags.In addition to the actual XML file, it is possible to have a definition document known as an XSD file or a database schema. You may also have one or more XSL files that are used to transform the data. But as long as you have at least one XML file, Excel will be happy to infer a schema, as shown in Figure 28.33.
As you can see in Figure 28.34, the data is then imported as an Excel table. This is pretty basic functionality. By using a trick in the VBA Editor, however, you can actually retrieve the schema and save it to allow Excel to do more XML tricks.
After Excel has imported the data, you can retrieve the schema by using the VBA Editor. To do so, you follow these steps:
print activeworkbook.XmlMaps(1).Schemas(1).XML
Then press Enter. Excel responds by printing the entire schema for the XML file, as shown in Figure 28.35.
Figure 28.36 shows the complete schema in Notepad, with WordWrap turned on.
The From Access icon on the Data ribbon allows you to retrieve all fields from any Access table or predefined query. There may be times when you want to join Access tables, filter records, or select only a subset of fields from a query. Excel 2007 offers the old Microsoft Query product for building such connections.
To build a new query against a table in an Access database, follow these steps:
Contrast the current example with the previous example in “Setting Up a Connection to an Access Database.” Although the previous example and this example use the same query from Access, the Microsoft Query option enables you to retrieve only the records with more than five items in inventory. The overhead involved in returning few records causes the query to run significantly faster. As shown in Figure 28.43, the data is returned in a sorted manner.
The Data ribbon includes a group called Manage Connections. As shown in Figure 28.44, this group includes an option to refresh all connections. Although the icon says Refresh All, there is a drop-down available where you can choose to refresh only the current query or to view properties for a connection.
Clicking the Connections icon brings up a summary of all the Web, text, Access, or ODBC connections in your workbook. This is a fantastic improvement in Excel 2007. As shown in Figure 28.45, you can click any connection in the top and then follow the hyperlink Click Here to See Where the Selected Connections Are Used in order to jump to the worksheet range that houses the results of the connection.
While this new dialog is very nearly a one-stop source for all external links, you might be disappointed to learn that links to other workbooks are not included in this dialog. You have to select Edit Links to Files from the Manage Connections group in order to display the Edit Links dialog, where you can check the status of any workbook links as well as maintain the link location, break the links, or adjust the startup prompt.
Excel offers fantastic connections. Although users have been able to link to Access databases for several versions, Excel 2007’s improved support for SQL Server, XML, ODBC, text, Web, and Access data is unparalleled. The following Excel in Practice sidebar talks about setting up a connection to one of the newest data stores: an Excel 2007 workbook with half a million records.
3.145.111.107