Can't change pivotfilter value1 parameter in code

P

PeterF

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).PivotFilters(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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top