Automating Pivot Table Filter Selection

blood_Milk_sky

New Member
Joined
Nov 16, 2016
Messages
10
Hi,

I have just recorded the below macro to go through and select each account number on a pivot table filter. I was just wondering if anyone knew any code that would do the below for all values in the filter instead of me going through and recording it ac number by account number?

the reason for this is that each time I run this there wont always be the same number of accounts or account numbers in the pivot table filter.

any help would be greatly appreciated.

many thanks

BMS

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        CurrentPage = "120000TST"
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        CurrentPage = "120003"
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        CurrentPage = "120005"
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID"). _
        CurrentPage = "120008003"
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I don't know the structure of your dataset. I am assuming these business partner id's ar in one column of the data tabel and you want to loop through each unique BP ID.

When the word 'unique' appears, think 'collection'. A Collection is a dataformat in VBA where you can store a collection of data together with a unique key to each datapoint. The trick is to set the key the same as the data you are storing, when you try to add a key that is already held in the collection, it will give you an error, which yo can trap, and the data doesn't get adde. This way you get the unique list, without duplicates. OK, I probably complety lost you so let's do a small example first:

in a sheet:

Book1
A
1mon
2sat
3sun
4mon
5tue
6thu
7fri
8sat
9tue
10wed
11thu
12fri
13wed
14sun
Sheet4


Now add this code to a module:

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> CollectionDemo()<br>    <SPAN style="color:#00007F">Dim</SPAN> colC <SPAN style="color:#00007F">As</SPAN> Collection<br>    <SPAN style="color:#00007F">Dim</SPAN> lR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> rC <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> v <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <br>    <SPAN style="color:#007F00">' Initialise the collection</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> colC = <SPAN style="color:#00007F">New</SPAN> Collection<br>    <br>    <SPAN style="color:#007F00">' load the data in the collection _<br>      We trap the error to avoid the _<br>      program halting. We can safely go to _<br>      the next line.</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rC <SPAN style="color:#00007F">In</SPAN> Range("A1:A14")<br>        <SPAN style="color:#007F00">' add each cell to the collection as value and as key</SPAN><br>        colC.Add Item:=rC.Value, Key:=rC.Value<br>    <SPAN style="color:#00007F">Next</SPAN> rC<br>    <SPAN style="color:#007F00">' set error behaviour back to normal</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <br>    MsgBox "Collection colC has: " & colC.Count & " items."<br>    <br>    <SPAN style="color:#007F00">' now output each item in the collection to column B</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rC = Range("B1")<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> v <SPAN style="color:#00007F">In</SPAN> colC<br>        rC = v<br>        <SPAN style="color:#00007F">Set</SPAN> rC = rC.Offset(1, 0)<br>    <SPAN style="color:#00007F">Next</SPAN> v<br>    <br>    <SPAN style="color:#007F00">'clean up</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> colC = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rC = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Now run the code.
It will first tell you that there are 7 items in the collection, then it will list these in column B.

In my next post I will show how to use this for your issue
 
Upvote 0
OK, you want to filter your pivo table using each of the ID's held in a column, but you don't (obviously) want to use duplicate ID's.

So with the above knowledge of Collections you can read the whole list into a collection and so filter out any duplicates.
Next you can go through each item in the collection and create your filter from that.
Because I don't know the size of your table, it could be huge, I don't want to step through each cell of the column holding the ID's. Reading from (and writing to) the spreadsheet is slow. So we read the complete column into an array and then read the data from the array. Much faster.

Read the comments in the code. Adjust sheet names and cell address as instructed.

Notice that I replace ActiveSheet with a worksheet object wsPivot set to the sheet holding the pivot table. That way the macro will run properly regardless of which sheet is visible at the time. Let me know how it goes

'-----------------------------------------------------
<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><br><br><SPAN style="color:#00007F">Sub</SPAN> FilterOnItems()<br>    <SPAN style="color:#00007F">Dim</SPAN> colC <SPAN style="color:#00007F">As</SPAN> Collection<br>    <SPAN style="color:#00007F">Dim</SPAN> lR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lC <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> rC <SPAN style="color:#00007F">As</SPAN> Range, rID <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> vDat <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, vID <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wsData <SPAN style="color:#00007F">As</SPAN> Worksheet, wsPivot <SPAN style="color:#00007F">As</SPAN> Worksheet<br><br>    <SPAN style="color:#007F00">' Initialise the collection</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> colC = <SPAN style="color:#00007F">New</SPAN> Collection<br>    <br>    <SPAN style="color:#007F00">' set the wsData to the worksheet holding the data table</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsData = Sheets("Datasheet") <SPAN style="color:#007F00">' <<< modify sheet name to suit</SPAN><br>    <SPAN style="color:#007F00">' set the wsPivot to the worksheet holding the Pivot table</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsData = Sheets("Pivot") <SPAN style="color:#007F00">' <<< modify sheet name to suit</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">' set rID to the first cel of the ID column, below the header</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rID = wsData.Range("B2") <SPAN style="color:#007F00">' <<< modify cell to suit</SPAN><br>    <br>    <SPAN style="color:#007F00">' get the number of rows in the database</SPAN><br>    lR = rID.CurrentRegion.Rows.Count - rID.Row + 1<br>    <br>    <SPAN style="color:#007F00">' read the column into an array. One read, so fast.</SPAN><br>    vDat = rID.Resize(lR, 1).Value<br>    <br>    <SPAN style="color:#007F00">' load the data in the collection _<br>      We trap the error to avoid the _<br>      program halting. We can safely go to _<br>      the next line.</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> lC = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(vDat, 1)<br>        <SPAN style="color:#007F00">' add each ID to the collection as value and as key</SPAN><br>        colC.Add Item:=vDat(lC, 1), Key:=vDat(lC, 1)<br>    <SPAN style="color:#00007F">Next</SPAN> lC<br>    <SPAN style="color:#007F00">' set error behaviour back to normal</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <br>    <SPAN style="color:#007F00">'while testing the macro this will print out into the _<br>     "Immediate Window" in the VBA editor</SPAN><br>    Debug.Print "Collection colC has: " & colC.Count & " items."<br>    <br>    <SPAN style="color:#007F00">' now filter the pivot table on each ID in the collection in turn</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> vID <SPAN style="color:#00007F">In</SPAN> colC<br>        <SPAN style="color:#00007F">With</SPAN> wsPivot.PivotTables("PivotTable1").PivotFields("Business" & Chr(10) & "Partner ID")<br>            .ClearAllFilters<br>            .CurrentPage = vID<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#007F00">' do whatever needs to be done with the output</SPAN><br>        <SPAN style="color:#007F00">'.....</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> vID<br>    <br>    <SPAN style="color:#007F00">'clean up</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> colC = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsData = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsPivot = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rID = <SPAN style="color:#00007F">Nothing</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Why not use the "Show Report Filter Pages..." ?

When in a Pivot Table that uses items in the Page Filter, go through the following ribbon path.
Pivottable tools/Analyze/Pivot Table/Options
select "Show Report Filter Pages"

This will create new worksheets for each available value in the selected filter item.
I use this feature to provide reports to 100's of individuals. I do have to select all the sheets and Copy/Paste as values to obliterate the Pivot Table so that the receivers cannot see the data for other locations.
 
Upvote 0

Forum statistics

Threads
1,224,760
Messages
6,180,816
Members
452,996
Latest member
nelsonsix66

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