Solved

pivot filter - not dynamic!

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office2007 Training Material Required 2 41
Cascading dropdown 9 26
Create Form using Wizard 14 31
Incorporate VBA Code to work with Original Workbook 23 27
My experience with Windows 10 over a one year period and suggestions for smooth operation
Outlook Free & Paid Tools
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now