在多个切片器VBA中选择项目

时间:2018-06-22 18:58:45

标签: excel vba slicers

我正在一个excel工作簿中工作,该工作簿包含5个数据透视表,其中包含一个切片器,每个切片器均根据市场区域过滤统计信息。

我正在尝试编写将SLICERS连接在一起的VBA程序。例如,如果我在Slicer1上选择一个区域,则希望在其他4个区域上选择该区域。

If Auro is selected in the first slicer, it should be selected in the others and no other region should be selected

这是我编写的代码:

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim sc1 As SlicerCache
Dim sc2 As SlicerCache
Dim sc3 As SlicerCache
Dim sc4 As SlicerCache
Dim sc5 As SlicerCache
Dim SI1 As SlicerItem

'These names come from Slicer Settings dialog box
Set sc1 = ThisWorkbook.SlicerCaches("Slicer_Market")
Set sc2 = ThisWorkbook.SlicerCaches("Slicer_Market1")
Set sc3 = ThisWorkbook.SlicerCaches("Slicer_Market2")
Set sc4 = ThisWorkbook.SlicerCaches("Slicer_Market3")
Set sc5 = ThisWorkbook.SlicerCaches("Slicer_Market4")

Application.ScreenUpdating = False
Application.EnableEvents = False

sc2.ClearManualFilter
sc3.ClearManualFilter
sc4.ClearManualFilter
sc5.ClearManualFilter

For Each SI1 In sc1.SlicerItems
    sc2.SlicerItems(SI1.Name).Selected = SI1.Selected
    sc3.SlicerItems(SI1.Name).Selected = SI1.Selected
    sc4.SlicerItems(SI1.Name).Selected = SI1.Selected
    sc5.SlicerItems(SI1.Name).Selected = SI1.Selected

Next SI1

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

在FOR EACH LOOP之后的第一行出现运行时错误“ 5”。

0 个答案:

没有答案