1
votes

I´m a newbie with VBA and need to learn how to change automatically the selected values on a Slicer. I first tried with a very simple one, but I tried every variation possible to the following code and always get an error 1004, this time "application-defined or object-defined error"

Sub SlicerSelect()
    With ActiveWorkbook.SlicerCaches("Slicer_Time")
        .SlicerItems("2016").Selected = False
    End With
End Sub

Does someone have an idea ? Here is also an image of my slicer and its settings.

By the way, it works when I'm using the .ClearManualFilter command.

Thanks a lot !

Here is also a macro record by filtering manually my items :

Sub Macro2()
' Macro2 Macro
ActiveWorkbook.SlicerCaches("Slicer_Time2").VisibleSlicerItemsList = Array( _
    "[Booking Period].[Time].[YEAR].&[2018]")
End Sub
2
Add your code for review please. Also, what what option are you trying to select? All slicer options? Most recent year? etc.urdearboy
(sorry for the images... this is the first time I ask a code question here ! )Charlotte Wiatrowski
'Sub SlicerSelect() With ActiveWorkbook.SlicerCaches("Slicer_Time") .SlicerItems("2016").Selected = False End With End Sub' for my code. I am trying to select the option "2016", I thought you could see all of this on the first image sorryCharlotte Wiatrowski
I'm still investigating and I have noticed that in my Locals, as soon as I assign the value of my SlicerCache to a variable, this one appears with the value <application-defined or object-defined error> by its SlicerItems, so I guess the problem comes from my Slicer or maybe the source file... But manually I have done many pivots with it and changed the values without any problem, so I don't really understand !Charlotte Wiatrowski
Can you fire up the Macro Recorder, filter the item manually, and then add the resulting code to your original question please. This often highlights the issue.jeffreyweir

2 Answers

2
votes

Your problem was that there are two different types of PivotTables:

  • PivotTables based on ranges, that use the kind of code you initially posted, and that let you pass in individual PivotItems one at a time; and
  • PivotTables based on the Data Model (i.e. PowerPivot) or OLAP cubes, that use a completely different syntax where you have to pass in an array showing ALL of the items you want visible, using a much more confusing syntax.
0
votes

Filtering SlicerItems can be tricky, because at least one item must remain visible at all times. This code shows how to filter a Slicer on an array called vSelection, and will show you how you need to accomplish this.

Option Explicit

Sub FilterSlicer()
Dim slr As Slicer
Dim sc As SlicerCache
Dim si As SlicerItem
Dim i As Long
Dim vItem As Variant
Dim vSelection As Variant

Set sc = ActiveWorkbook.SlicerCaches("Slicer_ID")
'Set sc = slr.SlicerCache

vSelection = Array("B", "C", "E")

For Each pt In sc.PivotTables
    pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed
Next pt

With sc

    'At least one item must remain visible in the Slicer at all times, so make the first
    'item visible, and at the end of the routine, check if it actually  *should* be visible
    .SlicerItems(1).Selected = True

    'Hide any other items that aren't already hidden.
    'Note that it is far quicker to check the status than to change it.
    ' So only hide each item if it isn't already hidden
    For i = 2 To .SlicerItems.Count
        If .SlicerItems(i).Selected Then .SlicerItems(i).Selected = False
    Next i

    'Make the PivotItems of interest visible
    On Error Resume Next 'In case one of the items isn't found
    For Each vItem In vSelection
        .SlicerItems(vItem).Selected = True
    Next vItem
    On Error GoTo 0

    'Hide the first PivotItem, unless it is one of the countries of interest
    On Error Resume Next
    If InStr(UCase(Join(vSelection, "|")), UCase(.SlicerItems(1).Name)) = 0 Then .SlicerItems(1).Selected = False
    If Err.Number <> 0 Then
        .ClearAllFilters
        MsgBox Title:="No Items Found", Prompt:="None of the desired items was found in the Slicer, so I have cleared the filter"
    End If
    On Error GoTo 0
End With


For Each pt In sc.PivotTables
    pt.ManualUpdate = False
Next pt

End Sub