Possible to list selected slicer values in cell?

largeselection

Active Member
Joined
Aug 4, 2008
Messages
353
Hi,

So I found some info online which outlines a routine to display messageboxes which show the items contained in a slicer.

What I want to know is:
1) Is it possible to only list the items that are selected in a slicer?

2) If part 1 is possible, is it then possible to list them in a string to put in a cell?


Basically if I have say 3 slicers over a report "NAME", "YEAR", "STATE", I want the report to display a text row at the top (or even a header/footer might work) which says what has been selected.

I know I can just print out the report with the slicers visible, but I actually have many slicers and might be selecting items that are not visible within the same slicer box window.

So rather than having to type what I selected manually, I'd want it to just show in a line above the report:

Name: Henry, Carl, Joe / Year: 2011 / State: NY, NJ, NM, CA

and have it update based on whatever is selected in the slicers.

Any thoughts on how to accomplish this?

I've listed the code from Microsoft's website below which shows how to list all of the items (all items, not only selected items) in messageboxes for reference.

Rich (BB code):
dim cache As Excel.slicerCache
Set cache = ActiveWorkbook.SlicerCaches("Slicer_Sales_Type")
Dim sItem As Excel.SlicerItem
For Each sItem In cache.SlicerItems
MsgBox sItem.Name
Next sItem

</PRE>




</PRE>

THANKS!


</PRE>
 

MikeLiberty

Board Regular
Joined
Aug 13, 2010
Messages
55
You probably figured this out or came to a workaround, but I thought I'd post this for people like me who are searching for similar answers.

You can just add an if statement to check if sitem.selected = True

Code:
Sub SelectedSlicers()

Dim cache As Excel.SlicerCache
Set cache = ActiveWorkbook.SlicerCaches("Slicer_Sales_Type)
Dim sItem As Excel.SlicerItem
For Each sItem In cache.SlicerItems
If sItem.Selected = True Then MsgBox sItem.Name
Next sItem
End Sub
Then instead of a message box, just have it write somewhere, either a variable to concatenate later or a cell.
 

largeselection

Active Member
Joined
Aug 4, 2008
Messages
353
Hey I have used a workaround, but this is much better so thanks for responding after so long!

I've gotten it to put the values in a list of cells in the worksheet, but is there a way to do that only if the selected items count to less than the total that are available?

Basically, I only want the items to populate in the worksheet when the code is run if there are less items selected than are available, because I don't want to fill the sheet with values because nothing is selected (so by default technically everything is "selected")
 

largeselection

Active Member
Joined
Aug 4, 2008
Messages
353
Spoke too soon...

So it worked on a test file, but it failed when I tried to put the same code on the actual file. The test file consisted of dummy data that I input, the actual file is connected to an OLAP cube.

The code I used is below:
Code:
Sub RevisedSlicers()
Dim sAvail As Long
Dim sSelect As Long
Dim cache As Excel.SlicerCache
Set cache = ActiveWorkbook.SlicerCaches("Slicer_TYPE")
Dim sItem As Excel.SlicerItem
sAvail = 0
sSelect = 0
For Each sItem In cache.SlicerItems
sAvail = sAvail + 1
If sItem.Selected = True Then Range("B5000").End(xlUp).Offset(1, 0).Value = sItem.Value
sSelect = sSelect + 1
Next sItem
If Range("B20").Value < sAvail Then
Exit Sub
End If
Range("B22:B5000").ClearContents
End Sub
That worked fine in the test workbook, but not in the actual one - I'm guessing it has something to do with the fact that one is an OLAP pivot table and one is not. Any ideas?

I'm getting an "Application defined or object defined error" at the row where it says "For Each sItem In cache.SlicerItems"
 

MikeLiberty

Board Regular
Joined
Aug 13, 2010
Messages
55
I'm pretty new to slicers but I did find this to get a count.

SlicerCache.SlicerItems.count

However, according to the help documents, that won't work with OLAP and you need to use SlicerCacheLevel. I think the help might be able to put you on the right track. Sorry I can't be of more use.
 

largeselection

Active Member
Joined
Aug 4, 2008
Messages
353
That is a bummer. I read the help info and tried some stuff to no avail. I guess I'll keep tinkering with it when I can and keep the workaround I've been using for now.

Thanks for your help again!
 

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,097
What I want to know is:
2) If part 1 is possible, is it then possible to list them in a string to put in a cell?
Hi
I know this is an old post but for others this may help.

I use slicers but to do what you want I use a combination of pivot tables and a simple piece of VBA that I stole from Chandoo (How to add a range of cells in excel – concat() | Chandoo.org - Learn Microsoft Excel Online)

I have an example using the slicers and names from this thread here
https://app.box.com/s/buxal4ebcewl7i0jz421qjl8xp4tom0o


HTH

Martin
 

Forum statistics

Threads
1,081,615
Messages
5,360,037
Members
400,565
Latest member
Tommy O

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top