VBA code that runs through pivot tables and refreshes fields/filters

alex1stef2

New Member
Joined
Sep 6, 2017
Messages
2
Hi all,

I am currently having a lot of difficulty trying to update legacy code to become dynamic. Effectively I want to create a loop that runs through a bunch of pivot tables, refreshes them according to some some cell value on an index sheet (i.e. new filter is Sheet1!A1 or similar), and then go on to the next one. I will omit the majority of the code as to begin with I just want a few pointers as to how I should be using my variables.

Code:
For Each MySheet In ActiveWorkbook.Worksheets


    For Each MyPivot In MySheet.PivotTables

        NewCat = Worksheets("WorkingSheet").Range("B44").Value


        MyPivot.RefreshTable
        Sheets(MySheet).PivotTables(MyPivot).AddDataField
        Sheets(MySheet).PivotTables(MyPivot).PivotFields ("GuidelinePercent"), "GuidelinePercent", xlCount

    With Sheets(MySheet).PivotTables(MyPivot).PivotFields("Date")
        .Orientation = xlRowField
        .Position = 1


    End With

    Sheet(MySheet).PivotTables(MyPivot).PivotFields("GuidelineItemInstanceName").ClearAllFilters
    Sheet(MySheet).PivotTables(MyPivot).PivotFields("GuidelineItemInstanceName").CurrentPage = NewCat



    Next MyPivot
Next MySheet

This is just a skeleton of what my end result will look like (potentially just pseudo-code as it doesn't even come close to fully running). I apologise that my code is awful - I am (obviously!) very new to this.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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