使用VBA更改数据透视表过滤器 [英] Change the pivot Table filter using VBA

查看:122
本文介绍了使用VBA更改数据透视表过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含CoB Date字段的数据透视表,如图所示。

我正在尝试创建一个宏,根据用户输入自动更改日期。

我写了下面的宏代码。但是它显示错误:

I have a pivot table which contains the "CoB Date" field as shown.
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:

无法获取数据透视表类的PivotField属性

任何人可以帮助我吗?

注意:假设日期格式不是问题

Can any one help me with this?
Note: Assume that Date Format is not an issue

代码:

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

假设你有这样的数据:

运行宏时,会提示输入日期:

When you run the macro, it will prompt for a date:

然后按OK,结果将是:

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.

这篇关于使用VBA更改数据透视表过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆