Filter PivotFields Containing String

ckrawiec

New Member
Joined
Oct 21, 2016
Messages
4
I have a pivot table and I'd like to filter by pivotfield: "Inspectionitem". I'd originally done it like this:

Code:
'data worksheet and cache defined up here
Set pt1 = ActiveSheet.PivotTables.Add(cacheofpt1, Range("A1"), "DP_Electrical")
 
'put the feilds in
With pt1[INDENT]'blahblahblah other pivotfields here[/INDENT]
 
    With .PivotFields("Inspectionitem")
        .Orientation = xlPageField
        .Position = 1
        .PivotItems("General Characteristic for Road Test").Visible = True
        On Error Resume Next[INDENT]'.pivotitems("abc").visible = False    
End With[/INDENT]
End With

I could go through every pivotitem and define .visible to be true or false, but there are well over 100 pivotitems and I don't want to go through all of them. I came up with the following to help. It's supposed to make all pivotitems containing the string "auto" visible. The problem is it never recognizes a pivotitem containing the string, even when I hold the cursor over p and I can see that it does.

Code:
Sub pivot()
Set pf = ActiveSheet.PivotTables("DP_Electrical").PivotFields("inspectionitem")
For Each p In pf.PivotItems
    If p.Name = "*auto*" Then
    p.Visible = True
    End If
Next p
End Sub

If I change p.Name to just p, the same problem occurs.

Any help is greatly appreciated! Thanks in advance
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I've solved my own problem. See below for successful code:

Code:
With mypivottable
     'add other fields here
     With .PivotFields("filteringpivotfield")
           .Orientation = xlPageField
           .Position = 1
           .ClearAllFilters
           .EnableMultiplePageItems = True

            For Each p In .PivotItems
                   If p.Name Like "*string*" Then
                         p.Visible = False
                   End If
            Next p
      End With
End With
 
Upvote 0

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

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