Vous êtes sur la page 1sur 5

250 Part II: Using Functions in Your Formulas

h Var: Displays the variance of the values in the column. Variance is another statistical
measure of how “spread out” the values are.
h More Functions: Displays the Insert Function dialog box so that you can select a function
that isn’t in the list.

Figure 9-10: Several types of summary functions are available for the Total row.

Using the drop-down list, you can select a summary function for the column. Excel inserts a for-
mula that uses the SUBTOTAL function and refers to the table’s column using a special structured
syntax (described later). The first argument of the SUBTOTAL function determines the type of
summary displayed. For example, if the first argument is 109, the function displays the sum. You
can override the formula inserted by Excel and enter any formula you like in the Total row cell.
For more information, see the sidebar “About the SUBTOTAL function.”

The SUBTOTAL function is one of two functions that ignores data hidden by filtering
(the other is the new AGGREGATE function). If you have other formulas that refer to
data in a filtered table, these formulas don’t adjust to use only the visible cells. For
example, if you use the SUM function to add the values in column C and some rows are
hidden because of filtering, the formula continues to show the sum for all the values in
column C — not just those in the visible rows.

If you have a formula that refers to a value in the Total row of a table, the formula
returns an error if you hide the Total row. However, if you make the Total row visible
again, the formula works as it should.

14_475362-ch09.indd 250 4/14/10 10:09 PM


Chapter 9: Tables and Worksheet Databases 251

About the SUBTOTAL function


The SUBTOTAL function is very versatile, but it’s also one of the most confusing functions in
Excel’s arsenal. First of all, it has a misleading name because it does a lot more than addition.
The first argument for this function requires an arbitrary (and impossible to remember) number
that determines the type of result that’s returned. Fortunately, the Excel Formula AutoComplete
feature helps you insert these numbers.
In addition, the SUBTOTAL function was enhanced in Excel 2003 with an increase in the number
of choices for its first argument, which opens the door to compatibility problems if you share
your workbook with someone who uses an earlier version of Excel.
The first argument for the SUBTOTAL function determines the actual function used. For exam-
ple, when the first argument is 1, the SUBTOTAL function works like the AVERAGE function. The
following table shows the possible values for the first argument for the SUBTOTAL function:

Value Function
1 AVERAGE
2 COUNT
3 COUNTA
4 MAX
5 MIN
6 PRODUCT
7 STDEV
8 STDEVP
9 SUM
10 VAR
11 VARP
101* AVERAGE
102* COUNT
103* COUNTA
104* MAX
105* MIN
106* PRODUCT
107* STDEV
108* STDEVP
109* SUM
110* VAR
111* VARP
*Excel 2003 and later
continued

14_475362-ch09.indd 251 4/14/10 10:09 PM


252 Part II: Using Functions in Your Formulas

continued
When the first argument is greater than 100, the SUBTOTAL function behaves a bit differently.
Specifically, it does not include data in rows that were hidden manually. When the first argument
is less than 100, the SUBTOTAL function includes data in rows that were hidden manually but
excludes data in rows that were hidden as a result of filtering or using an outline.
To add to the confusion, a manually hidden row is not always treated the same. If a row is manu-
ally hidden in a range that already contains rows hidden via a filter, Excel treats the manually
hidden rows as filtered rows. After a filter is applied, Excel can’t seem to tell the difference
between filtered rows and manually hidden rows. The SUBTOTAL function with a first argument
over 100 behaves the same as those with a first argument under 100, and removing the filter
shows all rows — even the manually hidden ones.
The ability to use a first argument that’s greater than 100 was introduced in Excel 2003. You can
use this updated version of the SUBTOTAL function anywhere in your workbook; that is, it’s not
limited to tables. Be aware, however, that this function is not backward compatible. If you share
your workbook with someone who is using a version prior to Excel 2003, the SUBTOTAL func-
tion will display an error if you use a first argument greater than 100.
Another interesting characteristic of the SUBTOTAL function is its ability to produce an accurate
grand total. It does this by ignoring any cells that already contain a formula with SUBTOTAL in
them. For a demonstration of this ability, see the “Inserting Subtotals” section later in this chapter.

Using formulas within a table


Adding a Total row to a table is an easy way to summarize the values in a table column. In many
cases, you’ll want to use formulas within a table. For example, in the table shown in Figure 9-11,
you might want to add a column that shows the difference between the Actual and Projected
amounts. As you’ll see, Excel makes this very easy when the data is in a table.

Figure 9-11: Adding a calculated column to this table is easy.

14_475362-ch09.indd 252 4/14/10 10:09 PM


Chapter 9: Tables and Worksheet Databases 253

This workbook, named table formulas.xlsx, is available on the companion


CD-ROM.

1. Activate cell E2 and type Difference for the column header.


Excel automatically expands the table to include a new column.
2. Move to cell E3 and type an equal sign to signify the beginning of a formula.
3. Press ←, and Excel displays =[@Actual], which is the column heading in the Formula bar.
4. Type a minus sign and then press ← twice. Excel displays =[@Actual]–[@Projected] in
your formula.
5. Press Enter to end the formula.
Excel copies the formula to all rows in the table.

Figure 9-12 shows the table with the new column.

Figure 9-12: The Difference column contains a formula.

If you examine the table, you’ll find this formula for all cells in the Difference column:

=[@Actual]–[@Projected]

The “at” symbol (@) that precedes the column header represents “this row” (the row
that contains the formula).

Keep in mind that I didn’t define any names in this worksheet. The formula uses table references
that are based on the column names. If you change the text in a column header, any formulas
that refer to that data update automatically.

14_475362-ch09.indd 253 4/14/10 10:09 PM


254 Part II: Using Functions in Your Formulas

Although I entered the formula into the first data row of the table, that’s not necessary. Any time
you enter a formula into any cell in an empty table column, it will automatically fill all the cells in
that column. And if you need to edit the formula, edit the copy in any row, and Excel automati-
cally copies the edited formula to the other cells in the column.
The preceding steps use the pointing technique to create the formula. Alternatively, you can
enter the formula manually using standard cell references. For example, you can enter the follow-
ing formula in cell E3:

=D3–C3

If you type the formulas using cell references, Excel still copies the formula to the other cells
automatically: It just doesn’t use the column headings.

When Excel inserts a calculated column formula, it also displays a Smart Tag, with some
options, one of which is Stop Automatically Creating Calculated Columns. Select this
option if you prefer to do your own copying within a column.

Referencing data in a table


The preceding section describes how to create a column of formulas within a table. What about
formulas outside of a table that refer to data inside of a table? You can take advantage of the
structured table referencing that uses the table name, column headers, and other table elements.
You no longer need to create names for these items.
The table itself has a name (for example, Table1), and you can refer to data within the table by
using column headers.
You can, of course, use standard cell references to refer to data in a table, but the structured
table referencing has a distinct advantage: The names adjust automatically if the table size
changes by adding or deleting rows.
Refer to Figure 9-13, which shows a simple table that contains regional sales information. Excel
named this table Table2 when it was created; it was the second table in the workbook. To calcu-
late the sum of all the values in the table, use this formula:

=SUM(Table2)

This formula always returns the sum of all the data, even if rows or columns are added or deleted.
And if you change the name of the table, Excel adjusts all formulas that refer to that table auto-
matically. For example, if you rename Table1 to be Q1Data, the preceding formula changes to

=SUM(Q1Data)

14_475362-ch09.indd 254 4/14/10 10:09 PM

Vous aimerez peut-être aussi