+ Reply to Thread
Results 1 to 7 of 7

PivotTable filters based on cell value, if the value doesn't match it shows me everything

  1. #1
    Registered User
    Join Date
    06-05-2020
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    4

    Question PivotTable filters based on cell value, if the value doesn't match it shows me everything

    Hello all!

    So I've got a VBA code on the worksheet. This basically says to filter the PivotTable based on the value in B2. However, when the value in B2 is blank OR does not match with anything, it will show me everything in the PivotTable.

    Is there any way to change this to make it show nothing if the value in B2 doesn't match anything in the filter?

    The code I used on the worksheet is the following:
    Please Login or Register  to view this content.
    I'm pretty clueless regarding VBA, so the solution might be really simple. Any help would be greatly appreciated!

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,880

    Re: PivotTable filters based on cell value, if the value doesn't match it shows me everyth

    In pivot table you must at least have 1 Label value selected. Otherwise it will throw error.

    To work around this, you could add blank row to Pivot Table (i.e. add blank in source table) and use that when nothing is selected or selected value not found.

    I'd recommend uploading desensitized sample workbook. It will make it so much easier for us to help you.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    06-05-2020
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    4

    Re: PivotTable filters based on cell value, if the value doesn't match it shows me everyth

    Quote Originally Posted by CK76 View Post
    In pivot table you must at least have 1 Label value selected. Otherwise it will throw error.

    To work around this, you could add blank row to Pivot Table (i.e. add blank in source table) and use that when nothing is selected or selected value not found.

    I'd recommend uploading desensitized sample workbook. It will make it so much easier for us to help you.
    Oh, that might be the issue then. Before I tried this I did it with a regular table and different code and that would show me only what I put in the cell, but that method would cause for more maintenance. I'm thinking about recording a macro to maybe automate the updating of that regular table, but if it's possible with the Pivot Table it would be a lot nicer.

    The source is actually a Query from an ERP-system, so I don't think it would work to put in a blank there.

    I made a desensitized sample workbook to make it a bit easier for anyone trying to help if theres still an alternate solution.

    Thanks for the reply!
    Attached Files Attached Files

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,880

    Re: PivotTable filters based on cell value, if the value doesn't match it shows me everyth

    The source is actually a Query from an ERP-system
    Are you using PowerQuery to connect to ERP-system and bring in data? If so, you can always append blank row to the query using M code.

    If not, then I assume you are using querytable (i.e. legacy method like MS Query) to bring in data. And building Pivot Table from resulting table.

    In that case, you can use code to load table data + 1 row into data model and use that to base your pivot table on.

    Attached file is sample I created for merging two tables in memory and load to data model for creating pivot table.
    You can skip merge portion and use that as basis for creating pivot table as needed.
    By using something like... Range("AddressRange").Resize(Range("AddressRange").Rows.Count + 1 as source.

    Though I'd strongly suggest using PowerQuery to bring in data, perform any necessary transformation, then load it directly to data model.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-05-2020
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    4

    Re: PivotTable filters based on cell value, if the value doesn't match it shows me everyth

    Quote Originally Posted by CK76 View Post
    Are you using PowerQuery to connect to ERP-system and bring in data? If so, you can always append blank row to the query using M code.

    If not, then I assume you are using querytable (i.e. legacy method like MS Query) to bring in data. And building Pivot Table from resulting table.

    In that case, you can use code to load table data + 1 row into data model and use that to base your pivot table on.

    Attached file is sample I created for merging two tables in memory and load to data model for creating pivot table.
    You can skip merge portion and use that as basis for creating pivot table as needed.
    By using something like... Range("AddressRange").Resize(Range("AddressRange").Rows.Count + 1 as source.

    Though I'd strongly suggest using PowerQuery to bring in data, perform any necessary transformation, then load it directly to data model.
    Thanks for the help. I'm pretty sure it's the second option yeah, not a PowerQuery. I tried making sense of your sample but I just really don't understand 99% of it to be honest.

    It merges the 2 tables, and then creates a new pivot table?

    Is it maybe possible to somehow make the Pivot Table add that + 1 row like you said without creating a new one?

    I'm trying to keep it as simple as possible since it's for my thesis and I'll be gone from the company soon after so I can't make it too complex (not that I'm able to make something complex myself). Thanks for the help anyway. If what I asked doesn't have an easy solution (or at least not too someone who doesn't know a thing about VBA) maybe I'll just use the not-as-ideal solution I mentioned before.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,880

    Re: PivotTable filters based on cell value, if the value doesn't match it shows me everyth

    Ok, here's mockup of concept.

    I used dynamic named range as source for pivot table.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-05-2020
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    4

    Re: PivotTable filters based on cell value, if the value doesn't match it shows me everyth

    Quote Originally Posted by CK76 View Post
    Ok, here's mockup of concept.

    I used dynamic named range as source for pivot table.

    Please Login or Register  to view this content.
    Sorry for the late reply. I finally got it to work thanks to you! Thanks a lot for the help, I really appreciate it.

+ 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. After updating PivotTable Source data with Macro, slicer doesn't show PivotTable
    By mrdouglaswee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-17-2018, 02:10 AM
  2. PivotTable Filters Disappearing
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-13-2014, 09:35 AM
  3. Updating PivotTable filters based on other cell value
    By dalew in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-12-2012, 08:18 PM
  4. Replies: 3
    Last Post: 04-24-2012, 02:06 PM
  5. Replies: 9
    Last Post: 02-26-2012, 07:20 AM
  6. Excel 2007 : PivotTable: how to manage filters?
    By Luk3tto in forum Excel General
    Replies: 0
    Last Post: 06-19-2009, 07:19 AM
  7. Excel should have a drop down box that shows all the filters on
    By dasmjp in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-21-2006, 05:20 PM

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