如何在 Excel 中使用切片器 link Table 和枢轴 Table?
How to link a Table and a Pivot Table using Slicers in Excel?
嗯。我在 Excel 中的 Sheet 上有一个名为 "ALL_INFO" 的 table,我在其他 sheet 中创建了一个 Pivot table,它的名字是 "PIVOT_INFO"。我想知道 link 一个 table 和一个枢轴 table 使用切片器过滤信息的方法,它反映在两个 table 中。
有人知道我该怎么做吗?
提前谢谢你。
为 PivotTable 创建一个切片器,并为 Table 创建一个切片器。确保 PT 切片器可见,并且 Table 切片器隐藏在用户看不到的地方。然后把这段代码放在Sheet Module对应你的PT所在的worksheet中:
Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim sLastUndoStackItem As String
Dim sc_Pivot As SlicerCache
Dim sc_Table As SlicerCache
Dim si_Pivot As SlicerItem
Dim si_Table As SlicerItem
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
If Target.Name = "PivotTable1" Then '<= Change name as appropriate
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
'The above line doesn't seem to work in my version of O365 so we'll use the English language backup
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
On Error GoTo 0
If sLastUndoStackItem = "Filter" Or sLastUndoStackItem = "Slicer Operation" Then
Set sc_Pivot = ActiveWorkbook.SlicerCaches("Slicer_Data") '<= Change name as appropriate
Set sc_Table = ActiveWorkbook.SlicerCaches("Slicer_Data1") '<= Change name as appropriate
sc_Table.ClearAllFilters
On Error Resume Next 'In case items differ between Table and PT
For Each si_Pivot In sc_Pivot.SlicerItems
With si_Pivot
sc_Table.SlicerItems(.Name).Selected = .Selected
End With
Next si_Pivot
End If
End If
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
这是我使用 "Master" 切片器之前的样子:
...这是我使用 "Master" 切片器后的样子:
请注意,过滤 Table 会隐藏整个工作表中的行。因此,您不想在 Table 旁边放置任何您希望始终保持可见的内容。
嗯。我在 Excel 中的 Sheet 上有一个名为 "ALL_INFO" 的 table,我在其他 sheet 中创建了一个 Pivot table,它的名字是 "PIVOT_INFO"。我想知道 link 一个 table 和一个枢轴 table 使用切片器过滤信息的方法,它反映在两个 table 中。
有人知道我该怎么做吗?
提前谢谢你。
为 PivotTable 创建一个切片器,并为 Table 创建一个切片器。确保 PT 切片器可见,并且 Table 切片器隐藏在用户看不到的地方。然后把这段代码放在Sheet Module对应你的PT所在的worksheet中:
Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim sLastUndoStackItem As String
Dim sc_Pivot As SlicerCache
Dim sc_Table As SlicerCache
Dim si_Pivot As SlicerItem
Dim si_Table As SlicerItem
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
If Target.Name = "PivotTable1" Then '<= Change name as appropriate
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
'The above line doesn't seem to work in my version of O365 so we'll use the English language backup
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
On Error GoTo 0
If sLastUndoStackItem = "Filter" Or sLastUndoStackItem = "Slicer Operation" Then
Set sc_Pivot = ActiveWorkbook.SlicerCaches("Slicer_Data") '<= Change name as appropriate
Set sc_Table = ActiveWorkbook.SlicerCaches("Slicer_Data1") '<= Change name as appropriate
sc_Table.ClearAllFilters
On Error Resume Next 'In case items differ between Table and PT
For Each si_Pivot In sc_Pivot.SlicerItems
With si_Pivot
sc_Table.SlicerItems(.Name).Selected = .Selected
End With
Next si_Pivot
End If
End If
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
这是我使用 "Master" 切片器之前的样子:
...这是我使用 "Master" 切片器后的样子:
请注意,过滤 Table 会隐藏整个工作表中的行。因此,您不想在 Table 旁边放置任何您希望始终保持可见的内容。