+ Reply to Thread
Results 1 to 6 of 6

Detecting Whether Value is in Slicer

  1. #1
    Registered User
    Join Date
    09-09-2016
    Location
    UK
    MS-Off Ver
    2013
    Posts
    3

    Detecting Whether Value is in Slicer

    Hi guys

    Problem: I have a list of cities. Some of these cities have population data and some don't. I have a slicer which is related only to the cities which do have population data (i.e. cities with no data do not appear in the slicer). Basically, I have written a macro which goes through the total list of all cities and, one by one, selects them in the slicer and reports a value from an associated PivotTable. However, this doesn't entirely work as the macro encounters cities which are not in the slicer and gives me an error.

    Question: How could I write VBA code to detect whether a city is in the slicer or not? Ideally, I want something like 'If City is in slicer, then...'.

    Thanks in advance!

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Detecting Whether Value is in Slicer

    Try this:
    Please Login or Register  to view this content.

    You can use it like this:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Detecting Whether Value is in Slicer

    Of course, you could use a similar principle to just loop through the slicer items which already exists, and do your reporting with those...

  4. #4
    Registered User
    Join Date
    09-09-2016
    Location
    UK
    MS-Off Ver
    2013
    Posts
    3

    Re: Detecting Whether Value is in Slicer

    Thanks very much, Olly! I think I see the logic in what you're suggesting. I've tried doing this myself by copying/pasting your code but it is giving me an error saying application-defined or object-defined error. This is on the 'For Each si In ThisWorkbook.SlicerCaches(sSlicer).SlicerItems' line. Do you know why? Could it be that 'si' isn't defined as anything (when I hover over it in debug mode, it says si = nothing)?

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Detecting Whether Value is in Slicer

    Post your workbook, let's see what's going on

  6. #6
    Registered User
    Join Date
    09-09-2016
    Location
    UK
    MS-Off Ver
    2013
    Posts
    3

    Re: Detecting Whether Value is in Slicer

    I've fiddled around with my workbook and found that I was defining various variables in a weird way which probably didn't help. After fixing this, I think that your code should work. For my needs, I have used an 'On Error GoTo' command to bypass all cities which don't have data (as these are the only source of error in my macro) and just continue onto the next city.

    Thank you very much for your help, Olly!

+ 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. Can I Sync a Slicer to Another Slicer If....
    By Polak in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2015, 12:35 AM
  2. Make one slicer mirror filters of another slicer
    By IbenezPrez in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2014, 02:00 PM
  3. Slicer Macro - unfilter another slicer on selection
    By chadheins in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-24-2012, 10:41 AM
  4. [SOLVED] Detecting a value in a range
    By michaelt721 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-26-2012, 03:40 AM
  5. [SOLVED] Change Chart Title Depending on a Slicer - slicer advice
    By JungleJme in forum Excel General
    Replies: 8
    Last Post: 08-17-2012, 07:59 AM
  6. Detecting dates...
    By KennytheFish in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 01-07-2008, 04:58 PM
  7. detecting a tab delimiter
    By PLPE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2005, 01:29 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