2

我正在通过不同工作表中的单元格引用更新 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]"

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

4

1 回答 1

2

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

"[Closed Cases].[Closed Date Week End].&[" & FilterDate & "]"
于 2013-10-01T21:29:48.587 回答