the problem is that whenever I run the macro nothing really happens on the slicer - I mean no item is selected. I need to filter by month - automatically, depending on the current date. What's wrong with the code? I need to underscore that my pivot tables are created on data model:
Sub Archive()
Dim i As SlicerItem
Dim ostWierszWS As Long
Dim zakres As Range
Dim zakres_ws As Range
Dim WS As Worksheet
Application.ScreenUpdating = False
For Each i In ActiveWorkbook.SlicerCaches("Slicer_Month").SlicerItems
If i.Value = Month(Now) Then
i.Selected = True
Else
i.Selected = False
End If
Next i
On Error Resume Next
Set WS = Worksheets("Archive")
Set zakres = Worksheets("aaa").Range("D7:D10")
ostWierszWS = WS.Cells(Rows.Count, 3).End(xlUp).Row + 1
Set zakres_ws = WS.Cells(ostWierszWS, 3)
zakres.Copy
zakres_ws.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlPasteSpecialOperationNone, skipblanks:=False, Transpose:=True
Set WS = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Sheets("aaa").Activate
End Sub
Ok, I digged into the topic and now know that.SlicerCacheLevel.SlicerItems let me get into item on slicer list. Now the problem is that I can't select specific month on the slicer list. Let's say we have 5 elements on a list (March, April, May, June, July) which means that array consists of 5 elements (1 to 5). So, if we have July now (7) I can't in any way select 7 on a slicer. Looping through doesn't work out as well. How can it be solved? Look at my code:
Sub Archive()
Dim i As SlicerItem
Dim varrSlicerItems As Variant
Dim k As Long
With ActiveWorkbook.SlicerCaches("Slicer_Month")
ReDim varrSlicerItems(1 To .Slicers("Month").SlicerCacheLevel.SlicerItems.Count)
For Each i In .Slicers(1).SlicerCacheLevel.SlicerItems
k = k + 1
varrSlicerItems(k) = i.Name
Next i
'and ERROR occurs here...
.VisibleSlicerItemsList = Array(varrSlicerItems(Month(Date)))
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.