ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can't change pivotfilter value1 parameter in code (https://www.excelbanter.com/excel-programming/420650-cant-change-pivotfilter-value1-parameter-code.html)

PeterF

Can't change pivotfilter value1 parameter in code
 
I have a workbook that has a bunch of pivottables that are based on an
Analysis Services cube. Each of the pivottables has a filter on it that
limits the row data to one customer account. Once a week I go in an manually
change the filter for the "Account ID" field so that the pivottables show the
data in the pivots for a different account.

All works great, except it is manual so I took some time and tried to write
some VB code to automate the update for all pivots. To get started a
recorded a macro. The macro has the following code:
ActiveSheet.PivotTables("PivotTable1").PivotFields ( _
"[Account].[Account ID].[Account ID]").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields ( _
"[Account].[Account ID].[Account ID]").PivotFilters.Add
Type:=xlCaptionEquals _
, Value1:="194108"

Here's what I don't understand, when I run the macro, I get an "Out of
Memory" error when it attempts to "ClearAllFields". So I removed that line
of code and I am just running the second line...
ActiveSheet.PivotTables("PivotTable1").PivotFields ( _
"[Account].[Account ID].[Account ID]").PivotFilters.Add
Type:=xlCaptionEquals _
, Value1:="194108"

The thing is, when I run this line by itself, I get an error:
"Run-time error '1004':
Application-defined or object-defined error"

Why am I able to do this manually, when I recorded the macro, but not when
run it through code? My guess is that it doesn't let me "add" a filter to
something that already has the filter (hence the "ClearAllFilters" that the
recorded macro had). Is there a workaround where I could just change the
filter value? I tried just setting the value1 parameter (which says is
read/write) with the following:

Worksheets("Major App Usage over 30
days").PivotTables("Pivottable1").RowFields(1).Piv otFilters(1).Value1 =
"132238"

But that didn't work because I got a "Wrong number of arguments or invalid
property assignment".

Any ideas for a workaround?


All times are GMT +1. The time now is 12:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com