【发布时间】:2018-01-03 01:36:26
【问题描述】:
第一次发帖。
我有一个 Excel 2013 电子表格,上面有三个数据透视图。我试图通过引用同一张表上的单元格来自动更新它们。 VBA 代码为:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell B1 is touched
If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub
'Set the Variables to be used and assigns their values
Dim WS1 As Worksheet: Set WS1 = ActiveWorkbook.Worksheets("PIVOT")
Dim PT1 As PivotTable: Set PT1 = WS1.PivotTables("pgmTable1")
Dim PT2 As PivotTable: Set PT2 = WS1.PivotTables("pgmTable2")
Dim PT3 As PivotTable: Set PT3 = WS1.PivotTables("pgmTable3")
Dim PF1 As PivotField: Set PF1 = PT1.PivotFields("REPORTING_DATE")
Dim PF2 As PivotField: Set PF2 = PT2.PivotFields("REPORTING_DATE")
Dim PF3 As PivotField: Set PF3 = PT3.PivotFields("REPORTING_DATE")
Dim filterCell As Date
'This reassigns the filter to the sheet-defined value
filterCell = WS1.Range("B1").Value
'This updates and refreshes the PIVOT tables
With PT1
PF1.ClearAllFilters
PF1.CurrentPage = filterCell
PT1.RefreshTable
End With
With PT2
PF2.ClearAllFilters
PF2.CurrentPage = filterCell
PT2.RefreshTable
End With
With PT3
PF3.ClearAllFilters
PF3.CurrentPage = filterCell
PT3.RefreshTable
End With
End Sub
它回来了
运行时错误“1004”:应用程序定义或对象定义错误
此代码过去曾适用于包含单个数据透视表的工作表。我是一个相对的菜鸟,把这件事混在一起了。我确信有一种更优雅的方法可以使用 for 循环进行此更新。
另外,我尝试将这些数据透视表拆分到它们自己的工作表中,但没有成功。
任何帮助将不胜感激!
【问题讨论】: