• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA filter to a Set PivotItem

MrsLisa

New Member
Hello. I've learned how to create a pivot table using the pivot cache. My next step is to use vba to filter the table to 1 specific item. See the attached (I want to set the location filter to New York. Do you of know a good tutorial that covers this? Thank you.
 

Attachments

  • Colors 3.xlsm
    23.5 KB · Views: 4
Hi:

I added the following line to your Module 1 addCache subroutine
.CurrentPage = "New York"

Code:
Option Explicit

Sub addCache()
ThisWorkbook.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion).CreatePivotTable _
TableDestination:="R4C" & Range("A1").CurrentRegion.Columns.Count + 3

With ActiveSheet.PivotTables(1)
'First row field
With .PivotFields("Name")
.Orientation = xlRowField
.Position = 1
End With

'Report Filter field
With .PivotFields("Location")
.Orientation = xlPageField
.Position = 1
.CurrentPage = "New York"
End With

'Order Amount or numerical data  in the Values field
.AddDataField ActiveSheet.PivotTables(1).PivotFields("Order Amount"), _
"Sum of Amount", xlSum
End With


End Sub

Thanks
 
Back
Top