我终于找到了一个代码,可以在数据透视表更新时将切片器与不同的缓存连接起来。基本上,当 slicer1 的值发生变化时,它将更改 slicer2 以匹配 slicer1,从而更新连接到第二个 slicer 的任何数据透视表。
我已经添加.Application.ScreenUpdating
并.Application.EnableEvents
试图加速宏,但它仍然滞后并导致 Excel 变得无响应。
是否有更直接的编码方式,或者这里是否有任何潜在的不稳定行导致 Excel 烧毁它的大脑?
Private Sub Worksheet_PivotTableUpdate _
(ByVal Target As PivotTable)
Dim wb As Workbook
Dim scShort As SlicerCache
Dim scLong As SlicerCache
Dim siShort As SlicerItem
Dim siLong As SlicerItem
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False
Set wb = ThisWorkbook
Set scShort = wb.SlicerCaches("Slicer_Department")
Set scLong = wb.SlicerCaches("Slicer_Department2")
scLong.ClearManualFilter
For Each siLong In scLong.VisibleSlicerItems
Set siLong = scLong.SlicerItems(siLong.Name)
Set siShort = Nothing
On Error Resume Next
Set siShort = scShort.SlicerItems(siLong.Name)
On Error GoTo errHandler
If Not siShort Is Nothing Then
If siShort.Selected = True Then
siLong.Selected = True
ElseIf siShort.Selected = False Then
siLong.Selected = False
End If
Else
siLong.Selected = False
End If
Next siLong
exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
errHandler:
MsgBox "Could not update pivot table"
Resume exitHandler
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
在Contextures上找到的原始代码
一如既往地感谢您的任何建议。