Home » excel » excel – Speed up pivot table filtering VBA code

excel – Speed up pivot table filtering VBA code

Posted by: admin March 9, 2020 Leave a comment


I have a pivot table with a pivot field and contain many items.
I’ve VBA code logic to decide if the pivot value should be visible or not.
The problem is excel recalculates pivot table for each field shown or hidden which makes it very slow.
I would like something where it recalculates only once, after all the values are set.
I tried using Application.Calculation = xlCalculationManual but it didnt help.

vba code that I am using is something like this

For i = 1 To oPivotField.PivotItems.Count
    If (oPivotField.PivotItems(i).Name = "TestCondition") Then
        oPivotField.PivotItems(i).Visible = True   'Recalulates pivot table
        oPivotField.PivotItems(i).Visible = False 'Recalulates pivot table
    End If

I am to do this manually by uncheck the “show all” box and re-check for the fields I want visible. This cause Excel to recalculate once and show only the pivot items I want visible.
I would like to do same thing via VBA code.

I even tried using

Application.ScreenUpdating = False
Application.DisplayAlerts = False

but didn’t work.

How to&Answers:

Oh! I just solved that one:

At the beginning of your code, turn off the auto-update like this:

PivotTable.ManualUpdate = True

And then at the end of the code, turn it back on:


I found this thread searching for help writing code that determines if a PivotTable value should be visible. What is behind your oPivotField? That’s the part I’m missing!


PivotTable objects have a ManualUpdate property which might be what you are looking for.

See http://www.ozgrid.com/VBA/hide-pivot-fields.htm for some related code


pivottable.ManualUpdate [ = setting ]
True causes RefreshTable to clear data from the pivot table, rather than refreshing it
False allows RefreshTable to work normally.
Default is False.
This property is reset to False automatically after the calling procedure ends (important)

This property should be set to true just before you make an update (e.g. changing pivot item Visible property)
So your code would look like:

For i = 1 To oPivotField.PivotItems.Count
    If (oPivotField.PivotItems(i).Name = "TestCondition") Then
        oPivotField.Parent.ManualUpdate = True
        oPivotField.PivotItems(i).Visible = True  'doesn't recalculate pivot table because ManualUpdate is set to True
        oPivotField.Parent.ManualUpdate = True
        oPivotField.PivotItems(i).Visible = False 'doesn't recalculate pivot table because ManualUpdate is set to True
    End If

'setting pivot table ManualUpdate property to False might be redundant at this point because it gets reset to false immediately after you set Visible property of oPivotField
oPivotField.Parent.ManualUpdate = False

As a conclusion, ManualUpdate property change doesn’t stay for long (in my tests, I could see that it gets reset to false as soon as possible, so that’s why I recommended you to set it to true whenever you want to make a change for a pivot item)

For more info on what means an update in Excel, you can check the following:
Pivot Refresh vs. Update – is there a real difference?

Title: Programming Excel with VBA and .NET
By: Jeff Webb, Steve Saunders
Print ISBN: 978-0-596-00766-9 | ISBN 10: 0-596-00766-3
Ebook ISBN: 978-0-596-15951-1 | ISBN 10: 0-596-15951-X


Try to add the following. Helped to give extra speed in my case aswell.

At the beginning:

Application.Calculation = xlCalculationManual

At the end:

Application.Calculation = xlCalculationAutomatic


Save a copy of your workbook and save it as “excel 93-2007” file. Then try your code. Hope this helps you.