如何通过单元格引用更新powerpivot数据透视表过滤器?

时间:2013-10-01 21:27:10

标签: excel vba pivot-table powerpivot

我正在通过不同工作表中的单元格引用更新Powerpivot数据透视表,但是在确定正确的语法时遇到了问题。

如果我对日期进行硬编码(见下文),代码就可以正常工作:

Sheets("Close Rate").Select                   'Select the sheet containing the pivot table to update

ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "[Closed Cases].[Closed Date Week End].[Closed Date Week End]").ClearAllFilters

ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "[Closed Cases].[Closed Date Week End].[Closed Date Week End]"). _
    CurrentPageName = _
    "[Closed Cases].[Closed Date Week End].&[2013-09-28T00:00:00]"

但是,如果我尝试使用变量而不是硬编码日期,我会收到“应用程序定义或对象定义错误”消息。

这是我正在尝试使用的代码:

'Set up variables
Dim FilterDate As String

FilterDate = Sheets("CS Dashboard").Range("I5").Value   'Get date for filter

Sheets("Close Rate").Select                   'Select the sheet containing the pivot table to update

ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "[Closed Cases].[Closed Date Week End].[Closed Date Week End]").ClearAllFilters

ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "[Closed Cases].[Closed Date Week End].[Closed Date Week End]"). _
    CurrentPageName = _
    "[Closed Cases].[Closed Date Week End].&[FilterDate]"

任何人都可以提供一些指导,说明我应该如何编码,以便它使用变量吗?

1 个答案:

答案 0 :(得分:2)

它被定义为一个字符串,但实际上是一个变量。更改此行代码:

"[Closed Cases].[Closed Date Week End].&[" & FilterDate & "]"