+ Reply to Thread
Results 1 to 5 of 5

Print selection for each PivotItem

Hybrid View

  1. #1
    Registered User
    Join Date
    12-08-2016
    Location
    Rotterdam, the Netherlands
    MS-Off Ver
    2016
    Posts
    24

    Print selection for each PivotItem

    Hi all,

    I'm writing a script that prints a specific selection of a pivot table that can be filtered for each day of the week (so Monday, Tuesday, Wednesday etc).

    The pivot table has a row called "Dayname" which contains each day of the week. The script uses the range provided in cells J2 and K2.

    I've looked around and came up with this script but it is not filtering the pivot table for each day to then print the selection.

    Does anyone know how I can best do this?

     
    Sub PrintSelection()
    
    Dim Cval1 As Variant
    Dim Cval2 As Variant
    Dim Rng As Range
    
    Dim Table As PivotTable
    Dim All As Range
    Dim PvI As PivotItem
    
        Set All = Worksheets("PIVOT").Range("A1:AZ10000")
        Set Table = Worksheets("PIVOT").PivotTables("PivotSchedule")
        For Each PvI In Table.PivotFields("Dayname").PivotItems
    
            Cval1 = ActiveSheet.Range("J2").Value
            Cval2 = ActiveSheet.Range("K2").Value
            Set Rng = Range(Cval1 & ":" & Cval2, Selection.End(xlDown))
            Rng.Select
            Application.PrintCommunication = True
            Selection.PrintOut Copies:=1, Collate:=True
    
        Next
    End Sub
    Attached Files Attached Files
    Last edited by AlexJr; 12-16-2016 at 03:00 PM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Print selection for each PivotItem

    I am not sure what you are trying to print. Are you trying to print everything from row 6 cols A:H on down to last item in the pivot table?

    In that case, you might look at the following range Table.TableRange1.

    Take a look at this reference: http://peltiertech.com/referencing-p...ranges-in-vba/
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    12-08-2016
    Location
    Rotterdam, the Netherlands
    MS-Off Ver
    2016
    Posts
    24

    Re: Print selection for each PivotItem

    Okay. Thanks! I've cleaned up the code. The selection part was actually working, although more tedious. Your link is much better.

    I can't get the script to filter to each DayName first, to then print the tablerange.

    Would you know what is wrong?

     
    Sub PrintSelection()
    
    Dim Table As PivotTable
    Dim PvI As PivotItem
    
        Set Table = Worksheets("PIVOT").PivotTables("PivotSchedule")
        For Each PvI In Table.PivotFields("Dayname").PivotItems
            Table.TableRange1.Select
            Selection.PrintOut Copies:=1, Collate:=True
        Next
    End Sub

  4. #4
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Print selection for each PivotItem

    I understand you want to set each day and then print it.

    One issue with multiple selections with pivot table filters is that you must always have at least one item selected or you will get an error. For example if the only item selected is item 3 and you intend on turning on item 4 and you code attempts to turn off item 3 before turning anything back on, you will get an error.

    So you have to do a double loop. First you have to turn everything on. Then you have to go back and turn off all those things you don't want to see.

    In the code, I defined PvI as a PivotItem just to keep from having to type too much. I also introduced two more variables iI which is the item index - there are 8 days: Mon - Sun + (Blank) and they can be pointed to by their position. The CountI variable keeps track of what position I want to print.

    The only other thing I did was put in a message box to stop and show me the intermediate filter selections so I could QA it and I commented out your printout command. You can delete the msgbox and uncomment your command when you want to go live with the function.

    If you have any questions about the logic or anything else I did, please ask.
    Attached Files Attached Files

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Print selection for each PivotItem

    You might also use a label filter rather than looping through all the items so many times
    Sub PrintSelection()
    
    Dim Table As PivotTable     ' Pivot Table
    Dim PvF As PivotField       ' Pivot Field
    Dim dayList As Variant, theDay As Variant
    
    dayList = Split("Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday", "|")
    ' Initalize variables
    Set Table = Worksheets("PIVOT").PivotTables("PivotSchedule")
    Set PvF = Table.PivotFields("Dayname")
      
    For Each theDay In dayList
        PvF.ClearAllFilters
        PvF.PivotFilters.Add Type:=xlCaptionEquals, Value1:=theDay
        Table.TableRange1.Select
        MsgBox "Stop and look"
        'Selection.PrintOut Copies:=1, Collate:=True
    Next theDay
        
    End Sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ 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. PivotItem loop with getpivotdata - hide pivotitem
    By csutera in forum Excel General
    Replies: 1
    Last Post: 01-14-2016, 10:41 AM
  2. Change from last print preview selection to the Print Active Sheets
    By Tiger20 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-13-2015, 09:53 AM
  3. Default Settings in Backstage Print to "Print Selection
    By excelforumkeys in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-10-2014, 06:12 PM
  4. Macro to filter pivotitem resulting in incorrect month selection
    By Avinash1234 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-13-2014, 02:35 AM
  5. Replies: 0
    Last Post: 06-14-2013, 12:33 PM
  6. How to print a selection to PDF
    By amartino44 in forum Excel General
    Replies: 2
    Last Post: 04-09-2013, 09:05 PM
  7. [SOLVED] How can I print frozen panes to appear above print selection in Ex
    By Snr Franco in forum Excel General
    Replies: 1
    Last Post: 04-02-2006, 10:10 AM

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