当第一个数据透视表的过滤器字段发生更改时,我正在运行一个宏来更新工作簿中所有数据透视表的过滤器字段(" Facility")(请参阅下面的代码)。宏按预期运行,直到.PivotTables("PivotTable5").PivotFields("Facility").CurrentPage = facility
行,这给出了运行时错误5。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim facility As String
'If the PivotTable1 filter value is changed...
If Not Intersect(Target, Me.PivotTables("PivotTable1").PivotFields("Facility").DataRange) Is Nothing Then
facility = Me.PivotTables("PivotTable1").PivotFields("Facility").CurrentPage
'Change all the other PivotTable filters to the same as PivotTable1
With Me
.PivotTables("PivotTable2").PivotFields("Facility").CurrentPage = facility
.PivotTables("PivotTable3").PivotFields("Facility").CurrentPage = facility
.PivotTables("PivotTable4").PivotFields("Facility").CurrentPage = facility
.PivotTables("PivotTable5").PivotFields("Facility").CurrentPage = facility
.PivotTables("PivotTable6").PivotFields("Facility").CurrentPage = facility
.PivotTables("PivotTable7").PivotFields("Facility").CurrentPage = facility
End With
With ThisWorkbook
.Worksheets("4E - Bili Screen (PivotTable)").PivotTables("PivotTable1").PivotFields("Facility").CurrentPage = facility
.Worksheets("4E - DVT Proph (PivotTable)").PivotTables("PivotTable1").PivotFields("Facility").CurrentPage = facility
.Worksheets("4F - High-Risk Del (PivotTable)").PivotTables("PivotTable1").PivotFields("Facility").CurrentPage = facility
End With
End If
End Sub
答案 0 :(得分:1)
在PivotTable5上手动更新过滤器时录制宏(工作正常)后,我注意到在设置.ClearAllFilters
值之前插入了.CurrentPage
录制的宏(如下所示),这解决了问题。
.PivotTables("PivotTable5").PivotFields("Facility").ClearAllFilters
.PivotTables("PivotTable5").PivotFields("Facility").CurrentPage = facility
但是,我需要在数据透视表5之后为每个数据透视表.ClearAllFilters
,否则错误将在下一个.CurrentPage
行发生。我不知道为什么错误只发生在.PivotTables("PivotTable5").PivotFields("Facility").CurrentPage = facility
行之上或之后,而不是它上面的等效行。