Subtotal Method

Microsoft Excel Visual Basic

Show All

Subtotal Method

       

Subtotal method as it applies to the WorksheetFunction object.

Creates subtotals. For information about using the Subtotal worksheet function in Visual Basic, see Using Worksheet Functions in Visual Basic.

expression.Subtotal(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30)

expression   Required. An expression that returns a WorksheetFunction object.

Arg1  Required Double.

Arg2  Required Range object.

Arg3-Arg30  Optional Variant.

Subtotal method as it applies to the Range object.

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

For information about using the Subtotal worksheet function in Visual Basic, see Using Worksheet Functions in Visual Basic.

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 one-based integer offset. For more information, see the example.

Function  Required XlConsolidationFunction. The subtotal function.

XlConsolidationFunction can be one of these XlConsolidationFunction constants.
xlAverage
xlCount
xlCountNums
xlMax
xlMin
xlProduct
xlStDev
xlStDevP
xlSum
xlUnknown
xlVar
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 XlSummaryRow. Places the summary data relative to the subtotal.

XlSummaryRow can be one of these XlSummaryRow constants.
xlSummaryAbove
xlSummaryBelow default

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)