How to update powerpivot pivot table filter via cell reference?

廉价感情. 提交于 2019-12-11 00:38:02

问题


I am working on updating a Powerpivot pivot table via a cell reference in a different worksheet but am having trouble with determining the correct syntax.

The code works just fine if I hard-code a date (see below):

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]"

However, if I try to use a variable rather than hard-code a date, I get an "Application Defined or Object Defined error" message.

This is the code I'm trying to use:

'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]"

Can anyone give some guidance on how I should code this so that it uses the variable?


回答1:


It's defined as a string but is really a variable. Change this line of code:

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


来源:https://stackoverflow.com/questions/19126266/how-to-update-powerpivot-pivot-table-filter-via-cell-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!