Change Pivot filter Value to exclude all others

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hello VBA Pros,

I am trying to edit the below code so that if the filter Items do not = Month_Name and are not selected. i.e. If Month_name = March then March must be the only Month visible so I need to add <>Month_name then .visible = false, I havnt been able to figure out the correct way to writing this.

Code:
Dim Month_Name As Range
Set Month_Name = Range("C7")
    With Sheets("List").PivotTables("PivotTable2").PivotFields("MONTH")
        .PivotItems(Month_Name.Value).Visible = True
    End With
    End With
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
Dim Month_Name As Range
Set Month_Name = Range("C7")
    With Sheets("List").PivotTables("PivotTable2").PivotFields("MONTH")
        .PivotItems(Month_Name.Value).Visible = True
        For each pi in .pivotitems
           If pi.name <> month_name.value then pi.visible = false
         Next pi
    End With

If you have 2007 or later you can just apply a filter.
 
Upvote 0
Hey Rory,

I must say this code has been so helpfull, thank you for the help, I have updated the code as well :)

Code:
Sub FilterRegionalPT()
Dim Region_Name As Range
Dim i As Integer
Dim PT_Counta As Integer
pt_count = ActiveSheet.PivotTables.Count
Set Region_Name = Range("B2")
i = 0
 
Application.ScreenUpdating = False

Do
    On Error Resume Next
        i = i + 1
        With Sheets(11).PivotTables(i).PivotFields("REGION")
            .PivotItems(Region_Name.Value).Visible = True
             For Each Pi In .PivotItems
                If Pi.Name <> Region_Name.Value Then Pi.Visible = False
             Next Pi
        End With
    On Error GoTo 0
Loop Until i = pt_count
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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