Help with VBA - PivotTable Macro

astrontelstar

New Member
Joined
Feb 2, 2016
Messages
1
My company provides me a PivotTable report in Excel. I need the report filtered by a series of ZipCodes. I would like assistance with something easier than manually going through the filter and selecting 200+ zip codes.

When using a macro recorder to see what Excel would "auto generate" for me, I got the following code about 50 times, as their are thousands of zip codes in this column.

I am looking to see if someone can help me with a simple macro loop that would take a set of zip codes like "75010, 75020, 75030" maybe in a string and change the pivotTable to filter by those, without the thousands of lines the Macro recorder seems to one to do.

Anyone? My boss just handed me the list of zip codes so I have them as outlined above.

Help?

With ActiveSheet.PivotTables("PivotTable3").PivotFields("shipping_zip")
.PivotItems("00000").Visible = False
.PivotItems("00802").Visible = False
.PivotItems("00918").Visible = False
.PivotItems("01001").Visible = False
.PivotItems("01002").Visible = False
.PivotItems("01007").Visible = False
.PivotItems("01013").Visible = False
.PivotItems("01020").Visible = False
.PivotItems("01027").Visible = False
.PivotItems("01028").Visible = False
.PivotItems("01033").Visible = False
.PivotItems("01034").Visible = False
.PivotItems("01038").Visible = False
.PivotItems("01040").Visible = False
.PivotItems("01050").Visible = False
.PivotItems("01053").Visible = False
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Assuming that "Sheet1" contains your pivot table, and that the pivot table is named "PivotTable1", try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] FilterZipCodes()

    [COLOR=darkblue]Dim[/COLOR] oPivotItem [COLOR=darkblue]As[/COLOR] PivotItem
    [COLOR=darkblue]Dim[/COLOR] vZipCodes [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] vMatchVal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    
    vZipCodes = Array("75010", "75020", "75030") [COLOR=#008000]'change and/or add zip codes as desired[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("shipping_zip")
        .ClearAllFilters
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oPivotItem [COLOR=darkblue]In[/COLOR] .PivotItems
            vMatchVal = Application.Match(oPivotItem.Value, vZipCodes, 0)
            oPivotItem.Visible = [COLOR=darkblue]Not[/COLOR] IsError(vMatchVal)
        [COLOR=darkblue]Next[/COLOR] oPivotItem
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Change the name of the sheet and pivot table, accordingly.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,216,462
Messages
6,130,781
Members
449,591
Latest member
sharmavishnu413

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