Macro to Filter in PIVOT Table.

harts15

New Member
Joined
Mar 18, 2019
Messages
5
Hi Guyz,

I am working with a Pivot. That has some 1000 project each having unique alpha numerical project codes and there budgets for the entire year.

I have grouped the multiple projects under Labs. So I want to filter the project codes in the pivot which in turn effects an Chart.

I tried to use macro recording but it returns a Macro with lot of lines pivot item ac039 = false and finally gives an error that the procedure is too large.
I got the below macro. But this un-ticks the project codes one by one and each time the entire report refreshes and the slicers run taking several hours.

I tried using manual update = true but i dont know whether it is kicking in or not. All i need is remove all filters and keep the project codes which I need to have. The project codes may vary from 1 to 10 project codes at a time.

<code style="margin: 0px;">ption Explicit

Sub FilterPivotItems()

Dim PT As PivotTable
Dim PTItm As PivotItem
Dim FiterArr()AsVariant

' use an array to select the items in the pivot filter you want to keep visible
FiterArr
= Array("101","105","107")

' set the Pivot Table
Set PT = ActiveSheet.PivotTables("PivotTable3")

' loop through all Pivot Items in "Value" Pivot field
ForEach PTItm In PT.PivotFields("Value").PivotItems
IfNot IsError(Application.Match(PTItm.Caption, FiterArr,0))Then' check if current item is not in the filter array
PTItm
.Visible =True
Else
PTItm
.Visible =False
EndIf
Next PTItm

EndSub</code>
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Weclome to the forum,

WHat version of Excel are you using as you could consider using Slicers.
 
Upvote 0
Hi Trevor.

I tried that also If i create a slicer and recorded a macro for selecting the codes I need. Even then I am getting a long macro.
After running the macro it goes by checking out each code one by one. Making the report refresh. I am actually working in a excel dash board created by some one. He has hidden the raw data. So I dont have the raw data. there are may 20 pivots and several work sheets and slicers connected to the raw data so when i make any change the entire work book gets refreshed taking 1-2 minutes. So I just need to go the Report filter clear all filters. Check mark only a few codes that I need. This will provide a dash board view along with a graph I need to copy that graph and paste it in a separate excel sheet.
 
Upvote 0
thank you for the time though all I need is apply the filter with the codes I need. I will mostly record macro for rest of the steps. Thank you again
 
Upvote 0
Hi People.

Finally I source a Code that issuer close to what I want but this filters only the value in the cell b47 how do I get it to filter values in a range like a2:a10

Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
Set pt = Worksheets("Pivots").PivotTables("PivotTable4")
Set Field = pt.PivotFields("PRN")
NewCat = Worksheets("PivotS").Range("b47").Value

With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

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