I am attempting to Print multiple reports, based on a pivot table, after searching the web, I have found the below code.
This does work, it will cycle through the first filter field, and update the pivot table. However, it doesn't advance properly.
For example it will run through the For loop multiple times and print the same data multiple times before going to the next "pi".
I'm not sure what is causing this, and it's different for each one. Some values get printed out several times, others just a couple.
Needless to say I only need the value printed once, before advancing and printing the next value of "pi"
Any help would be appreciated.
Sub PrintFirstFilterItems()
'downloaded from contextures.com
'prints a copy of pivot table
'for each item in
'first Report Filter field
On Error Resume Next
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Set pf = pt.PageFields(1)
If pf Is Nothing Then Exit Sub
For Each pi In pf.PivotItems
pt.PivotFields(pf.Name) _
.CurrentPage = pi.Name
'ActiveSheet.PrintOut 'for printing
ActiveSheet.PrintPreview 'for testing
Next pi
End Sub
This does work, it will cycle through the first filter field, and update the pivot table. However, it doesn't advance properly.
For example it will run through the For loop multiple times and print the same data multiple times before going to the next "pi".
I'm not sure what is causing this, and it's different for each one. Some values get printed out several times, others just a couple.
Needless to say I only need the value printed once, before advancing and printing the next value of "pi"
Any help would be appreciated.
Sub PrintFirstFilterItems()
'downloaded from contextures.com
'prints a copy of pivot table
'for each item in
'first Report Filter field
On Error Resume Next
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Set pf = pt.PageFields(1)
If pf Is Nothing Then Exit Sub
For Each pi In pf.PivotItems
pt.PivotFields(pf.Name) _
.CurrentPage = pi.Name
'ActiveSheet.PrintOut 'for printing
ActiveSheet.PrintPreview 'for testing
Next pi
End Sub