[英]Can't select/deselect an item on a Slicer - VBA
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:
我需要强调我的 pivot 表是在数据 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.好的,我深入探讨了主题,现在知道了。SlicerCacheLevel.SlicerItems 让我进入切片器列表上的项目。 Now the problem is that I can't select specific month on the slicer list.
现在的问题是我不能在切片器列表中显示 select 特定月份。 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).
假设列表中有 5 个元素(三月、四月、五月、六月、七月),这意味着该数组由 5 个元素(1 到 5)组成。 So, if we have July now (7) I can't in any way select 7 on a slicer.
所以,如果我们现在有 7 月 (7),我不能以任何方式 select 7 在切片器上。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.