You are on page 1of 3

Some Useful VBA Routines for Pivot Tables If you have written VBA macros before, this last

section will provide some usefu l examples of how to manipulate Pivot Tables with code. If you are not used to p rogramming in VBA, you might want to just use the sample worksheets to run the V BA macros, these provide a handy set of utilities for working with Pivot Tables. The macros are found in EG8.XLS, open this now and press the buttons in the blu e band above the Pivot Table. The next section explains how each macro works. You can also download the EFutil xla. This is an add-in that places the "Utility " menu item on the standard Excel worksheet menus. The Utility menu contains fiv e handy items. Set Footer - places the file name and path, date created and author in the foote r Align Pivot - Changes orientation in pivot tables with multiple data fields Gridlines - Toggles gridlines on and off Kill Styles - Removes all except the six standard styles Dynamic Range - Builds dynamic ranges for use in Charts, Lists, and Pivot Tables a) Aligning Multiple Data Fields The default setting in Excel is for multiple data fields to be displayed in rows down the page. We can change this to show the fields side by side. The code use s the Orientation property of the PivotField object. Sub ToggleAlignment() ' Sets data fields side by side and back again on Pivot ' First, turn off screen refresh Application.ScreenUpdating = False ' Switches orientation from one to the other With ActiveSheet.PivotTables(1).PivotFields("Data") If .Orientation = xlColumnField Then .Orientation = xlRowField Else .Orientation = xlColumnField .Position = 1 End If End With End Sub b) Formatting a Pivot Table This can be trickier that it first appears. Here is a general scheme for Pivot T able formatting. It uses the PivotSelect method to format different areas of the Pivot Table. Sub FormatData() ' Formats each field in Data Fields Application.ScreenUpdating = False ' Need to make sure selection is on Application.PivotTableSelection = True Set pvtTable = ActiveSheet.PivotTables(1) ' Format months as right-aligned with date format For Each pvtField In pvtTable.PivotFields If pvtField = "Month" Then

pvtTable.PivotSelect "Month[All]", xlLabelOnly Selection.HorizontalAlignment = xlRight Selection.NumberFormat = "mmm-yy" End If Next pvtField For Each pvtField In pvtTable.DataFields Select Case pvtField ' Format % fields to not show DIV ZERO Case "Sum of VarMargin" pvtTable.PivotSelect pvtField, xlDataOnly Selection.NumberFormat = "[Black]0%;[Red](0%);[Black]0%" Selection.Font.ColorIndex = 2 ' Format other fields as numbers Case Else pvtTable.PivotSelect pvtField, xlDataOnly Selection.NumberFormat = "# ##0" End Select Next pvtField ' Format grand totals to wrap text For Each pvtField In pvtTable.ColumnFields pvtTable.PivotSelect "'Row Grand Total'", xlDataAndLabel Selection.WrapText = True Next pvtField Range("A3").Select End Sub c) Toggling Data Fields I often find a need to quickly switch between Sales and Margin when looking at P ivot Table data. This macro lets me swap fields quickly without having to go int o the Pivot Table Wizard. Sub ToggleSales() ' Changes data field from Sales to Margin Application.ScreenUpdating = False Application.PivotTableSelection = True Set pvtTable = ActiveSheet.PivotTables(1) For Each pvtField In pvtTable.DataFields Select Case pvtField Case "Sum of Sales" pvtField.Orientation = xlHidden With pvtTable.PivotFields("Margin") .Orientation = xlDataField .Position = 1 End With Case "Sum of Margin" pvtField.Orientation = xlHidden With pvtTable.PivotFields("Sales") .Orientation = xlDataField .Position = 1 End With End Select Next pvtField End Sub

d) Grouping and Ungrouping When you need to group into quarters without going into the menus you can use a macro that changes the Periods array. Sub GroupQuarters() ' groups pivot table on active sheet by quarter Application.PivotTableSelection = True ActiveSheet.PivotTables(1).PivotSelect "Month", xlLabelOnly Selection.Group Periods:=Array(False, False, False, False, False, True, False) FormatData End Sub Sub GroupMonths() ' groups pivot table on active sheet by quarter Application.PivotTableSelection = True ActiveSheet.PivotTables(1).PivotSelect "Month", xlLabelOnly Selection.Ungroup FormatData End Sub

You might also like