VBA Pivot filtering

bigboss89

New Member
Joined
Sep 28, 2018
Messages
2
Hello guys

I'm trying to do a pivot table using VBA alongside multiple other things, however I'm trying to filter the pivot table using the VBA to have account_number to be equal to 123456, I've done this code, however I don't know how to add the extra line to filter as mentioned above.

VBA Code:
Set ws = ActiveSheet
Set wb = ThisWorkbook

Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Range("Bank[#All]"), _
    Version:=xlPivotTableVersion15)
    
Set pt = ws.PivotTables.Add(PivotCache:=pc, TableDestination:=Range("AR2"), TableName:="Pivot")


With pt

.PivotFields("narrative_account_number").Orientation = xlRowField
.PivotFields("Debit amount").Orientation = xlDataField

End With

Is there a way to add the filter to the above mentioned code?

Thanks in advance.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
VBA Code:
    With pt.PivotFields("narrative_account_number") 
        .ClearAllFilters
         For Each pi In .PivotItems
                If pi.Name <> "123456" Then Pi.Visible = False
         Next
    End With
    pt.RefreshTable
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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