简体   繁体   English

宏来更改数据透视表筛选器

[英]Macro To Change Pivot Table Filters

My goal is to change the filter of 3 pivot tables, all with the same field with the click of a button attached to a macro. 我的目标是通过单击附加到宏的按钮来更改3个数据透视表的过滤器,所有这些数据透视表都具有相同的字段。

Here is my code: 这是我的代码:

    Dim pickedDate As String
    Dim shift As String

     pickedDate = Worksheets("Report").Range("C1").Value
    shift = Worksheets("Report").Range("C2").Value

    Worksheets("Report").PivotTables("PivotTable2").PivotCache.SourceData = Worksheets("Fullness").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
    Worksheets("Report").PivotTables("PivotTable3").PivotCache.SourceData = Worksheets("Backed").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
    Worksheets("Report").PivotTables("PivotTable4").PivotCache.SourceData = Worksheets("Hoist").Range("A1").CurrentRegion.Address(, , xlR1C1, True)


    For Each PT In ActiveSheet.PivotTables
        PT.PivotFields("Date").CurrentPage = CDate(pickedDate)
        PT.PivotFields("Shift").CurrentPage = shift
    Next

However when I get to my loop for the pivot tables, I get the error on PT.PivotFields("Date").CurrentPage = CDate(pickedDate) : 但是,当我转到数据透视表的循环时,在PT.PivotFields("Date").CurrentPage = CDate(pickedDate)上收到错误:

Run time error 1004

Application Defined or Object Defined Error

I've verfied the source data for the tables is updated correctly. 我已验证表格的源数据已正确更新。

I've tried not using the loop, and doing it manually like 我试过不使用循环,而是手动执行

Worksheets("Report").PivotTables("PivotTable4").PivotFields("Date").CurrentPage = CDate(pickedDate)

for each table, and it only works for one of the tables, my PivotTable3 . 对于每个表,它仅适用于其中一个表,即我的PivotTable3

Even more curiously, when I comment out my code to change the date, and use either way (the loop or the manual change for the 3 tables) for the shift change it works perfectly with no error. 更奇怪的是,当我注释掉我的代码以更改日期,并使用任一种方式(3个表的循环或手动更改)进行班次更改时,它都可以完美地工作而不会出错。

I've verfied the name of my pivot tables, that's not the issue. 我已经核实了数据透视表的名称,这不是问题。 The name of the field I'm trying to change appears correct as well- it works for one of the tables and I've copied and pasted that cell "Date" into the others even. 我要更改的字段名称似乎也正确-它适用于其中一个表,并且我已经将该单元格“ Date”复制并粘贴到了其他表中。

What am I missing? 我想念什么?

I'm not completely sure why this works now, but all I did to change my code was add into the loop a clear filter command for the "Date" field. 我不完全确定为什么现在可以正常工作,但是我所做的所有更改代码的操作都是将“日期”字段的清除过滤器命令添加到循环中。

So now my code has this loop instead for making the date and shift change for all the pivot tables based on a cell value I enter manually, just once 所以现在我的代码有了这个循环,而不是根据我手动输入的单元格值对所有数据透视表进行日期和班次更改

 For Each PT In ActiveSheet.PivotTables
        PT.PivotFields("Date").ClearAllFilters
        PT.PivotFields("Date").CurrentPage = CDate(pickedDate)
        PT.PivotFields("Shift").CurrentPage = shift
    Next

Again, I'm not sure why I didn't have to do this for the shift filter, but now it works. 同样,我不确定为什么不必为移位过滤器执行此操作,但是现在它可以工作了。 I suggest trying that first if anyone encounters this issue 我建议如果有人遇到此问题,请先尝试

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

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