+ Reply to Thread
Results 1 to 7 of 7

check filter items in pivot table

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010
    Posts
    9

    check filter items in pivot table

    Hi

    In the sample code below, I am using the data from cell C3 ( a drop down list) as a filter value. This works OK if the value in C3 matches an item in the filter list. However if the data in C3 does not match any of the filter items in the pivot field filter list, a VBA error is generated (understandably). How can I check the filter options available in the filter list and then generate an error msgbox if there is no match.

    Note I specifically do not want the user to have search each pivot table in my workbook manually.

    ActiveSheet.PivotTables("PivotTable4").PivotFields("Inclusion").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable4").PivotFields("Inclusion").CurrentPage = Range("C3").Value
    ActiveSheet.PivotTables("PivotTable4").PivotCache.Refresh

    Thanks

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: check filter items in pivot table

    http://www.cpearson.com/excel/errorhandling.htm
    read here to see what error handling is

    and then go here see post #4 of this thread and see how the error handler could work with pivot table
    http://www.excelforum.com/excel-prog...tachement.html

    the code in the thread is for current month but it can be easily changed to point to range("C3")
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    06-26-2014
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010
    Posts
    9

    Re: check filter items in pivot table

    Thanks Humdingaling for responding but I am missing a point somewhere. I have modified my code as follows:

    Sub Location1()

    (1) ActiveSheet.PivotTables("PivotTable4").PivotFields("Inclusion").ClearAllFilters
    (2) ActiveSheet.PivotTables("PivotTable4").PivotCache.Refresh
    (3) ActiveSheet.PivotTables("PivotTable4").PivotFields("Inclusion").CurrentPage = Range("C3").Value
    (4) On Error GoTo errormsg:
    (5) ActiveSheet.PivotTables("PivotTable4").PivotCache.Refresh

    (6) errormsg:
    (7) MsgBox "error message"
    End Sub
    .....................
    I have numbered the lines for this post
    If the content of C3 exists in the pivot table list, then the msgbox is displayed. This (in my mind) should not be happening!
    If the content does not exist in the pivot table list then I get the VBA error 1004 message. This is when I expect the msgbox rather than the VBA error code.

    I thought the error handling in line (4) would have worked (wrong!)
    It now seems to me that maybe I need to somehow check the content of C3 against the pivot table list immediately before line (3)and if C3 does not exist in the pivot table list,then go
    to the msgbox before executing lines (3) onwards.

    If this assumption is correct, how can I check if C3 content is (or is not)available in the pivot table list? Otherwise help!

    Thanks

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: check filter items in pivot table

    probably would of been easy for you to provide a sample file

    i think you have the premise correct except if there is no "error" you will not get a msgbox

    i can tell from your code you are missing an "exit sub" between 5-6

  5. #5
    Registered User
    Join Date
    06-26-2014
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010
    Posts
    9

    Re: check filter items in pivot table

    Thanks again

    The 'exit sub' was a bit too obvious, but thanks!

    Now my remaining problem. Assume a filter list looks like:

    .....(All)
    .....L1 Duct
    ..... Blank

    If I now enter 'L3 Duct' into my cell C3, I want to be able to check if 'L3 Duct' against the list in the filter box and if not, generate a MSGBOX.

    I am approaching this by trying to refer to the field list but I cannot establish how to lookup the field list that is displayed in the filter box?
    Attached Images Attached Images

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: check filter items in pivot table

    not quite sure what the issue is
    the error message only pops up if the filter is not on the list

    are you wanting to find anything "similar"
    ie type duct and filters all things with duct ?

    it may actually be easier to check against the source data
    do a pre-check against source data before going into the pivot table code

  7. #7
    Registered User
    Join Date
    06-26-2014
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010
    Posts
    9

    Re: check filter items in pivot table

    Thanks again Humdingaling. Your last post introducing some lateral thinking, which has solved the problem.

    Previously I was trying to filter by using the text from my cell C3. If the C3 data did not appear in the filter list, when the macro ran an error was generated.

    Now I am AND-ing C3 data with the data in the 'key data' column and placing a TRUE/FALSE result into my 'inclusion' column
    (Previously I was placing the content of the 'key data' (e.g. L1 duct, L2 duct, etc) column into the 'inclusion' column when the data matched C3).

    Example:
    C3 data = L1 duct

    key data Inclusion
    L1 duct True (C3 data & L1 duct = TRUE)
    L2 duct False (C3 data & L2 duct = FALSE)
    L1 duct True (C3 data & L1 duct = TRUE)
    L3 duct False (C3 data & L3 duct = FALSE)

    Now my pivot table macro only ever filters for data associated with the TRUE results.

    ActiveSheet.PivotTables("PivotTable4").PivotFields("Inclusion").CurrentPage = True

    Thanks again, all sorted!

+ 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. Count Multiple Items in a Pivot Table filter
    By Karen13 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 09-16-2015, 05:49 AM
  2. Replies: 1
    Last Post: 07-16-2015, 05:46 AM
  3. Filter pivot items using pivot items from another table
    By DKolev in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2014, 08:49 PM
  4. Macro that would filter pivot table top 10 items
    By weecha in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2013, 01:28 PM
  5. Change Pivot table Filter Based on Cell Value *Multiple Filter items* Possible?
    By Flydd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2012, 06:57 AM
  6. Rename items in report filter in Pivot table
    By jenzz22 in forum Excel General
    Replies: 1
    Last Post: 03-07-2012, 02:36 PM
  7. Select and unselect items in a filter in pivot table
    By TPFKAS in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-10-2010, 09:39 AM

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