Create macro button to refresh pivot tables and apply filter

%3CLINGO-SUB%20id%3D%22lingo-sub-2840328%22%20slang%3D%22en-US%22%3ECreate%20macro%20button%20to%20refresh%20pivot%20tables%20and%20apply%20filter%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2840328%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20everyone%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHoping%20someone%20out%20there%20can%20help%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20built%20a%20worksheet%20that%20has%20several%20pivots%20across%20a%20few%20tabs%20all%20with%20the%20same%20data%20source%20reading%20from%20one%20tab%20(raw%20data).%26nbsp%3BI%20am%20wanting%20to%20create%20a%20button%20that%20once%20clicked%2C%20refreshes%20all%20pivot%20tables%20on%20the%20worksheet%20(about%206%20in%20total%20across%203%20different%20tabs)%20whilst%20retaining%20the%20same%20filters%20(Yes%2FNo)%20on%20each%20pivot%20that's%20applied.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20managed%20to%20figure%20out%20the%20refresh%20part%20which%20I%20found%20by%20googling%20which%20is%20working%20great%20(see%20below)%20but%20not%20the%20part%20that%20forces%20each%20of%20the%20pivots%20back%20into%20the%20same%20filter.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%3CP%3ESub%20RefreshAllPivots()%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EDim%20pc%20As%20PivotCache%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EFor%20Each%20pc%20In%20ThisWorkbook.PivotCaches%3C%2FP%3E%3CP%3Epc.Refresh%3C%2FP%3E%3CP%3ENext%20pc%3C%2FP%3E%3CP%3E%5BInstructions!A10%5D%20%3D%20%22The%20pivots%20were%20last%20refreshed%20at%20%22%20%26amp%3B%20Now%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20reason%20I%20need%20the%20pivots%20to%20force%20the%20filters%20back%20is%20sometimes%20they%20break%20after%20a%20refresh.%20So%20basically%2C%20the%20raw%20data%20has%20a%20bunch%20of%20Yes%2FNo%20columns%20and%20each%20Pivot%20only%20contains%20one%20Yes%2FNo%20filter.%20However%2C%20sometimes%20the%20raw%20data%20may%20contain%20not%20contain%20a%20'Yes'%20which%20is%20acceptable%20but%20when%20the%20pivot%20is%20refreshed%2C%20it%20loses%20that%20filter%20going%20forward.%20So%20when%20adding%20new%20data%20and%20'Yes'%20is%20contained%20in%20that%20column%2C%20the%20pivot%20no%20longer%20applies%20'Yes'%20since%20it%20wasn't%20previous%20in%20the%20data%20and%20filter.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20basically%2C%20I%20just%20need%20the%20Macro%20to%20refresh%20each%20pivot%20then%20apply%20'Yes'%20in%20as%20the%20filter%20in%20each%20of%20the%206%20pivots.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHopefully%20that%20makes%20but%20if%20any%20further%20clarification%20required%2C%20please%20let%20me%20know.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAppreciate%20if%20anyone%20can%20help%20with%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EKind%20regards%2C%3C%2FP%3E%3CP%3EDarren%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2840328%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Hi everyone, 

 

Hoping someone out there can help me.

 

I have built a worksheet that has several pivots across a few tabs all with the same data source reading from one tab (raw data). I am wanting to create a button that once clicked, refreshes all pivot tables on the worksheet (about 6 in total across 3 different tabs) whilst retaining the same filters (Yes/No) on each pivot that's applied.

 

I managed to figure out the refresh part which I found by googling which is working great (see below) but not the part that forces each of the pivots back into the same filter. 

 

####################################

Sub RefreshAllPivots()


Dim pc As PivotCache


For Each pc In ThisWorkbook.PivotCaches

pc.Refresh

Next pc

[Instructions!A10] = "The pivots were last refreshed at " & Now

 

End Sub

####################################

 

The reason I need the pivots to force the filters back is sometimes they break after a refresh. So basically, the raw data has a bunch of Yes/No columns and each Pivot only contains one Yes/No filter. However, sometimes the raw data may contain not contain a 'Yes' which is acceptable but when the pivot is refreshed, it loses that filter going forward. So when adding new data and 'Yes' is contained in that column, the pivot no longer applies 'Yes' since it wasn't previous in the data and filter. 

 

So basically, I just need the Macro to refresh each pivot then apply 'Yes' in as the filter in each of the 6 pivots. 

 

Hopefully that makes but if any further clarification required, please let me know.

 

Appreciate if anyone can help with this.

 

Kind regards,

Darren

 

0 Replies