VBA to automatically create individual pivot tables for each row header and allow filtering by region

ninaking

New Member
Joined
Jun 5, 2011
Messages
15
Hello VBA Gurus,

Could really use your expertise. I'm trying to create a project schedule that can be filtered by region. The only way I found to do that was to create Pivot Tables and filter each one. I need to be able to automatically create the pivot tables and filter each caption by whatever filter critieria the user enters.

I created the following VBA but I need the PivotField names to be dynamically pulled from the table (as opposed to hard coded the way it is now) and I also need the pivot tables to be automatically created (I created these manually).

Please help!

This is the macro:
Sub changeFilterCriteria()
strName = InputBox(Prompt:="Please enter region to filter by.", _
Title:="ENTER REGION NAME", Default:="(West)")
ActiveSheet.PivotTables("PivotTable1").PivotFields("June 23 - June 29"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("June 23 - June 29"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable2").PivotFields("July 7 - July 13"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("July 7 - July 13"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable3").PivotFields("July 14 - July 18"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable3").PivotFields("July 14 - July 18"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable4").PivotFields("July 21 - July 27"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable4").PivotFields("July 21 - July 27"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable5").PivotFields("July 28 - Aug 3"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable5").PivotFields("July 28 - Aug 3"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable6").PivotFields("Aug 4 - Aug 10"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable6").PivotFields("Aug 4 - Aug 10"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable7").PivotFields("Aug 11 - Aug 17"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable7").PivotFields("Aug 11 - Aug 17"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable8").PivotFields("Aug 18 - Aug 24"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable8").PivotFields("Aug 18 - Aug 24"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable9").PivotFields("Aug 25 - Aug 29"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable9").PivotFields("Aug 25 - Aug 29"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName

ActiveSheet.PivotTables("PivotTable10").PivotFields("Sep 1 - Sep 4"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable10").PivotFields("Sep 1 - Sep 4"). _
PivotFilters.Add Type:=xlCaptionContains, Value1:=strName
End Sub


And this is what the Data Source for the Pivot looks like:

Jul 7 - Jul 11 Jul 14 - Jul 18 Jul 21 - Jul 25 Jul 28 - Aug 14Aug 4 - Aug 8
Develop talking points for communications Meeting with Admin Managers (Northeast) Meeting with Admin Managers (West) Training (Northeast) Training (West)
Meeting with RVPs (Northeast) Meeting with RVPs (West)
Meeting with PLMs (Northeast) Meeting with PLMs (West)
Leadership Meeting

<tbody>
</tbody><colgroup><col><col><col><col><col><col><col><col><col></colgroup>
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I forgot to put the
Code:
wrapper around the code and now it won't let me edit the post. Sorry about that!
 
Upvote 0

Forum statistics

Threads
1,215,197
Messages
6,123,585
Members
449,108
Latest member
rache47

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