VBA - optimising multiple pivot tables code

bukimi

Board Regular
Joined
Apr 12, 2017
Messages
105
Office Version
  1. 2019
Platform
  1. Windows
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:
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.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top