PDA

View Full Version : Trouble setting pivot page filter using VBA



werafa
03-13-2014, 11:03 PM
Hi all,

I have 4 pivot tables that connect to an external data source - and I am trying to use VBA to preset the pivot filters.
....
and failing
....

The following code generates an "unable to set page property of pivotfield class" error


Sub FilterPivots()
Dim qcode As String
Dim pT As PivotTable
Dim wB As Workbook
Dim wS As Worksheet
Dim myString As String

Set wB = ActiveWorkbook

'Call RunFast
qcode = Worksheets("Instructions").Range("C7").Value
myString = "[Facility].[Quick Code].&[" & qcode & "]"
If qcode = "AAN" Then qcode = GetQCode
For Each wS In wB.Worksheets
For Each pT In wS.PivotTables
'On Error Resume Next
pT.PivotFields("[Facility].[Quick Code].[Quick Code]").ClearAllFilters
pT.PivotFields("[Facility].[Quick Code].[Quick Code]").CurrentPage = "[Facility].[Quick Code].&[MJP]" ' string is as recorded by macro recorder - macro fails at this point
'original recording showed activesheet.pivottables(".......... .Pivotfields etc.
'pT.PivotFields("[Facility].[Quick Code].[Quick Code]").CurrentPage = mystring
'On Error GoTo 0
Next pT
Next wS
Call ResetApp

Set wB = Nothing
Set wS = Nothing
Set pT = Nothing
End Sub

The goal is to loop through all pivot tables in workbook and set the 'quick code' field - which is set in the page filters section of the table
This code is a mash plagiarised from the best(?) of the web (the looping bit - this works) and the macro recorder (clear filters works, but set filters does not)

I run win7, Office 2013 - 64 bit, and use slicers in the pivot tables.
Does anyone know where I am getting lost?

Thanks
Tim

werafa
03-16-2014, 03:52 PM
addendum - this one still has me beat.
Can't find much help on the web - at least not that I can understand sufficiently to manipulate.

The Pivot has three page (filter) fields, one of which has a slicer attached, three row fields - 1 with slicer, and 1 data field
The filter I wish to set contains a 3 leter string - MJP in the instance above.

I seem to be getting stuck trying to reference the pagefield or currentpage correctly. What is annoying is that the macro recorder cannot write code that is repeatable.

werafa
03-16-2014, 04:05 PM
as a hint, here is the macro recorder generated code with nothing extra


Sub Macro2()
'
' Macro2 Macro
'

'
ActiveSheet.PivotTables("PivotTable4").PivotFields("[Facility].[Quick Code].[Quick Code]").ClearAllFilters
ActiveSheet.PivotTables("PivotTable4").PivotFields("[Facility].[Quick Code].[Quick Code]").CurrentPage = "[Facility].[Quick Code].&[MJP]"
End Sub

this fails on the second line with "Unable to set the CurrentPage property of the PivotField Class"

werafa
03-18-2014, 02:50 PM
bumping this back up as I still have no solution.
my only clue is the presence of 'cubefield' as a pivottable subproperty - this table is fed by an external olap cube datasource