Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Created May 15, 2013 04:07
Show Gist options
  • Save stevehenderson/5581595 to your computer and use it in GitHub Desktop.
Save stevehenderson/5581595 to your computer and use it in GitHub Desktop.
VBA code to configure a pivot table
'
' Configure the pivot table for analysis
'
Sub PvotTableTweak()
Dim pt As PivotTable
Dim WSD As Worksheet
Set WSD = Worksheets("DATA")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("PIVOT")
Dim PTCache As PivotCache
Dim PRange As Range
'Select the first (and only pivot table)
Sheets("Pivot").Select
Set pt = ActiveSheet.PivotTables(1)
Set PTCache = pt.PivotCache
'Clear any existing filters and fields
pt.ClearTable
' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out
pt.ManualUpdate = True
' Set up the row fields
pt.AddFields RowFields:=Array("FACTOR1")
'Set up the columns
Set objField = pt.PivotFields("FACTOR2")
objField.Orientation = xlColumnField
' Set up the data fields
With pt.PivotFields("FACTOR2")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
End With
' Now calc the pivot table
pt.ManualUpdate = False
pt.TableRange2.Copy
Sheets("SUMMARY").Select
'ActiveWindow.SmallScroll Down:=-6
ActiveSheet.Range("A3").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
'Add Header Label
ActiveSheet.Range("A1") = "FACTOR ANALYSIS"
ActiveSheet.Range("A:J").EntireColumn.ColumnWidth = 17
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment