简体   繁体   English

使用VBA更改数据透视表过滤器

[英]Change the pivot Table filter using VBA

I have a pivot table which contains the "CoB Date" field as shown. 我有一个数据透视表,其中包含“CoB Date”字段,如图所示。
I am trying to create a macro which automatically changes the date as per the user input. 我正在尝试创建一个宏,根据用户输入自动更改日期。
I've written the following macro code. 我写了以下宏代码。 But it shows the error: 但它显示错误:

Unable to get PivotFields property of the PivotTable class 无法获取数据透视表类的PivotFields属性

Can any one help me with this? 谁能帮我这个?
Note: Assume that Date Format is not an issue 注意:假设日期格式不是问题

Code: 码:

Sub My_macro()
    Dim num as String
    num = InputBox(Prompt:="Date", Title:="ENTER DATE")
    Sheets("Sheet1").PivotTables("PivotTable1") _
        .PivotFields("CoB Date").CurrentPage = num
End Sub

在此输入图像描述

As commented the exact same code works on my end. 正如评论所说,完全相同的代码在我的最终。

Sub My_macro()
    Dim num As String
    num = InputBox(Prompt:="Date", Title:="ENTER DATE")
    Sheets("Sheet1").PivotTables("PivotTable1") _
        .PivotFields("CoB Date").CurrentPage = num
End Sub

Suppose you have a data like this: 假设您有这样的数据:

在此输入图像描述

When you run the macro, it will prompt for a date: 运行宏时,它将提示输入日期:

在此输入图像描述

And then after pressing ok, the result would be: 然后按下确定后,结果将是:

在此输入图像描述

Take note that we assumed that entering of date is not an issue. 请注意,我们假设输入日期不是问题。
So we used a simple data which will eliminate that and so your code works. 所以我们使用了一个简单的数据来消除它,所以你的代码可以工作。
The probable issue you're dealing with is if the dates have Time Stamp . 您正在处理的可能问题是日期是否有时间戳
And based on your screen shot, that is the case. 根据您的屏幕截图,就是这样。

I had the same problem with "Unable to get PivotFields property of the PivotTable class". 我遇到了“无法获取数据透视表类的PivotFields属性”的问题。

I figured out that while my pivot table was the only one on that sheet (or in the workbook for that matter) it was not "PivotTable1". 我发现虽然我的数据透视表是该工作表上的唯一一个(或者在工作簿中),但它不是“PivotTable1”。 It was "PivotTable4" most likely because I had created and deleted 3 others beforehand. 最有可能是“PivotTable4”,因为我之前创建并删除了其他3个。

To find out the name of your pivot table, and change it if you want, just right click anywhere in your pivot table and then select "Pivot Table Options". 要查找数据透视表的名称,并根据需要进行更改,只需右键单击数据透视表中的任意位置,然后选择“数据透视表选项”。 You will see the "PivotTable Name" right at the top and can rename it from there. 您将在顶部看到“数据透视表名称”,并可以从那里重命名。

Additionally, I was having issues with this same error when trying to change one of the filters. 此外,在尝试更改其中一个过滤器时,我遇到了同样的错误问题。 When I referenced the field using: 当我使用以下参考字段时:

Sheets("mySheetName").PivotTables("PivotTable4").PivotFields("myFieldName")

it would throw the same error as above. 它会抛出与上面相同的错误。 It turned out I had 3 spaces at the end of my field name. 事实证明我的字段名称末尾有3个空格。 The way I found this out may was to loop through all my filter fields displaying a MsgBox for each until I found it. 我发现这个的方法可能是循环遍历显示每个MsgBox的所有过滤器字段,直到找到它为止。 The following code is how I did that and hopefully may help someone with a similar issue: 以下代码是我如何做到这一点,希望可以帮助有类似问题的人:

Dim pt As PivotTable
Dim pf As PivotField

Set pt = Sheets("mySheetName").PivotTables("PivotTable4")
For Each pf In pt.PivotFields
    MsgBox ("FieldName: [" & pf.Name & "] with a length of: " & Len(pf.Name) & " and a trimmed length of: " & Len(Trim(pf.Name)))
Next pf

Hope this helps someone! 希望这有助于某人!

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

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