+ Reply to Thread
Results 1 to 8 of 8

Pivot item filtering performance with VBA

  1. #1
    Registered User
    Join Date
    03-29-2014
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    5

    Angry Pivot item filtering performance with VBA

    Hi Guys,

    I'm having a performance/speed issue when trying to apply a set of filters to a pivot table using VBA.

    In my excel doc - I have a tab called "FilterTable" which I hold a list of pivot field names and pivot field items - in columns A and B of the tab - starting on the first row - as shown below:

    ---------------------FilterTable---------------------
    Gender Male
    City London
    Department Finance


    This tab represents the set of filters that I wish to apply to my pivot table - so for example in this case to filter on Gender = "Male" and City = "London" and Department = "Finance"


    To actually apply the filters - I have written the following VBA:

    Please Login or Register  to view this content.

    The code works fine - but it is just incredibly inefficient - as say for example I have 100 departments - I would need to loop through all of these and set all the field items that I do not want to be visible to false (takes about 0.5 sec for each item). And obviously the more filters I apply - the worse the performance will become......I don't even want to think about how this would work when it comes to filtering on dates!!!!

    I am aware that this vba will not work for multiple value filtering on the same field - but i'm just giving an example to highlight the issue I am having.

    I have done the following which has not improved performance to the level I require:
    • application.calculation =xlmanual
    • application.enableevents = false
    • application.screenupdating = false
    • pivottable.manualupdate = false

    ----

    I'm not that experienced with pivioting/vba - so I think there must be a more efficient way to filter in VBA using pivot tables that doesn't involve iterating around all items in a field and setting them to false. Maybe one of you lovely people can help me out !!!

    Many Thanks!


  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pivot item filtering performance with VBA

    Hi,

    You should actually use pivottable.manualupdate = True to speed up processing. Also, using the PivotFilters.Add method for each pivot field should be faster than looping.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    03-29-2014
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Pivot item filtering performance with VBA

    Thanks xlnitwit. I meant to write pivottable.manualupdate = True

    So I have changed my code to the following based on your advice:

    Please Login or Register  to view this content.

    ----

    However - whilst this does apply a filter to the pivot table (i can see it applied on the actual table in the pivot table details view) - it doesn't actually filter the results at all.

    I tried to do this action manually on the pivot table - as per the picture below - and it also had no action on the pivot table data.
    manual.png

    I'm also not sure whether the approach you suggest will work when it comes to applying multiple filters to one field - e.g. filtering on department = "Finance" or "Sales"

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pivot item filtering performance with VBA

    I can't really tell anything from that picture I'm afraid. A filter should work, assuming you are applying it to a field that is present in the table. (otherwise, you need a slicer)

  5. #5
    Registered User
    Join Date
    03-29-2014
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Pivot item filtering performance with VBA

    Hi xlnitwit - thats right I don't have the field in the table - and it cannot go in the table as I need my table in a certain format for a graph.

    I do have all of the fields as slicers though - however I don't believe there is a method like:

    pf.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:=myFilterItem

    for slicers???

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pivot item filtering performance with VBA

    For a slicer, you actually need to loop through the items in its slicercache thus- http://www.jkp-ads.com/Articles/slicers04.asp

    Otherwise, applying a filter to a field that is not in the table will have no effect on the table.

  7. #7
    Registered User
    Join Date
    03-29-2014
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Pivot item filtering performance with VBA

    but i'm guessing looping through the items of the slicer is going to run in to exactly the same performance issues as those I experienced with looping through pivottable items.....

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pivot item filtering performance with VBA

    I wouldn't have thought it would be too bad as long as you turn off screenupdating, events and set the table's manualupdate property.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code to create pivot for single pivot item, if required item is not available cre
    By Ishwarind in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-04-2016, 11:06 AM
  2. [SOLVED] Filtering and Possible Performance Issue
    By rau in forum Excel General
    Replies: 9
    Last Post: 07-02-2013, 06:58 PM
  3. Filtering by entering a an item in a Cell
    By Statue in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-15-2013, 09:00 AM
  4. Macro for selecting a single item while filtering the data in Pivot Table
    By Syed964 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2013, 01:28 PM
  5. filtering more than one item in the same column
    By zazzz in forum Excel General
    Replies: 13
    Last Post: 09-19-2012, 12:18 PM
  6. Does Excel have an item limit when filtering?
    By Jacko at DHL in forum Excel General
    Replies: 2
    Last Post: 03-30-2006, 12:10 PM
  7. Pivot Item: removing non-existant item from the drop down
    By CinqueTerra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-23-2006, 04:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1