Iteration through PivotTables/PageFields/Items not working

billritz

New Member
Joined
Aug 20, 2012
Messages
23
I'm trying to get all possible combinations of some pivottables with page fields (pivottable filters). I know you can't set all items in a page field to false, and I tried to handle that, but my logic is screwed somehow because I'm getting duplicates not all values. This seems like it should be a pretty simple logic problem to get it to go through all possibilities, so has anybody done this before? I'll copy my current effort below. I seed it with 1,1,1, and all pagefields are true to start.

Sub IterateThruTablesFieldsItems(ByRef ptCurrent As Long, ByRef pfCurrent As Long, ByRef piCurrent As Long)
Dim pt As Integer
Dim pf As Integer
Dim pi As Integer
Dim tf As Integer
Dim piName As String
Dim piCheck As Integer


With Sheets("Pivot 1 Simple")
For pt = ptCurrent To 1 Step -1
With .PivotTables(pt)
For pf = pfCurrent To 1 Step -1
With .PageFields(pf) 'specify pivot field by name to use table filters only
For pi = piCurrent To 1 Step -1
piName = .PivotItems(pi).Name
'set item to true and write a line
.PivotItems(piName).Visible = True
If pt <> ptCurrent Or pf <> pfCurrent Or pi <> piCurrent Then WriteLine
'if possible, set item to false and write a line
For piCheck = 1 To .PivotItems.Count
If .PivotItems(piCheck).Visible = True And piCheck <> pi Then
.PivotItems(piName).Visible = False
WriteLine
Exit For
End If
Next piCheck
Next pi
If piCurrent < .PivotItems.Count Then IterateThruTablesFieldsItems ptCurrent, pfCurrent, piCurrent + 1
End With
Next pf
If pfCurrent < .PageFields.Count Then IterateThruTablesFieldsItems ptCurrent, pfCurrent + 1, 1
End With
Next pt
If ptCurrent < .PivotTables.Count Then IterateThruTablesFieldsItems ptCurrent + 1, 1, 1
End With
endit: End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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