[VBA] Excel PivotTable Filtering

ZombieHairdu

New Member
Joined
Oct 1, 2019
Messages
4
Code:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Assignment Descriptor" _[INDENT])
.PivotItems("CCR Review").Visible = False
.PivotItems("CCR Review ").Visible = False[/INDENT]
End With

There are numerous other fields listed, about 25 in total

Question 1: Is there a way to just provide a wild card so that I don't have to include potentially having situations were they might have added 2 spaces at the end of the name?
Question 2: Lets say that I ONLY want to display the options "Manage HSC", "GAP Review", and "Other" from the PivotTable filter. Do I HAVE to call out every row and say false?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
for question 2
i think pivot items are numbered, so instead of
Code:
PivotItems("CCR Review").Visible = False
you can use
Code:
PivotItems(1).Visible = False
if CCR Review is the first item in the list
then just loop through all the items with something like
For i=1 to i=count of items
PivotItems(i).Visible = False
and add some line about excluding the ones you want to display
 
Upvote 0
I am really bad at VBA -- most of the content I have right now is through recording and editing the recording. I tried to make sense of it, but I genuinely don't even know what the code would look like.
 
Upvote 0
Question 1: Is there a way to just provide a wild card so that I don't have to include potentially having situations were they might have added 2 spaces at the end of the name?

Code:
Sub testPivot1()
  Dim pi As PivotItem
  Application.ScreenUpdating = False
  With ActiveSheet.PivotTables("PivotTable1").PivotFields("Assignment Descriptor")
    .ClearAllFilters
    For Each pi In .PivotItems
      Select Case True
        Case LCase(pi) Like LCase("CCR Review" & [B][COLOR=#0000ff]"*"[/COLOR][/B])
          pi.Visible = False
      End Select
    Next
  End With
End Sub


Question 2: Lets say that I ONLY want to display the options "Manage HSC", "GAP Review", and "Other" from the PivotTable filter. Do I HAVE to call out every row and say false?

Code:
Sub testPivot2()
  Dim pi As PivotItem
  Application.ScreenUpdating = False
  With ActiveSheet.PivotTables("PivotTable1").PivotFields("Assignment Descriptor")
    .ClearAllFilters
    For Each pi In .PivotItems
      Select Case LCase(pi)
        Case [COLOR=#0000ff]LCase("Manage HSC"), LCase("GAP Review"), LCase("other")[/COLOR]
        Case Else
          pi.Visible = False
      End Select
    Next
  End With
End Sub

Try the options and tell me.
 
Upvote 0
Perfect that worked absolutely flawlessly. Thank you Dante Amor.

My next quest is to figure out why when I use VBA to color the pivot table the color shows but when I unfilter it, the color disappears until I refilter it. Basically I filter the sheet down to for instance Manage HSC and then give those rows a specific color, then when everything has its specific color I unfilter to show the entire sheet which should show all the different colors but instead only shows white. Any ideas on that?

This is my color code..
Code:
'COLORS ON PIVOT TABLE
     ActiveSheet.PivotTables("Full Inventory").PivotFields("LOB").ClearAllFilters
     ActiveSheet.PivotTables("Full Inventory").PivotFields("LOB").CurrentPage = _
          "CNS DME"
     ActiveSheet.PivotTables("Full Inventory").PivotSelect _
          "'Assigned To' CNS DME" _
          , xlDataAndLabel + xlFirstRow, True
     With Selection.Interior
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
          .ThemeColor = xlThemeColorAccent2
     End With
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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