Disable pivot filter with VBA

Binbs

New Member
Joined
Jan 7, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a pivot table with a filter applied. I do not want users to inadvertently clear this filter.

I have been researching how to disable a pivot filter using VBA and found the following VBA code: ActiveSheet.PivotTables(1).RowFields(2).EnableItemSelection = False

This seems to be the code I need but I am having trouble implementing it due to my limited VBA experience.

The name of my pivot table is called "OL" and the filter I want to disable is on row 18. here is what I have tried:

Sub BLOCK()

Dim wks As Worksheet
ActiveSheet.PivotTables("OL").RowFields(18).EnableItemSelection = False
End Sub


This does not appear to work however. Any help with VERY appreciated.

Thank you!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I also tried this to no avail:

Sub DesativaFiltroPivotTables()
Dim pt As PivotTable
Dim pf As PivotField

For Each pt In ActiveSheet.PivotTables
For Each pf In pt.PivotFields
pf.EnableItemSelection = False
Next pf
Next
End Sub
 
Upvote 0
Try referring to your first RowField . . .

VBA Code:
ActiveSheet.PivotTables("OL").RowFields(1).EnableItemSelection = False

Hope this helps!
 
Upvote 0
Try referring to your first RowField . . .

VBA Code:
ActiveSheet.PivotTables("OL").RowFields(1).EnableItemSelection = False

Hope this helps!
thanks! But no luck unfortunately,
 
Upvote 0
The name of my pivot table is called "OL" and the filter I want to disable is on row 18. here is what I have tried:

Sub BLOCK()

Dim wks As Worksheet
ActiveSheet.PivotTables("OL").RowFields(18).EnableItemSelection = False
End Sub
You need to refer to your RowFields by PivotField index number or name, not by worksheet row reference. So, for example, if you want to disable the item selection for a single PivotField in your Rows area . . .

VBA Code:
ActiveSheet.PivotTables("OL").RowFields(1).EnableItemSelection = False

or

VBA Code:
ActiveSheet.PivotTables("OL").RowFields("PivotFieldName").EnableItemSelection = False

I also tried this to no avail:

Sub DesativaFiltroPivotTables()
Dim pt As PivotTable
Dim pf As PivotField

For Each pt In ActiveSheet.PivotTables
For Each pf In pt.PivotFields
pf.EnableItemSelection = False
Next pf
Next
End Sub
This should disable the item selection for all PivotFields in all PivotTables within the active worksheet. After running this code, the drop down arrow for all PivotFields should disappear. Is this not the case?
 
Upvote 0
Solution
You need to refer to your RowFields by PivotField index number or name, not by worksheet row reference. So, for example, if you want to disable the item selection for a single PivotField in your Rows area . . .

VBA Code:
ActiveSheet.PivotTables("OL").RowFields(1).EnableItemSelection = False

or

VBA Code:
ActiveSheet.PivotTables("OL").RowFields("PivotFieldName").EnableItemSelection = False


This should disable the item selection for all PivotFields in all PivotTables within the active worksheet. After running this code, the drop down arrow for all PivotFields should disappear. Is this not the case?
Hi Domenic,

The second macro to disable all filter in the pivot does in fact work the way you said it should. Thank you very much for your comments and help.
 
Upvote 0
That's great, I'm glad you were able to work it out.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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