Hello,
I run a lot of macros that have one big table as an input data, that is later tranformed into separate tables by making pivot tables (using different column/row sets and usually different filter).
Since my upgrade from Office 2016 to 2019, all of them started to work very slowly. I wonder how to optimise my code.
Example fragment of my code with my comments added:
After making a "Stock" data table, code proceeds with activating source data sheet again, making a pivot table with different RowField, ColumnField sets and different xlPageField filter activated. Therefore, it's mostly the same code again and again, but with different "PivotFields" mentioned in different order.
I suppose that getting rid of subtotals is what takes up the most time.
Even smaller data sets take minutes to calculate.
PS. Of course I start with disabling ScreenUpdating in whole macro.
I run a lot of macros that have one big table as an input data, that is later tranformed into separate tables by making pivot tables (using different column/row sets and usually different filter).
Since my upgrade from Office 2016 to 2019, all of them started to work very slowly. I wonder how to optimise my code.
Example fragment of my code with my comments added:
VBA Code:
'All code fragments start with activating big source data table again, before making a different data set out of it
Sheets("source").Activate
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="'source'!R1C1:R" & lastrow & "C" & lastcol, _
Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="", TableName:= _
"Pivot1", DefaultVersion:=xlPivotTableVersion14
With ActiveSheet.PivotTables("Pivot1")
.PivotFields("ProductName").Orientation = xlRowField
.PivotFields("ProductCode").Orientation = xlRowField
.PivotFields("ProductGroup").Orientation = xlRowField
.PivotFields("GroupCode").Orientation = xlRowField
.PivotFields("ProductStatus").Orientation = xlRowField
.PivotFields("StatusCode").Orientation = xlRowField
.AddDataField ActiveSheet.PivotTables("Pivot1").PivotFields("Quantities"), "Quantity", xlSum
.AddDataField ActiveSheet.PivotTables("Pivot1").PivotFields("Values"), "Value", xlSum
.PivotFields("DATA_SET").Orientation = xlPageField
.PivotFields("DATA_SET").CurrentPage = "Stock"
.RowAxisLayout xlTabularRow
.RepeatAllLabels xlRepeatLabels
End With
ActiveSheet.PivotTables ("Pivot1")
Set PvtTbl = ActiveSheet.PivotTables(1)
'hides Subtotals for all fields in the PivotTable - universal piece of code that works with all data sets
On Error Resume Next
With PvtTbl
For Each pvtFld In .PivotFields
pvtFld.Subtotals(1) = False
Next pvtFld
End With
On Error GoTo 0
After making a "Stock" data table, code proceeds with activating source data sheet again, making a pivot table with different RowField, ColumnField sets and different xlPageField filter activated. Therefore, it's mostly the same code again and again, but with different "PivotFields" mentioned in different order.
I suppose that getting rid of subtotals is what takes up the most time.
Even smaller data sets take minutes to calculate.
PS. Of course I start with disabling ScreenUpdating in whole macro.