Solved

pivot filter - not dynamic!

Posted on 2014-02-19
3
396 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Outlook Free & Paid Tools
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 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