VBA - Iterating through report filter on TWO pivot tables

Juiceboxjuke

New Member
Joined
Apr 25, 2014
Messages
1
Hello! I'm brand new to learning VBA, and this forum has been a wonderful resource so far. Thank you in advance for your help.

I have two pivot tables from the same source data displaying different fields, but each pivot table has a report filter on the same field. That is, one pivot table displays age and gender and the other pivot table shows street address, but they both have a report filter on "name". (Details have been changed due to sensitive information - I cannot do this with one pivot table, though it may appear so from this description).

My final aim is to have a macro that iterates through the report filters (on "name") for both pivot tables at the same time, and then copies the results into a new sheet.

Thus, at the end, I should end up with X sheets, each of which shows the age, gender, and street address of an individual person.

I had gotten this to work with one pivot table, using modified code from this thread (2nd post by Jerry Sullivan).

However, when I try to do the same thing for two pivot tables, only one of the pivot tables iterates through the name filter, while the other pivot table remains stuck on the first name.

Here is the code for iterating through the report filters on the pivot tables - as you can see, I just repeated the same code for both pivot tables, changing the name of the pivot table.

Any ideas what might be the problem?

Dim sItem As String
Dim i As Integer

Application.ScreenUpdating = True
With ActiveSheet.PivotTables("AgeGender")
.PivotCache.MissingItemsLimit = xlMissingItemsNone
.PivotCache.Refresh
With .PivotFields("Name")
'---hide all items except item 1
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next

With ActiveSheet.PivotTables("StreetAddress")
.PivotCache.MissingItemsLimit = xlMissingItemsNone
.PivotCache.Refresh
With .PivotFields("Name")
'---hide all items except item 1
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next

For i = 1 To .PivotItems.Count
.PivotItems(i).Visible = True
If i <> 1 Then .PivotItems(i - 1).Visible = False
sItem = .PivotItems(i)

'this is followed by code to copy and paste the results of the pivots onto new sheets, which is working fine
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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