+ Reply to Thread
Results 1 to 5 of 5

Pivottable - cycle through each PivotItem

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Pivottable - cycle through each PivotItem

    Hello everybody

    Just can't quite figure out how to crack this one.
    Trying to cycle through each item in pivotfield, then copy the tablerange1 to a new sheet.

    Everything works with the code EXCEPT that the pivottable values do not update.
    Here is the code that I've put together. How do I get the pivottable values to update for each pivotitem?

      Sub Test()
    Dim Ws As Worksheet
        Dim Pt1 As PivotTable
        Dim pf As PivotField
        Dim pi As PivotItem
        
        Set Ws = ActiveSheet
        Set Pt1 = Ws.PivotTables(1)
        
        'select only first item in list
        For Each pi In Pt1.PivotFields("closed month").PivotItems
            If pi.name = "July, 2013" Then
            pi.Visible = True
            End If
        Next pi
        
        For Each pi In Pt1.PivotFields("Closed Month").PivotItems
                Pt1.TableRange1.Copy
                Sheets.Add after:=Ws
                ActiveSheet.name = Right(pi.Caption, 4) & Left(pi.Caption, 3)
                Range("a4").Select
                With Selection
                    .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                        :=False, Transpose:=False
                    .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                        SkipBlanks:=False, Transpose:=False
                    .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                        SkipBlanks:=False, Transpose:=False
                End With
                Application.CutCopyMode = False
                Ws.Select
        Next pi
    
        MsgBox "Done"
    Last edited by rasonline; 01-20-2014 at 05:22 AM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,897

    Re: Pivottable - cycle through each PivotItem

    how about:
    Pt1.PivotCache.Refresh
    PS. after making sure that July is visible
        For Each pi In Pt1.PivotFields("closed month").PivotItems
            If pi.name = "July, 2013" Then
            pi.Visible = True
            End If
        Next pi
    it might (or not - test it) be good idea to hide other:

        For Each pi In Pt1.PivotFields("closed month").PivotItems
            If pi.name <> "July, 2013" Then
            pi.Visible = False
            End If
        Next pi
    Last edited by Kaper; 01-18-2014 at 04:33 AM.

  3. #3
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Pivottable - cycle through each PivotItem

    hI kaper

    thanks for a quick response.
    Regardng the month selection, you were correct. I needed to update the code to ensure that everything else is not visible.

    The pivotcache suggestion didn't work.
    I suspect the problem is that I'm not actually selecting the pivotitem. I need to find a way to select the pivotitem, then do the copy-paste.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,897

    Re: Pivottable - cycle through each PivotItem

    So probably it is worth considering part of: http://www.excelforum.com/forum-rule...rum-rules.html (near bottom):
    Post a WORKBOOK. Nobody wants to type data from a picture or paste text from your post into a spreadsheet as a prelude to helping. To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

  5. #5
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Pivottable - cycle through each PivotItem

    Seeing as I was only going to use this code once off, I went for a slightly different approach.

    Essentially, the pivotitem needed to be selected using the .visible property.
    In the original code in post 1 above, there isn't anything to change the property for each pi.

    Here is the code that I used -

    The references to "n" can be substituted with "For each pi in pivotfields....."
    The learning point here is that the pi needs to be set to visible and other to false.

    Thanks for your post though as it guided me in the right direction.

    Sub Monthly_Movement_History()

    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim ws As Worksheet
    Dim MthSelection As String

    Set ws = ActiveSheet
    Set pt = ActiveSheet.PivotTables(1)

    For n = 1 To 48
    MthSelection = WORKSHEETS("Pi.list").Range("A" & n).Value 'the list of all months (pivotitems) are listed on a separate sheet Pi.list

    'first clear filters on Closed Month field
    pt.PivotFields("Closed Month").ClearAllFilters

    For Each pi In pt.PivotFields("Closed Month").PivotItems
    If pi.name = MthSelection Then
    pi.Visible = True
    Else: pi.Visible = False
    End If
    Next pi

    pt.TableRange1.Copy
    WORKSHEETS.Add after:=ws
    ActiveSheet.name = Right(MthSelection, 4) & Left(MthSelection, 3)
    Range("A1").Select

    With Selection
    .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    End With
    ws.Select
    Next n

    End Sub

+ 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. Replies: 0
    Last Post: 06-14-2013, 12:33 PM
  2. [SOLVED] PivotItems in a PivotTable position -- Skip if PivotItem errors
    By bcn1988 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-28-2013, 10:03 AM
  3. Replies: 0
    Last Post: 07-27-2006, 03:35 PM
  4. [SOLVED] Set PivotItem using counter
    By CinqueTerra in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-02-2005, 01:40 PM
  5. PivotItem positioning...
    By Jesterhoz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2005, 10:05 PM

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