The below code will build the Pivot Table in cell A1 on a specific sheet (I used Sheet2...You'll need to change those references in the code).
(edited to correct a typo referenced in subsequent posts...Ron)
Sub BuildMyPivotTable()
ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:="'Average Deployment'!" & Sheets("Average Deployment").Range("a1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)) _
.CreatePivotTable _
TableDestination:="Sheet2!R1C1", _
TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion10
With Sheet2.PivotTables("PivotTable2")
.ColumnGrand = False
.RowGrand = False
.PivotFields("Year").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
.AddFields RowFields:=Array("Year", "Quarter")
With .PivotFields("Number of Days")
.Orientation = xlDataField
.Caption = "Average of Number of Days"
.Function = xlAverage
End With
End With
Sheet2.Range("a2").Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, False, False, True)
End Sub
Does that help?
Bookmarks