VBA Hide Pivot entry featuring text substring as part of string

kalikj

Board Regular
Joined
Sep 4, 2009
Messages
108
Please can anyone help? I would like to hide PIVOT row entries featuring a specific substring as part of the text string for that entry.

By using the following code, I can specify the name of a full entry to be hidden:

PHP:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("SPECIFIED FIELD")        .PivotItems("PRIVATE LABEL").Visible = False            End With


I would like to hide any entry featuring the following substring (which always starts from position 1): "UK_CORP_MASK_".

I have tried using the InStr function in place of the specified name, but this crashes the code.

Please can anyone suggest a solution.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
like this?

Code:
Sub FilterPivot()
  Dim sht As Worksheet
  Dim pvt As PivotTable
  Dim pvf As PivotField
  Dim pvi As PivotItem
  Dim txt As String
  
  Set sht = ThisWorkbook.Sheets("Sheet1")
  Set pvt = sht.PivotTables("PivotTable1")
  Set pvf = pvt.PivotFields("Field1")
  txt = "UK_CORP_MASK"
  
  For Each pvi In pvf.PivotItems
    pvi.Visible = Not InStr(1, pvi.Name, txt, vbTextCompare)
  Next pvi
End Sub
 
Upvote 0
Yes, perfect!

Thank you ParamRay. :)

The only downside, which is unavoidable, is where one is working with a large number of Pivot Items, as it takes a considerable length of time to cycle through them.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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