Select multiple items in Slicer

rahulsahu910

New Member
Joined
Apr 8, 2019
Messages
7
Hi,

I am trying to select multiple items in a given slicer, i recorded a macro to check how it happens and the result is below

Sub Macro1()
'
' Macro1 Macro
'


ActiveWorkbook.SlicerCaches("Slicer_costcentername1").VisibleSlicerItemsList = _
Array( _
"[costcentername].&[abc]", _
"[costcentername].&[def]", _
"[costcentername].&[ghi]", _
"[costcentername].&[jkl]")


End Sub

Tried to replicate the same thing as below

Sub Macro1()
'
' Macro1 Macro
'


Dim st As String
st = """_[costcentername].&[abc]""" + ",_" + """[costcentername].&[def]""" + ",_" + """[costcentername].&[ghi]" + ",_" + "[costcentername].&[jkl]"""
ActiveWorkbook.SlicerCaches("Slicer_costcentername1").VisibleSlicerItemsList = Array(st)

End Sub


But the above code selects all the options present in the slicer. Kindly help to resolve the issue.:(:(
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
This should work:
Code:
Sub Macro1()
    Dim VisibleItems() As String
    ReDim VisibleItems(1 To 4)
    VisibleItems(1) = "[costcentername].&[abc]"
    VisibleItems(2) = "[costcentername].&[def]"
    VisibleItems(3) = "[costcentername].&[ghi]"
    VisibleItems(4) = "[costcentername].&[jkl]"
    ActiveWorkbook.SlicerCaches("Slicer_costcentername1").VisibleSlicerItemsList = VisibleItems
End Sub
 
Upvote 0
This should work:
Code:
Sub Macro1()
    Dim VisibleItems() As String
    ReDim VisibleItems(1 To 4)
    VisibleItems(1) = "[costcentername].&[abc]"
    VisibleItems(2) = "[costcentername].&[def]"
    VisibleItems(3) = "[costcentername].&[ghi]"
    VisibleItems(4) = "[costcentername].&[jkl]"
    ActiveWorkbook.SlicerCaches("Slicer_costcentername1").VisibleSlicerItemsList = VisibleItems
End Sub

Thanks for the reply. But the visible items strings has be dynamic, how to achieve that
 
Upvote 0
Also i am getting error using the above method.

Error 1004: The query did not run or the data model could not be accessed. "The restriction value provided by the consumer either does not match other restrictions or refers to an unknown object.
 
Upvote 0
Hi,

There is no option of attaching files here. However i can share a image and let you know the requirement.

The graph should change as per the multiple selections done in the slicer.

url


Thanks
 
Upvote 0
The sample file you uploaded contains a pivottable NOT tied to the data model whereas the VBA code you posted indicates a slicer tied to a pivot table which uses either the data model or an OLAP cube. The VBA syntax for slicers from these two source types is different. I recorded a macro, which then gives:
Code:
    With ActiveWorkbook.SlicerCaches("Slicer_Name")
        .SlicerItems("aaa").Selected = True
        .SlicerItems("bbb").Selected = True
        .SlicerItems("cccc").Selected = True
        .SlicerItems("ddd").Selected = False
        .SlicerItems("eee").Selected = False
        .SlicerItems("fff").Selected = False
        .SlicerItems("ggg").Selected = False
        .SlicerItems("hhh").Selected = False
        .SlicerItems("iii").Selected = False
    End With
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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