Solved

pivot filter - not dynamic!

Posted on 2014-02-19
3
395 Views
Last Modified: 2014-02-20
Hello

I just recorded a macro - but don’t find it dynamic.
There is a pivot table that has a pivot filter. Now if you press the button, it reset to certain pivot filter values. If you see the macro-

 it basically turns on all the field values >>
ActiveSheet.PivotTables("PivotTable1").PivotFields("Region").CurrentPage = _
        "(All)"

 and then switches off all the remaining values.  >>
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Region")
        .PivotItems("West").Visible = False
        .PivotItems("(blank)").Visible = False
    End With



This code is not at all dynamic and everytime the data changes, I have to  adjust it? This is not dynamic. How do I accomplish the same goal by just telling the vba code to selectively filter for a set of values (Central and East) - ?
issuePlease.xlsm
0
Comment
Question by:Rayne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Assisted Solution

by:regmigrant
regmigrant earned 50 total points
ID: 39871951
Your pivot table is counting no of units rather than summing them so unless the number of records change the count won't change regardless of changes to the number of units.
- change the field settings on no.of units to 'sum' instead of count

Pivot tables are not dynamic they need to be refreshed if data changes - add the following to the code:-

 Dim pt As PivotTable
 Set pt = ActiveSheet.PivotTables("PivotTable1")
 
 pt.RefreshTable

before it updates the table and it will incorporate the new data as part of the reset.

The attachment contains these revisions

You could also put similar code into the 'worksheet.activate' private module for sheet4 to ensure the refresh happens as the sheet is activated.


it would be possible to choose which regions to include/exclude in the filter by passing them in as variables but this would be the same as selecting them from the list in the drop down. If the number of regions is static then you could consider having a separate button to enable each region and a reset button to turn them all off - or a form with a checklist to mirror the content of the drop down but this seems like overkill given that the dialog is already in place within the filter button
Copy-of-issuePlease.xlsm
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 450 total points
ID: 39873565
You can use a loop like this:
   Dim pf                          As PivotField
   Dim pi                          As PivotItem
   
   Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("Region")
   With pf
      .CurrentPage = "(All)"
      For Each pi In .PivotItems
         pi.Visible = (pi.Name = "East" Or pi.Name = "Central")
      Next pi
   End With

Open in new window

0
 

Author Comment

by:Rayne
ID: 39875159
That’s a brilliant life saver, thanks Rory, you rock as always
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question