+ Reply to Thread
Results 1 to 3 of 3

Set Pivot Table Filters across Worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    75

    Set Pivot Table Filters across Worksheets

    I am attempting to set up a macro that will cycle through the worksheets in my file and update my pivot tables.

    The below code will cycle through each worksheet and each pivot table:

    Dim wks As Worksheet
    Dim pvt As PivotTable
    
    For Each wks In Worksheets
        For Each pvt In wks.PivotTables
           
    
        Next pvt
    Next wks
    the next step is where I'm having issues. I want to update two filters on each pivot table (all the same) . I attempted to record a macro to figure out how to get it done, but can't figure out how to incorporate it into my for /next loops:
        ActiveSheet.PivotTables("PivotTable1").PivotFields("FilterName1").CurrentPage = _
            "(All)"
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("FilterName1")
            .PivotItems("Name1").Visible = True
            .PivotItems("Name2").Visible = True
            .PivotItems("Name3").Visible = True
            .PivotItems("Name4").Visible = True
        End With
        ActiveSheet.PivotTables("PivotTable1").PivotFields("FilterName2").CurrentPage = "(All)"
        With ActiveSheet.PivotTables(pvt).PivotFields("FilterName2")
            .PivotItems("Item1").Visible = True
            .PivotItems("Item2").Visible = True
            .PivotItems("Item3").Visible = True
            .PivotItems("Item4").Visible = True
            .PivotItems("Item5").Visible = True
        End With
    I get the error "Method 'PivotTables' of object'_worksheet' failed." when I try the below code:
    Dim wks As Worksheet
    Dim pvt As PivotTable
    
    
    For Each wks In Worksheets
        For Each pvt In wks.PivotTables
        
        wks.PivotTables(pvt).PivotFields("PO Type").CurrentPage = _
            "(All)"
        With wks.PivotTables(pvt).PivotFields("FilterName1")
            .PivotItems("Name1").Visible = True
            .PivotItems("Name2").Visible = True
            .PivotItems("Name3").Visible = True
            .PivotItems("Name4").Visible = True
        End With
        wks.PivotTables(pvt).PivotFields("SBU").CurrentPage = "(All)"
        With wks.PivotTables(pvt).PivotFields("FilterName2")
            .PivotItems("Item1").Visible = True
            .PivotItems("Item2").Visible = True
            .PivotItems("Item3").Visible = True
            .PivotItems("Item4").Visible = True
            .PivotItems("Item5").Visible = True
        End With
    
        Next pvt
    Next wks

    any guidance is appreciated.

  2. #2
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: Set Pivot Table Filters across Worksheets

    Ok,

    SO I've partially figured out my issue. I can't utilize the following code:
    With wks.PivotTables(pvt).PivotFields("FilterName1") ' I also can't use: With wks.PivotTables(pvt.Name).PivotFields("FilterName1")
            .PivotItems("Name1").Visible = True
            .PivotItems("Name2").Visible = True
            .PivotItems("Name3").Visible = True
            .PivotItems("Name4").Visible = True
    End With
    I have to set a variable and use that variable in my code as such:
     
    pvtName = pvt.Name 
    
    With wks.PivotTables(pvtName).PivotFields("FilterName1")
            .PivotItems("Name1").Visible = True
            .PivotItems("Name2").Visible = True
            .PivotItems("Name3").Visible = True
            .PivotItems("Name4").Visible = True
    End With
    my follow up question is regarding the filters. How do I deselect all the options I don't want and just the ones listed in the code above. the only way I've found thus far is to set the .Visible property to False, but I don't know what values may or may not be in the filter to set them to False...

    I tried
            .PivotItems("(All)").Visible = False
    but got an error.

    Anyone have insight on clearing filters

  3. #3
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: Set Pivot Table Filters across Worksheets

    So I have a 99% solution coded below:

    Sub Set_Filters()
    Dim wks As Worksheet
    Dim pvt As PivotTable
    
    Application.ScreenUpdating = False
    
    For Each wks In Worksheets  'cycles through all worksheets
        For Each pvt In wks.PivotTables     'cycles through all pivot tables on the worksheet
        Worksheets(wks.Name).Select 'sets focus to Worksheet being manipulated
        pvtName = pvt.Name
    
    ' Set Filter1 filter:
        ActiveSheet.PivotTables(pvtName).PivotFields("Filter1").ClearAllFilters
        ActiveSheet.PivotTables(pvtName).PivotFields("Filter1").EnableMultiplePageItems = True
        With ActiveSheet.PivotTables(pvtName).PivotFields("Filter1")
        For i = 1 To .PivotItems.Count - 1
            .PivotItems(.PivotItems(i).Name).Visible = False
        Next i
            .PivotItems("Item1").Visible = True
            .PivotItems("Item2").Visible = True
            .PivotItems("Item3").Visible = True
            .PivotItems("Item4").Visible = True
        End With
    
    ' Set Filter2 filter:
        ActiveSheet.PivotTables(pvtName).PivotFields("Filter2").ClearAllFilters
        ActiveSheet.PivotTables(pvtName).PivotFields("Filter2").EnableMultiplePageItems = True
        With ActiveSheet.PivotTables(pvtName).PivotFields("Filter2")
        For i = 1 To .PivotItems.Count - 1
            .PivotItems(.PivotItems(i).Name).Visible = False
        Next i
            .PivotItems("Item1").Visible = True
            .PivotItems("Item2").Visible = True
            .PivotItems("Item3").Visible = True
            .PivotItems("Item4").Visible = True
            .PivotItems("Item5").Visible = True
            .PivotItems("#VALUE!").Visible = False
        End With
        
        Next pvt
    Next wks
    
    Application.ScreenUpdating = True
    
        Worksheets("First Worksheet").Select
                Range("A1").Select
        MsgBox "Pivot Table Filters set", vbExclamation, "Reset Pivot Filters"
    End Sub
    The issue I'm still working on is in the For Loop. It won't uncheck the last item in each filter. For the first filter, it's not currently an issue since the last item is an item I want included anyway. for the last filter, I had to hard code turning off #VALUE! hoping it will always be the last item in the list.

    Is there a function that can select the last filter item? This way, I could run the code listed above, setting the .Visible = True tags (or just one of them) to the one(s) I want, unselect the last item in the filter list, then reapply the .Visible = True code.

+ 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. [SOLVED] Speed up creating worksheets for all filters in pivot table
    By hdinkie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2016, 08:01 AM
  2. Replies: 1
    Last Post: 07-16-2015, 05:46 AM
  3. Pivot Table Filters
    By marcrist in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 05-01-2014, 06:39 AM
  4. [SOLVED] Pivot Table filters
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-06-2013, 05:35 AM
  5. [SOLVED] What filters to use in pivot table
    By glenath in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 01-24-2013, 01:04 AM
  6. [SOLVED] Pivot Table Filters
    By gsmcconville in forum Excel General
    Replies: 0
    Last Post: 04-13-2012, 05:31 AM
  7. VBA for Pivot Table Filters
    By Coldsteel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-28-2010, 09:03 AM

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