Consulting

Results 1 to 5 of 5

Thread: Loop through all Pivot tables in a worksheet and filter visability VBA

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location

    Loop through all Pivot tables in a worksheet and filter visability VBA

    I have what I thought was to be a straightforward task to accomplish, but two days later I cannot figure out how to do this.

    I've posted the simplest code of what I want to accomplish (for a single PivotTable). This code will turn off visibility of Name1-6 in Pivottable1. What I need the code to accomplish is to apply this filter to every PivotTable in my sheet (not workbook, only the activesheet), approximately 50 of them. Also, in there are instances where "Name2" or "Name4", etc. does not occur in a PivotTable. I would like the code to move on to the next name rather than throw an error.

    Excel 2013


    Sub filterPivot()
    
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Memo")
            .PivotItems("Name1").Visible = False
            .PivotItems("Name2").Visible = False
            .PivotItems("Name3").Visible = False
            .PivotItems("Name4").Visible = False
            .PivotItems("Name5").Visible = False
            .PivotItems("Name6").Visible = False
        End With
    End Sub

    Thanks to anyone who can help me out!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I don't think I've ever heard of 50 pivot tables on a single sheet, but try this


    Option Explicit
    
    Sub filterPivot()
        Dim pt As PivotTable
        
        On Error Resume Next
        For Each pt In ActiveSheet.PivotTables
            With pt.PivotFields("Memo")
                .PivotItems("Name1").Visible = False
                .PivotItems("Name2").Visible = False
                .PivotItems("Name3").Visible = False
                .PivotItems("Name4").Visible = False
                .PivotItems("Name5").Visible = False
                .PivotItems("Name6").Visible = False
            End With
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location
    Hi Paul,

    Thanks for your help.

    I tried your code and it only worked on the first PT in the worksheet, which is PivotTable49.

    Regards,

    greg

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Hmmm, should have worked (famous last words)

    If you want, try this version

    I added two Msgboxs and moved the On Error to which handles no pivot field named Memo and no PivotItem = Namex

    Option Explicit
    
    Sub filterPivot()
    
        Dim pt As PivotTable
         
        MsgBox ActiveSheet.PivotTables.Count
        
        For Each pt In ActiveSheet.PivotTables
            
            MsgBox pt.Name
            
            On Error Resume Next
            With pt.PivotFields("Memo")
                .PivotItems("Name1").Visible = False
                .PivotItems("Name2").Visible = False
                .PivotItems("Name3").Visible = False
                .PivotItems("Name4").Visible = False
                .PivotItems("Name5").Visible = False
                .PivotItems("Name6").Visible = False
            End With
            On Error GoTo 0
        Next
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location
    Paul,

    I figured it out... needed to add pt.update and pt.refresh.

    Thanks for your help!

    Sub filterPivot()
    
        Dim pt As PivotTable
    
        For Each pt In ActiveSheet.PivotTables
        
            pt.RefreshTable
            pt.Update
    
    
            On Error Resume Next
            With pt.PivotFields("Memo")
                .PivotItems("Name1").Visible = False
                .PivotItems("Name2").Visible = False
                .PivotItems("Name3").Visible = False
                .PivotItems("Name4").Visible = False
                .PivotItems("Name5").Visible = False
                .PivotItems("Name6").Visible = False
    
            End With
            On Error GoTo 0
            
        Next
    
    End Sub

Posting Permissions

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