How to list selected slicer values in cell

goncalogera

New Member
Joined
Nov 10, 2020
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
I've a slicer called "Selecione um ano" that filters the data in a table called "Vendas2020". What I want to do now is to extract the selection(s) made on the filter (which can go from 2015 to current year) and print them as a list in any given cell but I can't seem to make the code work. This list has to update each time the selection(s) on the list are changed.

I've adapted a code I've found in many answers on similar questions but my knowledge isn't much so there has to be something missing in my adaptation.

Here is my code:

VBA Code:
Dim sl2 As Slicer
Dim sc2 As SlicerCache
Set sc2 = ThisWorkbook.SlicerCaches.Add2(ws.ListObjects("Vendas2020"), "Ano")
Set sl2 = sc2.Slicers.Add(wr, , "Ano", "Selecione um ano")


Dim cache As Excel.SlicerCache
Set cache = ActiveWorkbook.SlicerCaches("Selecione um ano")
Dim sItem As Excel.SlicerItem
For Each sItem In cache.SlicerItems
If sItem.Selected = True Then MsgBox sItem.Name
Next sItem

Thanks for your help!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top