+ Reply to Thread
Results 1 to 6 of 6

Looping through in Pivot table Filter fields

Hybrid View

  1. #1
    Registered User
    Join Date
    07-05-2016
    Location
    Berlin, Germany
    MS-Off Ver
    2010
    Posts
    3

    Looping through in Pivot table Filter fields

    Hey guys,

    I have a big problem and I hope that you can help me I want to print all of the filter fields (all of the offers) in a pivot table, and print the tables one below another to the same page. Iīm trying with the following code:

    
    Dim PvtTbl1 As PivotTable
    Dim pvtFld1 As PivotField
    Dim pi1 As PivotItem
    Dim wsData1 As Worksheet
    Dim rngData1 As Range
    Dim PvtTblCache1 As PivotCache
    Dim wsPvtTbl1 As Worksheet
    
    
    Set wsData1 = Worksheets("Uber")
    Set wsPvtTbl1 = Worksheets("Uber_opt")
    Set rngData1 = wsData1.Range("A1").CurrentRegion
    
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData1, Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=wsPvtTbl1.Range("A1"), TableName:="Uber_pivot"
    Set PvtTbl1 = wsPvtTbl1.PivotTables("Uber_pivot")
    
    
    
    Set pvtFld1 = PvtTbl1.PivotFields("offer")
    pvtFld1.Orientation = xlPageField
    
    Set pvtFld1 = PvtTbl1.PivotFields("publisher")
    pvtFld1.Orientation = xlRowField
    
    Set pvtFld1 = PvtTbl1.PivotFields("conversions")
    pvtFld1.Orientation = xlDataField
    
    
    Set pvtFld1 = PvtTbl1.PivotFields("revenue")
    pvtFld1.Orientation = xlDataField
    
    With PvtTbl1.PivotFields("First Ride")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "#,##0"
    .Position = 1
    End With
    
    For Each pi1 In PvtTbl1.PageFields("offer").PivotItems
    PvtTbl1.PageFields("offer").CurrentPage = pi1.Name
    PvtTbl1.ManualUpdate = True
    
    Range("A3").End(xlDown).Offset(3, 0).Select
    
    
    Next pi1
    The code rolls down to the last item in the Filter field and prints only that table instead of all of them. Can you help me, please?

    Thank you,

    Best,

    Dani
    Last edited by zsirsaman; 07-06-2016 at 03:54 AM.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Looping through in Pivot table Filter fields

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Looping through in Pivot table Filter fields

    For Each pi1 In PvtTbl1.PageFields("offer").PivotItems
    PvtTbl1.PageFields("offer").CurrentPage = pi1.Name
    PvtTbl1.ManualUpdate = True
    
    Range("A3").End(xlDown).Offset(3, 0).Select
    Next pi1
    there is no printing (or extracting) being done here?
    did you leave something out?

  4. #4
    Registered User
    Join Date
    07-05-2016
    Location
    Berlin, Germany
    MS-Off Ver
    2010
    Posts
    3

    Re: Looping through in Pivot table Filter fields

    Hey humdingaling!

    Thank you for your quick reply. This is the first time Iīm using VBA to create Pivot tables. When I do it without the loop, the
    Select Code
    PvtTbl1.ManualUpdate = True
    prints the table, simply like this:

    [/CODE]
    PvtTbl1.ManualUpdate = False
    
    Dim PvtTbl2 As PivotTable
    Dim wsData2 As Worksheet
    Dim rngData2 As Range
    Dim PvtTblCache2 As PivotCache
    Dim wsPvtTbl2 As Worksheet
    Dim pvtFld2 As PivotField
    
    Set wsData2 = Worksheets("Stubhub")
    Set wsPvtTbl2 = Worksheets("Stubhub_opt")
    Set rngData2 = wsData2.Range("A1").CurrentRegion
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData2, Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=wsPvtTbl2.Range("A1"), TableName:="Stubhub_pivot"
    Set PvtTbl2 = wsPvtTbl2.PivotTables("Stubhub_pivot")
    
    PvtTbl2.ManualUpdate = True
    
    Set pvtFld2 = PvtTbl2.PivotFields("offer")
    pvtFld2.Orientation = xlPageField
    
    Set pvtFld2 = PvtTbl2.PivotFields("publisher")
    pvtFld2.Orientation = xlRowField
    
    Set pvtFld2 = PvtTbl2.PivotFields("conversions")
    pvtFld2.Orientation = xlDataField
    
    Set pvtFld2 = PvtTbl2.PivotFields("revenue")
    pvtFld2.Orientation = xlDataField
    
    With PvtTbl2.PivotFields("Purchase")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "#,##0"
    .Position = 1
    End With
    
    
    PvtTbl2.ManualUpdate = False
    [/CODE]

    Which command should I use to print the table?

    Thank you guys,

    Dani
    Last edited by zsirsaman; 07-06-2016 at 05:59 AM.

  5. #5
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Looping through in Pivot table Filter fields

    you print tables the same way you print normal sheets in excel

    heres a modified example of what you could do...printing to pdf in this case

    For Each pi1 In PvtTbl1.PageFields("offer").PivotItems
    PvtTbl1.PageFields("offer").CurrentPage = pi1.Name
    
            strFileName = ThisWorkbook.Path & "\print\" & pi1.Name
            With ActiveSheet
                .PageSetup.PrintArea = "B1:O" & Cells(Rows.Count, "B").End(xlUp).Row ' you need to update this to match your print area setup
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, IgnorePrintAreas:=False, OpenAfterPublish:=False
            End With
        Next
    ^
    this prints makes pi into a pdf into the \print\ subfolder of ThisWorkBook.Path
    if you want the headers to repeat..you look that into pagesetup prior to running the code
    you can shrink to fit on one page also ..this would require extra code

  6. #6
    Registered User
    Join Date
    07-05-2016
    Location
    Berlin, Germany
    MS-Off Ver
    2010
    Posts
    3

    Re: Looping through in Pivot table Filter fields

    Hey, thank you for your reply, I didnīt use the proper terminology. I donīt want them to be printed out, just to be displayed in the same sheet one below another. Right now it generates the pivot table and "rolls down" the filter field to the last entry (in alphabetical order). So instead of displaying all the offers, it shows only the last pivot table. If I donīt include the
    For Each pi1 In PvtTbl1.PageFields("offer").PivotItems
    PvtTbl1.PageFields("offer").CurrentPage = pi1.Name
    PvtTbl1.ManualUpdate = True
    
    Range("A3").End(xlDown).Offset(3, 0).Select
    Next pi1
    part, then it displays the overall pivot table (for All the offers).

    I hope now itīs clear what I would like to do,

    thank you in advance,

    Best,

    Dani

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. looping through pivot table filter
    By ShaliniGomes in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-25-2014, 12:02 AM
  2. Pivot Table, Looping through Filter
    By noobtime in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2013, 05:53 PM
  3. Pivot Table filter with added fields?
    By rbpd5015 in forum Excel General
    Replies: 3
    Last Post: 08-17-2010, 02:10 PM
  4. Pivot Table - Looping Through Pivot Fields
    By mini12 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-07-2009, 09:49 AM
  5. Looping page fields in pivot table
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2006, 08:50 PM
  6. Looping page fields in pivot table
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-13-2006, 06:15 PM
  7. How do I set up filter for page fields in pivot table?
    By Mitsycat in forum Excel General
    Replies: 3
    Last Post: 05-06-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1