简体   繁体   English

Excel 2013 VBA从单个单元格过滤多个数据透视表不起作用

[英]Excel 2013 VBA to filter multiple pivot tables from single cell not working

First time posting. 第一次发布。

I have an Excel 2013 spreadsheet with three pivot tales on it. 我有一个Excel 2013电子表格,上面有三个主要故事。 I'm trying to automatically update them by referencing a cell on the same sheet. 我正在尝试通过引用同一张纸上的单元格来自动更新它们。 The VBA code is: 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

Here's the spreadsheet: 这是电子表格:

It's returning 回来了

Run-time error '1004': Application-defined or object-defined error 运行时错误“ 1004”:应用程序定义或对象定义的错误

PF1.CurrentPage = filterCell PF1.CurrentPage = filterCell

This code has worked for worksheets containing single pivot tables in the past. 过去,此代码适用于包含单个数据透视表的工作表。 I'm a relative noob that has kludged this together. 我是一个相对的菜鸟,已经把这个混在一起了。 I'm certain that there is a more elegant way to do this update with a for-loop. 我敢肯定,有一个更优雅的方法可以使用for循环进行此更新。

Also, I have tried splitting these pivot tables onto their own worksheets without success. 此外,我尝试将这些数据透视表拆分到自己的工作表中,但没有成功。

Any help would be appreciated! 任何帮助,将不胜感激!

mmm, Why aren't you using "Slicer" in Excel? 嗯,为什么不在Excel中使用“切片器”? You can connect multiple pivot tables easily instead of using VBA. 您可以轻松连接多个数据透视表,而不必使用VBA。

  1. Click in any cell in the pivot table. 单击数据透视表中的任何单元格。
  2. Go to "Analyze" 转到“分析”
  3. Look for "Insert Slicer" 寻找“插入切片器”
  4. Chose the field that you want use as a filter 选择要用作过滤器的字段
  5. Right click in the Slicer 右键单击切片器
  6. Look for "Report Connections..." and connect all the pivot tables what you want to filter with that slicer. 查找“报告连接...”,然后使用该切片器连接所有要过滤的数据透视表。

Note: All pivot tables need the same source. 注意:所有数据透视表都需要相同的源。

I made this Tutorial . 我制作了本教程 It is in Spanish but I am using the English Excel version. 它是西班牙语,但我使用的是英语Excel版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM