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
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