Word tables are useful for organizing text into rows and columns and for providing an attractive layout option for lists and other data. But tables get especially powerful and dynamic when you apply formulas to the numeric data contained within a table's rows or columns. For example, if you have a table of sales for various departments, you could display the total sales in a cell at the bottom of the table. Similarly, if your table lists the gross margins from all company divisions, you could display the average gross margin in a cell. Referencing Table CellsThe trick to using formulas within tables is to reference the table cells correctly. The easiest way to do this is to use the relative referencing that's built into Word tables:
For example, the following formula field sums all the numeric values in the cells above the formula cell in the same column: { =SUM(ABOVE) } Figure 1.27 shows an example of this formula field in action in the subtotal cell (D12, showing the result $90.17). Figure 1.27. The subtotal in this invoice document is calculated using a field with the formula =SUM(ABOVE).If you need to refer to specific cells in your formula, you should use absolute referencing, which is very similar to the cell referencing used by Excel. That is, the table columns are assigned the letters A (for the first column), B (second column), and so on; the table rows are assigned the numbers 1 (for the first row), 2 (second row), and so on. Following are some examples:
In the invoice shown in Figure 1.27, for example, the total for the Non-Reflective Mirror item is derived using the following formula field: { =A2 * C2 \# $0.00 }
Finally, you can also use bookmarks to create formulas that have "named" operands. For example, if you select a cell and insert a bookmark named GrossMargin, you can refer to that cell using the bookmark name, as in this example: { =B3 * GrossMargin } In the invoice document shown in Figure 1.27, I assigned a bookmark named Subtotal to the subtotal cell. To calculate the sales tax value, I used the following formula field: { = Subtotal * .05 }
This also works for entire tables, although in an annoyingly quirky way. Select your table (via Table, Select, Table) and then insert a bookmark to name it. You can then reference a cell in that table as follows: SUM(TableName Cell) Here, TableName is the bookmark name for the table, and Cell is an absolute cell reference. For example, the following code refers to cell A1 in a table named Table1: SUM(Table1 A1) You might think I used the preceding SUM function as an example, but that's not the case. To reference a cell in another table in Word, you must include the table name and cell reference inside a function! In other words, the following apparently sensible code in fact produces a syntax error in Word: Table1 A1 The SUM function is most often used because the sum of a single cell is the value of the cell itself. You can also use AVERAGE, COUNT, MAX, MIN, or PRODUCT, if you want to be different. For example, to refer to the Total Due cell in the table in Figure 1.27, I first selected the table and assigned a bookmark named InvoiceDetail. With that done, the following formula field references the Total Due cell, as shown in Figure 1.28: { =SUM(InvoiceDetail C15) } Figure 1.28. Assign a bookmark to a table to reference cells in that table from other parts of the document.
Solving a Relative Reference ProblemThere's an important limitation to the relative references I mentioned earlier. When it comes upon a relative reference, Word gathers the cells to use in the formula by moving along the column (for ABOVE and BELOW) or the row (for LEFT and RIGHT). Crucially, it stops moving along the row or column when it comes to a cell that's either empty or that contains text. Even if you have more numbers further along the row or column, Word doesn't see them. To show you how this works, consider the version of the invoice table shown in Figure 1.29. Figure 1.29. When you use a relative reference, Word stops when it hits a cell that is blank or that contains text.Notice how the Total cell for the Teflon Bath Mat item (D4) is blank. If you examine the Subtotal, the result is clearly incorrect. That's because the ABOVE relative reference applies only to cells D5 through D11. Because cell D4 is empty, the sum doesn't include the numeric values in cells D2 and D3.
One way to work around this problem is to replace the relative reference with an absolute reference: { =SUM(D2:D11) } That works, as you can see in Figure 1.30. Figure 1.30. One way to work around Word's relative reference problem is to use absolute referencing.The problem with this workaround is that it's not particularly flexible. That is, if you add or delete rows in the table, you must edit the formula by hand. A better solution is to create two tables by splitting the original table just above the SUM formula. (The easiest way to do this is to place the cursor in the row that will form the top of the second table, and then select the Table, Split Table command.) Now use a bookmark to name the table containing the values you want to sum, and then reference this table in the formula. Figure 1.31 shows the split invoice table. The top table I've named OrderDetail, and I've changed the Subtotal field to the following: { =SUM(OrderDetail D:D) } Figure 1.31. To always get an accurate sum, place the SUM formula in a separate table and then reference the table containing the values.Now, no matter how many rows you add or delete in the OrderDetail table, the formula will always return the correct subtotal.
|