Anda di halaman 1dari 5

270 Part II: Using Functions in Your Formulas

Figure 9-25: Using the DSUM function to sum a table using a criteria range.

Inserting Subtotals
Excel’s Data➜Outline➜Subtotal command is a handy tool that inserts formulas into a worksheet
database automatically. These formulas use the SUBTOTAL function. To use this feature, your
database must be sorted because the formulas are inserted whenever the value in a specified
field changes. For more information about the SUBTOTAL function, refer to the sidebar, “About
the SUBTOTAL function,” earlier in this chapter.

When a table is selected, the Data➜Outline➜Subtotal command is not available.


Therefore, this section applies only to worksheet databases. If your data is in a table
and you need to insert subtotals automatically, convert the table to a range by using
Table Tools➜Design➜Tools➜Convert To Range. After you insert the subtotals, you can
convert the range back to a table by using Insert➜Tables➜Table.

Figure 9-26 shows an example of a range that is appropriate for subtotals. This database is
sorted by the Month field, and the Region field is sorted within months.

This workbook, named nested subtotals.xlsx, is available on the companion


CD-ROM.

To insert subtotal formulas into a worksheet database automatically, move the cell pointer any-
where in the range and choose Data➜Outline➜Subtotal. You will see the Subtotal dialog box, as
shown in Figure 9-27.

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


Chapter 9: Tables and Worksheet Databases 271

Figure 9-26: This database is a good candidate for subtotals, which are inserted at each change of the
month.

Figure 9-27: The Subtotal dialog box automatically inserts subtotal formulas into a sorted table.

The Subtotal dialog box offers the following choices:

h At each change in: This drop-down list displays all the fields in your table. You must have
sorted the table by the field that you choose.
h Use function: Choose from 11 functions. (Sum is the default.)
h Add subtotal to: This list box shows all the fields in your table. Place a check mark next
to the field or fields that you want to subtotal.
h Replace current subtotals: If checked, Excel removes any existing subtotal formulas and
replaces them with the new subtotals.
h Page break between groups: If checked, Excel inserts a manual page break after each
subtotal.
h Summary below data: If checked, Excel places the subtotals below the data (the default).
Otherwise, the subtotal formulas appear above the data.
h Remove All: This button removes all subtotal formulas in the table.

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


272 Part II: Using Functions in Your Formulas

When you click OK, Excel analyzes the database and inserts formulas as specified — it even cre-
ates an outline for you. Figure 9-28 shows a worksheet after adding subtotals that summarize by
month. You can, of course, use the SUBTOTAL function in formulas that you create manually.
Using the Data➜Outline➜Subtotals command is usually easier.

Figure 9-28: Excel adds the subtotal formulas automatically and creates an outline.

If you add subtotals to a filtered database, the subtotals may no longer be accurate
when you remove the filter.

The formulas all use the SUBTOTAL worksheet function. For example, the formula in cell E20
(Grand Total) is as follows:

=SUBTOTAL(9,E2:E18)

Although this formula refers to other cells that contain a SUBTOTAL formula, those cells are not
included in the sum to avoid double-counting.
You can use the outline controls to adjust the level of detail shown. Figure 9-29, for example,
shows only the summary rows from the subtotaled table. These rows contain the SUBTOTAL for-
mulas. I hid columns B and C because they show only empty cells.

In most cases, using a pivot table to summarize data is a much better choice. Pivot
tables are much more flexible, and formulas aren’t required. Figure 9-30 shows a
pivot table created from the data. Refer to Chapter 18 for more information about pivot
tables.

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


Chapter 9: Tables and Worksheet Databases 273

Figure 9-29: Use the outline controls to hide the detail and display only the summary rows.

Figure 9-30: Use a pivot table to summarize data!

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


274 Part II: Using Functions in Your Formulas

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

Anda mungkin juga menyukai