Solved

pivot filter - not dynamic!

Posted on 2014-02-19
3
386 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
Comment Utility
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
Comment Utility
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
Comment Utility
That’s a brilliant life saver, thanks Rory, you rock as always
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
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…

772 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

13 Experts available now in Live!

Get 1:1 Help Now