RTE 1004 when filtering pagefield via macro

Henning

New Member
Joined
Jul 19, 2013
Messages
5
Hello,
I am currently trying to make a simple button that filters a PageField in a Pivot table.
Code:
Worksheets("SN VIEW (2)").PivotTables("SN").PivotFields("DELIVERY SEQUENCE").PivotFilters.Add Type:=xlCaptionContains, Value1:="*ONSITE*"
This will give me a
Run-time error '1004':
Application-defined or object-defined error

Ive also tried this:
Code:
Dim PvtTbl As PivotTable
Set PvtTbl = Sheets("SN VIEW (2)").PivotTables("SN")
PvtTbl.PageFields("DELIVERY SEQUENCE").ClearAllFilters
PvtTbl.PageFields("DELIVERY SEQUENCE").EnableMultiplePageItems = True
PvtTbl.PageFields("DELIVERY SEQUENCE").PivotFilters.Add Type:=xlCaptionContains, Value1:="*ONSITE*"

This will run once, remove all the filters and then ignore the last row of code.
If you press it again, it will also give you the same run-time error.

The PageField "DELIVERY SEQUENCE" contains a load of strings, like so:
01. Remote With Parts, 02. Subcontractor Onsite
01. Subcontractor Onsite
01. Remote With Parts, 02. Subcontractor Onsite
01. Subcontractor Onsite
01. Subcontractor Onsite, 02. Subcontractor Onsite
01. Subcontractor Onsite, 02. Subcontractor Onsite
01. Subcontractor Onsite
01. Subcontractor Onsite, 02. Subcontractor Onsite, 03. Subcontractor Onsite
01. Depot(Repair and Return), 02. Subcontractor Onsite


I now want to get it to ON_Click "select multiple selections that all contain "Onsite"".

When I try to record that with the macro recorder, it simply ignores my selection of "Select multiple items" and then subsequent actual selection of multiple items.

Does anyone know what is missing, so everything is defined?

thanks in advance,
Henning
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You can't use PivotFilters with a PageField, manually or in VBA.

Hello Andrew, thank you for your quick response. Do you maybe know of a workaround? Or is there simply no way?

Because I know you can set pagefields to single values directly:
Code:
Sheets("SN VIEW (2)").PivotTables("SN").PivotFields("DI ONSITE (>2) ALL").CurrentPage = "1"

This works, but only for one value.
 
Upvote 0
I got it. So amazing how much you can code without knowing how to, just buy knowing what to google.
Thanks Andrew.

Below the code to set pagefields to multiple selections that contain a certain string:
Code:
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set pt = Sheets("SN VIEW (2)").PivotTables("SN")
Set pf = pt.PivotFields("DELIVERY SEQUENCE")
    pt.ManualUpdate = True
    Application.ScreenUpdating = False
    With pf
        On Error Resume Next
       
    
        For Each pi In pf.PivotItems
             pi.Visible = False
    Next pi
        
        For Each pi In pf.PivotItems
            If pi.Value Like "*Onsite*" Then
                pi.Visible = True
            Else: pi.Value = False
            End If
        Next pi
       
    End With
  pt.ManualUpdate = False
Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

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