问题在于,每当我运行宏时,切片器上都不会真正发生任何事情-我的意思是未选择任何项目。我需要按月份筛选-自动,具体取决于当前日期。代码有什么问题? 我需要强调,我的数据透视表是在数据模型上创建的:
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
答案 0 :(得分:0)
好吧,我深入研究了这个主题,现在知道.SlicerCacheLevel.SlicerItems让我进入了切片器列表中的项目。现在的问题是我无法在切片器列表中选择特定的月份。假设列表中有5个元素(3月,4月,5月,6月,7月),这意味着数组由5个元素(1至5)组成。因此,如果我们现在有7月(7),则无论如何我都无法在切片器上选择7。循环无法正常进行。如何解决?看我的代码:
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