Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pivot filter - not dynamic!

Posted on 2014-02-19
3
Medium Priority
?
402 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 200 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 1800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

721 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