Skjoldborg
New Member
- Joined
- Feb 16, 2009
- Messages
- 7
Hi Experts !
I am processing a quite large macro for several worksheets, on each worksheet I end up creating a pivot table. The code works fine, except that it is not running fast, and I think optimizing the pivot is a good place to start. What I have now working is this:
(Excel 2007)
any advice on optimizing is appreciated - thx
I am processing a quite large macro for several worksheets, on each worksheet I end up creating a pivot table. The code works fine, except that it is not running fast, and I think optimizing the pivot is a good place to start. What I have now working is this:
(Excel 2007)
Code:
'the GName variable is a string with the name of the sheet
'and here also the name of the Pivot
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Worksheets(GName).Range("A53").CurrentRegion, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:=ActiveSheet.Cells(77, 3), TableName:=GName, DefaultVersion:= _
xlPivotTableVersion12
Sheets(GName).Select
Cells(77, 3).Select
With ActiveSheet.PivotTables(GName).PivotFields("Business Area")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables(GName)
.AddDataField .PivotFields("Q1, 2003"), "Sum of Q1, 2003", xlSum
.AddDataField .PivotFields("Q2, 2003"), "Sum of Q2, 2003", xlSum
.AddDataField .PivotFields("Q3, 2003"), "Sum of Q3, 2003", xlSum
.AddDataField .PivotFields("Q4, 2003"), "Sum of Q4, 2003", xlSum
.AddDataField .PivotFields("Q1, 2004"), "Sum of Q1, 2004", xlSum
.AddDataField .PivotFields("Q2, 2004"), "Sum of Q2, 2004", xlSum
.AddDataField .PivotFields("Q3, 2004"), "Sum of Q3, 2004", xlSum
.AddDataField .PivotFields("Q4, 2004"), "Sum of Q4, 2004", xlSum
.AddDataField .PivotFields("Q1, 2005"), "Sum of Q1, 2005", xlSum
.AddDataField .PivotFields("Q2, 2005"), "Sum of Q2, 2005", xlSum
.AddDataField .PivotFields("Q3, 2005"), "Sum of Q3, 2005", xlSum
.AddDataField .PivotFields("Q4, 2005"), "Sum of Q4, 2005", xlSum
.AddDataField .PivotFields("Q1, 2006"), "Sum of Q1, 2006", xlSum
.AddDataField .PivotFields("Q2, 2006"), "Sum of Q2, 2006", xlSum
.AddDataField .PivotFields("Q3, 2006"), "Sum of Q3, 2006", xlSum
.AddDataField .PivotFields("Q4, 2006"), "Sum of Q4, 2006", xlSum
.AddDataField .PivotFields("Q1, 2007"), "Sum of Q1, 2007", xlSum
.AddDataField .PivotFields("Q2, 2007"), "Sum of Q2, 2007", xlSum
.AddDataField .PivotFields("Q3, 2007"), "Sum of Q3, 2007", xlSum
.AddDataField .PivotFields("Q4, 2007"), "Sum of Q4, 2007", xlSum
.AddDataField .PivotFields("Q1, 2008"), "Sum of Q1, 2008", xlSum
.AddDataField .PivotFields("Q2, 2008"), "Sum of Q2, 2008", xlSum
.AddDataField .PivotFields("Q3, 2008"), "Sum of Q3, 2008", xlSum
.AddDataField .PivotFields("Q4, 2008"), "Sum of Q4, 2008", xlSum
.AddDataField .PivotFields("Q1, 2009"), "Sum of Q1, 2009", xlSum
.AddDataField .PivotFields("Q2, 2009"), "Sum of Q2, 2009", xlSum
.AddDataField .PivotFields("Q3, 2009"), "Sum of Q3, 2009", xlSum
.AddDataField .PivotFields("Q4, 2009"), "Sum of Q4, 2009", xlSum
.AddDataField .PivotFields("Q1, 2010"), "Sum of Q1, 2010", xlSum
.AddDataField .PivotFields("Q2, 2010"), "Sum of Q2, 2010", xlSum
.AddDataField .PivotFields("Q3, 2010"), "Sum of Q3, 2010", xlSum
.AddDataField .PivotFields("Q4, 2010"), "Sum of Q4, 2010", xlSum
.AddDataField .PivotFields("Q1, 2011"), "Sum of Q1, 2011", xlSum
.AddDataField .PivotFields("Q2, 2011"), "Sum of Q2, 2011", xlSum
.AddDataField .PivotFields("Q3, 2011"), "Sum of Q3, 2011", xlSum
.AddDataField .PivotFields("Q4, 2011"), "Sum of Q4, 2011", xlSum
.AddDataField .PivotFields("Q1, 2012"), "Sum of Q1, 2012", xlSum
.AddDataField .PivotFields("Q2, 2012"), "Sum of Q2, 2012", xlSum
.AddDataField .PivotFields("Q3, 2012"), "Sum of Q3, 2012", xlSum
.AddDataField .PivotFields("Q4, 2012"), "Sum of Q4, 2012", xlSum
.AddDataField .PivotFields("Q1, 2013"), "Sum of Q1, 2013", xlSum
.AddDataField .PivotFields("Q2, 2013"), "Sum of Q2, 2013", xlSum
.AddDataField .PivotFields("Q3, 2013"), "Sum of Q3, 2013", xlSum
.AddDataField .PivotFields("Q4, 2013"), "Sum of Q4, 2013", xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = False
any advice on optimizing is appreciated - thx
Last edited: