Filter values in pivot table by reference to a range

dylans

New Member
Joined
Jun 18, 2010
Messages
12
The title says it all; after doing much reading and referencing unhelpful MSDN posts, I came to a dead end.

I have cells A1:A10 which have certain values entered in manually.

I have a pivot table with data similar to the data entered in manually for cells A1:A10.

I want the pivot items in my pivot table to show only the items that are in the range A1:10, and hide items that were not found.

This is what I've come up with so far:
Code:
Sub PT()
'
'PT Macro
'
Dim PT As PivotTable
Set PT = Sheets("Sheet3").PivotTables("PivotTable2")
PT.PivotFields("Server").CurrentPage = Sheets("Sheet2").Range("a1:a10").Value
Set PT = Nothing
End Sub
Unfortunately it will filter only on one cell within the range, not all 10.
Any help with this would be greatly appreciated!

-Dylan
 
What Andrew posted worked well, the downside is if I have 200 some entries in the pivot filter to go off of, naturally looping like that takes awhile. Out of curiosity, is there any any way to make it more efficient?
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi, dylans. This might help? Untested. F

Code:
Sub PT()
  '
  'PT Macro
  '
  Dim PT As PivotTable
  Dim PI As PivotItem
 
  Set PT = Sheets("Sheet3").PivotTables("PivotTable2")
  PT.PivotFields("Server").CurrentPage = "(All)"
 
  PT.ManualUpdate = True
  For Each PI In PT.PivotFields("Server").PivotItems
    PI.Visible = WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A10"), PI.Name) > 0
  Next PI
  PT.ManualUpdate = False
  Set PT = Nothing
 
End Sub
 
Upvote 0
Thanks for the suggestion, I had an error in my code but that fixed it :-P

Still takes a bit to run through, maybe I'm too impatient :biggrin:
 
Upvote 0
The WorksheetFunction might be an execution speed bottleneck. Maybe instead load the values (in VBA) into an array and check against that array? Or maybe a collection?

Maybe even take a totally different approach. I can't be sure without some sample data. The idea being to join the two data sources - one the full data and the other the list of values to show - via SQL and feed that dataset into the pivot table. Maybe without VBA, depending on requirements.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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