• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Print Pivot Table for Each Report Filter

Hello

Iam looking for Printing pivot table for each report filter working with a workbook, So this is manuel job for me at work.Any clue.
 
Hey James..

Not sure this helps, Done this at my work place.

Please let us no any challenges..Happy to help U.
Code:
Sub Print_pivot()

'Step 1: Declare your Variables
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem


'Step 2: Point to the pivot table in the activecell
    On Error Resume Next
    Set pt = ActiveSheet.PivotTables(ActiveCell.PivotTable.Name)


'Step 3:  Exit if active cell is not in a pivot table
    If pt Is Nothing Then
    MsgBox "You must place your cursor inside of a pivot table."
    Exit Sub
    End If


'Step 4:  Exit if more than one page field
    If pt.PageFields.Count > 1 Then
    MsgBox "Too many Report Filter Fields. Limit 1."
    Exit Sub
    End If


'Step 5:  Start looping through the page field and its pivot items
    For Each pf In pt.PageFields
        For Each pi In pf.PivotItems
      
'Step 6:  Change the selection in the report filter
        pt.PivotFields(pf.Name).CurrentPage = pi.Name
      
      
'Step 7: Set Print Area and print
        ActiveSheet.PrintOut Copies:=1
        Next pi
    Next pf

End Sub
 
Hey little champ.

That was so quick
This is the best of Chandoo.
i will take a while to test this am not quick as you.
Any challenges i will definitely let you know.:)
 
Little Champ.

While i check, i have another quick question to create each pivot filter as a workbook.

Iam trying to do on my own, but still to have proper coding, for me only macro recording.

Cheers!
 
Champ.

Agree but helps us to learn vba this way, i can understand that difficulty for you guys to provide solutions without any workbook.

Hope you can understand where am coming from, for beginner perspective.
 
James, Here we go.

Pls change path.


Code:
Sub Create_Workbook_Pivot()

'Step 1: Declare your Variables
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem


'Step 2: Point to the pivot table in the activecell
    On Error Resume Next
    Set pt = ActiveSheet.PivotTables(ActiveCell.PivotTable.Name)


'Step 3:  Exit if active cell is not in a pivot table
    If pt Is Nothing Then
    MsgBox "You must place your cursor inside of a pivot table."
    Exit Sub
    End If
   
   
'Step 4:  Exit if more than one page field
    If pt.PageFields.Count > 1 Then
    MsgBox "Too many Report Filter Fields. Limit 1."
    Exit Sub
    End If


'Step 5:  Start looping through the page field and its pivot items
    For Each pf In pt.PageFields
        For Each pi In pf.PivotItems
               

'Step 6:  Change the selection in the report filter
        pt.PivotFields(pf.Name).CurrentPage = pi.Name
       
       
'Step 7: Copy the data area to a new workbook
        pt.TableRange1.Copy _
        Workbooks.Add.Worksheets(1).Paste
       
        Application.DisplayAlerts = False
          ActiveWorkbook.SaveAs _
          Filename:="C:\Monty\New\" & pi.Name & ".xlsx" 'Change path here
          ActiveWorkbook.Close
        Application.DisplayAlerts = True
       
       
'Step 8: Get the next page field item
        Next pi
    Next pf

End Sub
 
Below code works faster as expected.
Need a change wanted this to work on its own, i meen.. i do not want to change the filter and click button. have a best sleep.

Am not in hurry! Cheers.


James, Here we go.

Pls change path.


Code:
Sub Create_Workbook_Pivot()

'Step 1: Declare your Variables
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem


'Step 2: Point to the pivot table in the activecell
    On Error Resume Next
    Set pt = ActiveSheet.PivotTables(ActiveCell.PivotTable.Name)


'Step 3:  Exit if active cell is not in a pivot table
    If pt Is Nothing Then
    MsgBox "You must place your cursor inside of a pivot table."
    Exit Sub
    End If
  
  
'Step 4:  Exit if more than one page field
    If pt.PageFields.Count > 1 Then
    MsgBox "Too many Report Filter Fields. Limit 1."
    Exit Sub
    End If


'Step 5:  Start looping through the page field and its pivot items
    For Each pf In pt.PageFields
        For Each pi In pf.PivotItems
              

'Step 6:  Change the selection in the report filter
        pt.PivotFields(pf.Name).CurrentPage = pi.Name
      
      
'Step 7: Copy the data area to a new workbook
        pt.TableRange1.Copy _
        Workbooks.Add.Worksheets(1).Paste
      
        Application.DisplayAlerts = False
          ActiveWorkbook.SaveAs _
          Filename:="C:\Monty\New\" & pi.Name & ".xlsx" 'Change path here
          ActiveWorkbook.Close
        Application.DisplayAlerts = True
      
      
'Step 8: Get the next page field item
        Next pi
    Next pf

End Sub
 
Back
Top