Datenschnitt eines PivotCharts
22.02.2021 15:13:16
Alonso
ich habe folgenden Code aus dem Internet kopiert, welcher auch super funktioniert.
Jetzt möchte ich aber statt vSelection = Array("19.02.2021") das Tagesdatum über Date verwenden.
Leider bekomme ich das irgendwie mit den Gänsefüßchen nicht hin.
Ziel ist den Datenschnitt eines PivotCharts immer mit dem Tagesdatum zu filtern und alle anderen Termine nicht im Chart zu berücksichtigen.
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
Dim DatumHeute As Variant
Set sc = ActiveWorkbook.SlicerCaches("Datenschnitt_Datum")
vSelection = Array("19.02.2021")
' So klappt es nicht---------------
'DatumHeute = Date
'DatumHeute = Chr(34) & DatumHeute & Chr(34)
'vSelection = Array(DatumHeute)
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