Subtotal Method

Applies To

Range object.

Description

Creates subtotals for the range (or the current region, if the range is a single cell).

Syntax

expression.Subtotal(GroupBy, Function, TotalList, Replace, PageBreaks, SummaryBelowData)

expression Required. An expression that returns a Range object.

GroupBy Required Long. The field to group by, as a 1-based integer offset. For more information, see the example.

Function Required Long. The subtotal function. Can be one of the following XlConsolidationFunction constants: xlAverage, xlCount, xlCountNums, xlMax, xlMin, xlProduct, xlStDev, xlStDevP, xlSum, xlVar, or xlVarP.

TotalList Required Variant. An array of 1-based field offsets, indicating the fields to which the subtotals are added. For more information, see the example.

Replace Optional Variant. True to replace existing subtotals. The default value is False.

PageBreaks Optional Variant. True to add page breaks after each group. The default value is False.

SummaryBelowData Optional Variant. Can be one of the following xlSummaryRow constants: xlSummaryAbove or xlSummaryBelow. The default value is xlSummaryBelow.

See Also

RemoveSubtotal method.

Example

This example creates subtotals for the selection on Sheet1. The subtotals are sums grouped by each change in field one, with the subtotals added to fields two and three.

Worksheets("Sheet1").Activate
Selection.Subtotal groupBy:=1, function:=xlSum, _
    totalList:=Array(2, 3)