VBA Pre-Filtter PowerPivot Table RowField before Displaying in Pivot Table

dmarq

New Member
Joined
Jul 28, 2006
Messages
7
Greetings,

Based on end user requirements, I have created a VBA based Slicer that allows the user to choose the row field, rather than filter within a data field. I used this YT video as the basis of my code.

https://www.youtube.com/watch?v=nmAdW8-3E2I&t=285s

I was able to successfully implement this slicer, however, I also needed to introduce some additional parameters, of which one of them was to ensure the Pivot Table did not exceed more than 10 rows for a (Top 10 of item) as other pivot tables will be used in the same worksheet and would overlap and generate the dreaded 'A PivotTable cannot overlap another PivotTable report' error.

I used a Macro recorder to see the VBA code on how to add a filter to a PivotField before adding it to the PivotTable

(comments are mine)

Sub Macro2()
'
' Macro2 Macro
'

'
' Code to remove the view of previous xlRow Field

ActiveSheet.PivotTables("pvtHierVol").CubeFields( _
"[ReportHierarchy].[TEAM LEADER]").Orientation = xlHidden

' Method that enables users to apply a filter to PivotFields not yet added to the PivotTable by
' creating the corresponding PivotFieldobject

ActiveSheet.PivotTables("pvtHierVol").CubeFields( _
"[ReportHierarchy].[COLLECTOR]").CreatePivotFields

' Subsequent action to apply the 'Top 10 of Item' to the PivotTable

ActiveSheet.PivotTables("pvtHierVol").PivotFields( _
"[ReportHierarchy].[COLLECTOR].[COLLECTOR]").PivotFilters.Add2 Type:= _
xlTopCount, DataField:=ActiveSheet.PivotTables("pvtHierVol").CubeFields( _
"[Measures].[Sum of Outstanding_Amt(USD)]"), Value1:=10

' Final method to apply to insert the xlRowField
With ActiveSheet.PivotTables("pvtHierVol").CubeFields( _
"[ReportHierarchy].[COLLECTOR]")
.Orientation = xlRowField
.Position = 1
End With

End Sub



When I introduce the same code into my VBA, it appears the CreatePivotFields does not work or is not viewable, because i get a Run Time error '1004' - Unable to get the PivotFields property.

Below is my current pseudo code and where I get the error when I step-into the VBA code:

-------------------------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Selection")) Is Nothing Then

Dim Pvt As PivotTable
Dim Cf As CubeField
Dim Sh As Worksheet


Set Sh = ActiveSheet
Set Pvt = Sh.PivotTables("pvtHierVol")

If Range("Selection") = Range("Hierarchy.Type") Then
Exit Sub
Else

Select Case Range("Selection.Number")

Case 1
Pvt.CubeFields("[ReportHierarchy].[TEAM LEADER]").Orientation = xlHidden
Pvt.CubeFields("[ReportHierarchy].[COM]").Orientation = xlHidden

Pvt.ClearAllFilters

Pvt.CubeFields("[ReportHierarchy].[COLLECTOR]").CreatePivotFields

Pvt.PivotFields( _
"[ReportHierarchy].[COLLECTOR].[COLLECTOR]").PivotFilters.Add2 Type:= _
xlTopCount, DataField:=ActiveSheet.PivotTables("pvtHierVol").CubeFields( _
"[Measures].[Sum of Outstanding_Amt(USD)]"), Value1:=10

With Sh.PivotTables("pvtHierVol").CubeFields( _
"[ReportHierarchy].[COLLECTOR]")
.Orientation = xlRowField
.Position = 1
End With


Exit Sub

Case 2

...

End Select

End If

End If

End Sub

----------------------------------------------

I feel I need to Set the PivotField prior to the Case Statement, but I am not 100%, nor on how I would do that.

Appreciate any help, pointers, or feedback.

Thanks!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,215,079
Messages
6,123,005
Members
449,092
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top