Search Contextures Sites

Excel VBA - Pivot Table Format Macro

Format a Pivot Table in Classic Style

Format a Pivot Table in Classic Style

This sample Excel VBA code from Bob Ryan, of Simply Learning Excel, formats the first pivot table on the active sheet.

Download the Format a pivot table in classic style sample file. The file is zipped, and in Excel 2007 format.

The macro does the following:

  • Applies the Classic PivotTable display, with gridlines and no colors
  • Ensures that only data that still exists in ' the data that drives the PivotTable ' will appear in the PivotTable dropdown lists
  • Sets all fields to ascending order with no subtotals including fields that are not in the Row Labels or Column Labels areas
  • For the data field(s) in the Values area, changes the setting to Sum, changes the number format, and if the field in the Values area is named "Amount" ot "Total Amount" it shortens the label in the PivotTable to "Sum Amt" or "Sum TtlAmt" respectively..

Watch the Format Pivot Table in Classic Style macro video to see the manual formatting time, compared to running the macro.

Sub ClassicPlusPivotTableSettings()

'Last modified and tested 2010-07
'Much of this is based on a book by Debra Dalgleish named
'    "Excel Pivot Tables Recipe Book: A Problem-Solution Approach", Chapter 13
'
'The following code applies to PivotTable(1) on the active sheet.
'In summary, this macro:
'     Applies the Classic PivotTable display, with gridlines and no colors
'     Ensures that only data that still exists in 
'     the data that drives the PivotTable
'          will appear in the PivotTable dropdown lists
'     Sets all fields to ascending order with no subtotals
'          including fields that are not in the Row Labels or Column Labels areas
'     For the data field(s) in the Values area, 
'          changes the setting to Sum, changes the number format, and
'          if the field in the Values area is named "Amount" ot "Total Amount" 
'          it shortens the label
'          in the PivotTable to "Sum Amt" or "Sum TtlAmt" respectively.

On Error Resume Next
Application.ScreenUpdating = False
 
Dim pt As PivotTable
Dim pf As PivotField
 
Set pt = ActiveSheet.PivotTables(1)
pt.ManualUpdate = True
  
'This section applies Classic PivotTable settings
'     and turns off the Contextual Tooltips and the Expand/Collapse buttons
With pt
    .InGridDropZones = True
    .RowAxisLayout xlTabularRow
    .TableStyle2 = ""
    .DisplayContextTooltips = False
    .ShowDrillIndicators = False
End With

'This sets each field in ascending order. It applies this even to fields 
'     that are not currently in the PivotTable.
For Each pf In pt.PivotFields
    pf.AutoSort xlAscending, pf.Name
    pf.Subtotals(1) = True
    pf.Subtotals(1) = False
Next pf

'This command changes the formatting of any field that appears in the Values area
For Each pf In pt.DataFields
    pf.Function = xlSum
    pf.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
Next pf

'These next two sets of statements shorten the description of 
'     any field in the Values area,
'     but only if the field is named "Amount" or "Total Amount"
With pt.PivotFields("Sum of Amount")
    .Caption = "Sum Amt"
End With
With pt.PivotFields("Sum of Total Amount")
    .Caption = "Sum TtlAmt"
End With

pt.ManualUpdate = False
Application.ScreenUpdating = True

'This ensures that only data that still exists in the data 
'     that drives the PivotTable
'     will appear in the PivotTable dropdown lists
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone

End Sub

 

 

 

 

 

 

 

Pivot Table Tutorials

Excel Pivot Table -- Introduction 
Excel Pivot Table -- Clear Old Items
Excel Pivot Table -- Create a Pivot Table in Excel 2007 
Excel Pivot Table -- Custom Calculations 
Excel Pivot Table -- Data Field Layout
Excel Pivot Table -- Dynamic Data Source
Excel Pivot Table -- FAQs
Excel Pivot Table -- Field Settings
Excel Pivot Table -- Filter Source Data  
Excel Pivot Table -- Filters, Top 10 
Excel Pivot Table -- GetPivotData
Excel Pivot Table -- Grand Totals
Excel Pivot Table -- Grouping Data
Excel Pivot Table -- Layout, Excel 2007
Excel Pivot Table -- Multiple Consolidation Ranges
Excel Pivot Table -- Pivot Cache   
Excel Pivot Table -- PivotTable Style
Excel Pivot Table -- Printing   
Excel Pivot Table -- Protection  
Excel Pivot Table -- Report Filters
Excel Pivot Table -- Running Totals  
Excel Pivot Table -- Show and Hide Items 
Excel Pivot Table -- Sorting
Excel Pivot Table -- Subtotals 
Excel Pivot Table -- Summary Functions
Excel Pivot Table -- Unique Items

Pivot Table Books

Pivot Tables, Beginning (Excel 2007) 
Pivot Tables, Recipe Book (Excel 2003) 
Pivot Tables, Recipe Book (Excel 2007) 

Pivot Table Add-Ins

Pivot Tables - Add-in -- Pivot Power 
Pivot Tables - Add-in - Pivot Play PLUS 

Pivot Table Videos

Pivot Tables - Clear Old Items
Pivot Tables - Copy a Custom PivotTable Style
Pivot Tables - Create in Excel 2007
Pivot Tables - Create from Multiple Sheets
Pivot Tables - Data Field Layout
Pivot Tables - Date Filters, Add
Pivot Tables - GetPivotData
Pivot Tables - Group Data
Pivot Tables - Layout, Excel 2007
Pivot Tables - Report Filters, Add
Pivot Tables - Running Totals
Pivot Tables - Select Sections
Pivot Tables - Subtotals, Create Multiple
Pivot Tables - Top 10 Filters

 

Learn how to create Excel dashboards.

 

       Home     Excel Tips     Excel Files     The Excel Store     Blog     Contact

 

Privacy Policy

 

Contextures Inc., Copyright ©2010
All rights reserved.

 

Last updated: August 15, 2010 1:50 PM