jeffreyweir
Active Member
Howdy folks. I've punched up some code that lets users quickly filter a PivotTable field based on an external range. So say you've got a pivotfield called 'Cities' with 10,000 cities in it, and an external range with say 100 of those cities. This code will let you very easily filter those 10,000 cities to just show the 100 of interest.
This is designed to go in your Personal Macro Workbook, so you can use it again and again.
Whenever you run it, you will be prompted to select the PivotField you want to filter (unless you have already selected a PivotField), and then you will be prompted to select the range of search terms, and then Excel will very quickly filter your PivotTable to match those terms.
It's pretty quick: here's how it performs on a PivotField containing 20,000 random alphanumeric strings:
Filter on 100 search terms: 7 seconds
Filter on 10,000 search terms: 1 minute, 30 seconds
Filter on 19,900 search terms: 4 seconds
The usual approach to a task like this is to just iterate through each pivotitem in the PivotItems collection, and check if that PivotItem is in the list of search terms. If it is, you make the PivotItem visible, otherwise you hide it. But on very large pivots, that can take ages because changing the .visible property is very slow, and under this approach, you are going to set that .visible property for every singe PivotItem in the PivotField.
A faster approach is to make all PivotItems visible, and then iterate through the PivotItems and then change the .visible status to FALSE if that PivotItem is NOT in the list of search terms. So if you only have say 19,900 search terms, then you will only use the .visible method 10 times. Much faster. But if you only have 10 items in your filter list, then you'll be setting that .Visible status 19,900 times. Very slow.
So my code works out whether it’s quickest to:
that method doesn't let you select multiple items anyway. Plus, as soon as you drag a page field with just one item showing to a row field, Excel clears the filter, so that all items are visible again. And you're back to square 1 again.
My code gets around this with a clever trick I discovered:
* it makes a temp copy of the pivot, and make the pf of interest a page field
* it then turns off multiple items and selects just one PivotItem - which pretty much instantly hides all the other items
* I then connect this temp pivot to the original pivot with a slicer. This very quickly syncs up the field on the original pivot with the temp copy, so that only one field is showing.
It would be good if you could take this code for a spin, and see if you turn up any issues. Note that you need the FilterPivot() routine to launch it.
Look forward to comments, feedback, suggestions.
Note that I'm using a dictionary with late binding. Had some issues with collections early on, which is why I switched to using a dictionary.
Regards,
Jeff
This is designed to go in your Personal Macro Workbook, so you can use it again and again.
Whenever you run it, you will be prompted to select the PivotField you want to filter (unless you have already selected a PivotField), and then you will be prompted to select the range of search terms, and then Excel will very quickly filter your PivotTable to match those terms.
It's pretty quick: here's how it performs on a PivotField containing 20,000 random alphanumeric strings:
Filter on 100 search terms: 7 seconds
Filter on 10,000 search terms: 1 minute, 30 seconds
Filter on 19,900 search terms: 4 seconds
The usual approach to a task like this is to just iterate through each pivotitem in the PivotItems collection, and check if that PivotItem is in the list of search terms. If it is, you make the PivotItem visible, otherwise you hide it. But on very large pivots, that can take ages because changing the .visible property is very slow, and under this approach, you are going to set that .visible property for every singe PivotItem in the PivotField.
A faster approach is to make all PivotItems visible, and then iterate through the PivotItems and then change the .visible status to FALSE if that PivotItem is NOT in the list of search terms. So if you only have say 19,900 search terms, then you will only use the .visible method 10 times. Much faster. But if you only have 10 items in your filter list, then you'll be setting that .Visible status 19,900 times. Very slow.
So my code works out whether it’s quickest to:
- make all items visible in the pivot, and then hide just the PivotItems that don’t match the filter terms; or
- hide all but one items in the PivotField, and unhide just the PivotItems that do match the filter terms
that method doesn't let you select multiple items anyway. Plus, as soon as you drag a page field with just one item showing to a row field, Excel clears the filter, so that all items are visible again. And you're back to square 1 again.
My code gets around this with a clever trick I discovered:
* it makes a temp copy of the pivot, and make the pf of interest a page field
* it then turns off multiple items and selects just one PivotItem - which pretty much instantly hides all the other items
* I then connect this temp pivot to the original pivot with a slicer. This very quickly syncs up the field on the original pivot with the temp copy, so that only one field is showing.
It would be good if you could take this code for a spin, and see if you turn up any issues. Note that you need the FilterPivot() routine to launch it.
Look forward to comments, feedback, suggestions.
Note that I'm using a dictionary with late binding. Had some issues with collections early on, which is why I switched to using a dictionary.
Regards,
Jeff