Macro to apply advanced pivot table filters based on cell value

egrospe17

New Member
Joined
May 31, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi Excel Geniuses,

I have a pivot table that contains some location data called "Milepost". One column is Start Milepost and the other is End Milepost. I need to be able to be able to filter data that is greater than or equal to the Start Milepost AND less than or equal to the End Milepost.

What I need to be able to do is to have the user enter a number on a cell for Start Milepost and another number on another cell for End milepost and use those values to apply the filters to the columns accordingly.

I wrote the VBA code below but it's not working and I'm stuck. HELP!


VBA Code:
Sub PivotFilter()

ActiveSheet.PivotTables("PivotTable2").PivotFields("Start MP").PivotFilters. _
Add2 Type:=xlCaptionIsGreaterThanOrEqualTo, Value1:=Sheets("Consolidated Pivot").Range("A1").Value

ActiveSheet.PivotTables("PivotTable2").PivotFields("End MP").PivotFilters.Add2 _
Type:=xlCaptionIsLessThanOrEqualTo, Value:=Sheets("Consolidated Pivot").Range("B1").Value

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,789
VBA Code:
Sub PivotFilter()

ActiveSheet.PivotTables("PivotTable2").PivotFields("Start MP").ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("End MP").ClearAllFilters

ActiveSheet.PivotTables("PivotTable2").PivotFields("Start MP").PivotFilters. _
Add2 Type:=xlCaptionIsGreaterThanOrEqualTo, Value1:=Sheets("Consolidated Pivot").Range("A1").Value

ActiveSheet.PivotTables("PivotTable2").PivotFields("End MP").PivotFilters.Add2 _
Type:=xlCaptionIsLessThanOrEqualTo, Value1:=Sheets("Consolidated Pivot").Range("B1").Value

End Sub
 

egrospe17

New Member
Joined
May 31, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub PivotFilter()

ActiveSheet.PivotTables("PivotTable2").PivotFields("Start MP").ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("End MP").ClearAllFilters

ActiveSheet.PivotTables("PivotTable2").PivotFields("Start MP").PivotFilters. _
Add2 Type:=xlCaptionIsGreaterThanOrEqualTo, Value1:=Sheets("Consolidated Pivot").Range("A1").Value

ActiveSheet.PivotTables("PivotTable2").PivotFields("End MP").PivotFilters.Add2 _
Type:=xlCaptionIsLessThanOrEqualTo, Value1:=Sheets("Consolidated Pivot").Range("B1").Value

End Sub

Thanks so much! This did the trick!!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,995
Messages
5,639,446
Members
417,090
Latest member
schoelleya

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
Top