VBA- Change multiple pivot fields

dinakar

New Member
Joined
Nov 8, 2017
Messages
11
Hi,

I am trying to change the pivot filters in a worksheet for all the pivot tables. I tried the code below and it does not work. Can anyone please help! (Level 4,5, and 1 are my pivot fields)

Code:
[I]Sub Reset_filters()[/I]
[I]Dim pt As PivotTable[/I]
[I]For Each pt In Sheets("Rates Pivot").PivotTables[/I]
[I]    With pt.PivotFields( _
        "Level 4").ClearAllFilters
         pt.PivotFields( _
        "Level 4").CurrentPage = Sheets("Maps").Range("G2").Value
         pt.PivotFields( _
        "Level 5").ClearAllFilters
         pt.PivotFields( _
        "Level 5").CurrentPage = Sheets("Maps").Range("H2").Value
         pt.PivotFields("Cons Mgmt Location Level 1" _
        ).ClearAllFilters
         pt.PivotFields("Cons Mgmt Location Level 1" _
        ).CurrentPage = Sheets("Maps").Range("F2").Value
        
    End With[/I]
[I]Next pt[/I]
[I]End Sub[/I]

Thank you!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
"It does not work" is not enough info... How do you know it doesn't work? Does it crash? Does it complete with an unexpected result?

The first thing you can do is set a break point at the beginning of the macro and step through each line using F8. Then you can tell us here what exactly is broken, how you know it is broken and what it should do. The more detail your provide the better chance of getting a reply.
 
Upvote 0
Thank you for responding Cerfani! That helped, I checked step by step and I was able to figure what was breaking. The range I was referring to, didn't exist.
 
Upvote 0
ok cool, glad you figured it out. VBA is a horrible language to program in... in respects to the compiler reporting errors so sometimes you just have to step through it line by line. You can also use messageboxes to tell you what is going on or if a problem has happened in your code. Sometimes your code can navigate somewhere unexpected based on a typo. If you make a typo in a variable or something, vba will just create a new variant and assume you wanted a new variable. Anyways with VBA you have to be very careful and dont rely on the compiler pointing errors out.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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