Excel 如何筛选其他工作表上的其他透视表?
我现在正在编写一个报告,其中包含一些从不同表创建的透视表 这是我的表1: 这是我的表2: 我想链接这两个过滤器。如果主枢轴将过滤器更改为“美国”。整个工作簿中的其他数据透视也必须在“filedbase”字段中更改为“USA”值 我目前正在使用此代码: 表1: 过滤器模块:Excel 如何筛选其他工作表上的其他透视表?,excel,vba,Excel,Vba,我现在正在编写一个报告,其中包含一些从不同表创建的透视表 这是我的表1: 这是我的表2: 我想链接这两个过滤器。如果主枢轴将过滤器更改为“美国”。整个工作簿中的其他数据透视也必须在“filedbase”字段中更改为“USA”值 我目前正在使用此代码: 表1: 过滤器模块: Sub FilterPivot(ByVal Target As PivotTable) Dim pt As PivotTable Dim pf As PivotField Dim sCurrentPage As Stri
Sub FilterPivot(ByVal Target As PivotTable)
Dim pt As PivotTable
Dim pf As PivotField
Dim sCurrentPage As String
Dim vItem As Variant
Dim vArray As Variant
'########################
'# Change these to suit #
'########################
Const sField As String = "filedbase" 'FIELD NAME IN ALL PIVOT TABELS IN THE WORKBOOK
vArray = Array("other") 'THE OTHER PIVOT THAT IS ON ANOTHER SHEET
If Target = "main" Then 'THE MAIN PIVOT TABLE
On Error GoTo errhandler
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
'Find out what item they just selected
Set pf = Target.PivotFields(sField)
With pf
If .EnableMultiplePageItems Then
.ClearAllFilters
.EnableMultiplePageItems = False
sCurrentPage = "(All)"
Else:
sCurrentPage = .CurrentPage
End If
End With
MsgBox sCurrentPage
'Change the other slave pivots to match. Slicers will pass on those settings
For Each vItem In vArray
Set pt = ActiveSheet.PivotTables(vItem)
With pt
Set pf = .PivotFields(sField)
If pf.CurrentPage <> sCurrentPage Then
pf.ClearAllFilters
pf.CurrentPage = sCurrentPage
End If
End With
Next
errhandler:
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
End If
End Sub
Sub-FilterPivot(ByVal目标作为数据透视表)
数据透视表
Dim pf作为数据透视字段
将当前页面变暗为字符串
作为变体的暗维它
Dim vArray作为变体
'########################
“#把这些换成合适的#
'########################
工作簿中所有透视选项卡中的Const sField As String=“filedbase”字段名
vArray=Array(“其他”)'另一张图纸上的另一个轴
如果Target=“main”,则“主数据透视表”
关于错误转到错误处理程序
应用
.ScreenUpdate=False
.Calculation=xlCalculationManual
.EnableEvents=False
以
'找出他们刚刚选择的项目
设置pf=目标数据透视字段(sField)
与pf
如果.EnableMultiplePageItems,则
.ClearAllFilters
.EnableMultiplePageItems=False
sCurrentPage=“(全部)”
其他:
sCurrentPage=.CurrentPage
如果结束
以
MsgBox sCurrentPage
'将其他从属轴更改为匹配。切片器将传递这些设置
对于vArray中的每个vItem
Set pt=ActiveSheet.PivotTables(vItem)
与pt
设置pf=.PivotFields(sField)
如果pf.CurrentPage sCurrentPage,则
pf.ClearAllFilters
pf.CurrentPage=sCurrentPage
如果结束
以
下一个
错误处理程序:
应用
.ScreenUpdate=True
.Calculation=xlcalculation自动
.EnableEvents=True
以
如果结束
端接头
代码工作起来很有魅力,但是,只有在这一行中我保持:Set pt=ActiveSheet.PivotTables(vItem)
。
如果我更改:ActiveSheet
代码将不再工作
我有点迷路了。谢谢你的帮助
谢谢大家
Sub FilterPivot(ByVal Target As PivotTable)
Dim pt As PivotTable
Dim pf As PivotField
Dim sCurrentPage As String
Dim vItem As Variant
Dim vArray As Variant
'########################
'# Change these to suit #
'########################
Const sField As String = "filedbase" 'FIELD NAME IN ALL PIVOT TABELS IN THE WORKBOOK
vArray = Array("other") 'THE OTHER PIVOT THAT IS ON ANOTHER SHEET
If Target = "main" Then 'THE MAIN PIVOT TABLE
On Error GoTo errhandler
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
'Find out what item they just selected
Set pf = Target.PivotFields(sField)
With pf
If .EnableMultiplePageItems Then
.ClearAllFilters
.EnableMultiplePageItems = False
sCurrentPage = "(All)"
Else:
sCurrentPage = .CurrentPage
End If
End With
MsgBox sCurrentPage
'Change the other slave pivots to match. Slicers will pass on those settings
For Each vItem In vArray
Set pt = ActiveSheet.PivotTables(vItem)
With pt
Set pf = .PivotFields(sField)
If pf.CurrentPage <> sCurrentPage Then
pf.ClearAllFilters
pf.CurrentPage = sCurrentPage
End If
End With
Next
errhandler:
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
End If
End Sub