数据透视表CurrentPage属性 – 获取“应用程序定义或对象定义的错误”

我是VBA全新的(这是第二天!)。 我试图设置数据透视表筛选器以匹配combobox用户select。 我发现了一些代码来设置filter的单元格的内容时,该单元格被选中,我已经修改它把它放在一个button的子,以便代码不会被激活,直到button被点击。

Sub Generate_Report() 'PLAN - Changes Plan filter value for PivotTable to value in Plan combo box, which is 'linked to cell D9 'Set the Variables to be used Dim pt As PivotTable Dim PlanFilter As PivotField Dim NewPlan As String Set pt = Worksheets("Pivot Table").PivotTables("PivotTable_Parameters") Set PlanFilter = pt.PivotFields("Plan") NewPlan = Worksheets("Report Options").Range("D9").Value 'Update and refresh the PIVOT table With pt PlanFilter.ClearAllFilters pt.PivotFields("Plan").CurrentPage = NewPlan pt.RefreshTable End With End Sub 

(我希望能find我得到代码的页面 – 感谢在线VBA社区,帮助我开始!)

我更改了原始代码中的一些variables名称以更好地匹配上下文。 PlanFilter之前被称为Field 。 以前读取的.CurrentPage行: Field.CurrentPage=NewCat

我遇到了.CurrentPage行的问题。 在我更改variables名之前它工作正常,但是一旦我做了,我就得到了“应用程序定义的或对象定义的”错误。 我将字段引用更改为您在此处看到的较长的引用,它最初起作用,然后它没有。

在获取“无法设置PivotField类的CurrentPage属性”错误时使用相同的代码的类似问题,但我无法留下评论。 我试过斯科特提出的改变:

 With PlanFilter .ClearAllFilters .CurrentPage = NewPlan End With 

…但我仍然得到错误。

有什么想法吗?

谢谢!

凯特

我会改变:

 With pt PlanFilter.ClearAllFilters pt.PivotFields("Plan").CurrentPage = NewPlan pt.RefreshTable End With 

 With pt .PivotFields("Plan").ClearAllFilters .PivotFields("Plan").CurrentPage = NewPlan .RefreshTable End With