Skip to content

Excel VBA Macro to Show/Hide Grand Totals in a PivotTable

    To control the visibility of Grand Totals in a PivotTable use the ColumnGrand and RowGrand properties of the PivotTable object.

    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Dim PTcache As PivotCache
    Dim PT As PivotTable
    'Define the cache for the PivotTable
    Set PTcache =wb.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=Range("Sales_Data[#All]"),Version:=xlPivotTableVersion15)
    'Create the PivotTable
    Set PT =ws.PivotTables.Add(PivotCache:=PTcache,  _
    TableDestination:=Range("J1"),TableName:="Sales Analysis")
    PT.AddFieldsRowFields:=Array("Region", "Branch"),  _
    ColumnFields:="Product",PageFields:="Product Category"
    PT.AddDataField Field:=PT.PivotFields("Revenue"), _
    Caption:="No of Transactions", Function:=xlCount
    'Remove grand totals from row and column
    PT.ColumnGrand = False
    PT.RowGrand = False