Skip variable if not found

ahuang3433

New Member
Joined
Jan 24, 2017
Messages
39
Hi,

I have this pivot table where if designates to filter out specific things. Is there a way to re-write this to where it skips the variable when it's not found?

I got an error to the 2nd to last variable because that is no longer an option.

VBA Code:
ActiveSheet.PivotTables("PivotTable2").PivotFields("Item Availability"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Item Availability")
        .PivotItems("ETA On Time & Qty Ordered Less than Demand").Visible = False
        .PivotItems("In Stock").Visible = False
        .PivotItems("No Gating PO").Visible = False
        .PivotItems("ETA Late").Visible = False
        .PivotItems("ETA On Time").Visible = False
        .PivotItems("ETA Late & Qty Ordered Less than Demand").Visible = False
        .PivotItems("Partial qty in INV. Balance No Gating PO").Visible = False
    End With
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The following macro checks if the variable exists. If it exists then it hides the data. If it does not exist then the variable skips.
But also check, if all the variables are in the pivot table then send a message, since you cannot hide all the data.

For example if in your pivot table in all the items you have "In Stock", then all the rows should be hidden; and that is not possible.
It may not be a real case, but if it ever occurs, the macro already contemplates it.

Try this:

VBA Code:
Sub FilterPT()
  Dim pt      As PivotTable
  Dim pItm    As PivotItem
  Dim aItm    As Variant
  Dim arr     As Variant
  Dim n       As Long
  
  Set pt = ActiveSheet.PivotTables("PivotTable2")
  
  arr = Array("ETA On Time & Qty Ordered Less than Demand", "In Stock", "No Gating PO", _
        "ETA Late", "ETA On Time", "ETA Late & Qty Ordered Less than Demand", _
        "Partial qty in INV. Balance No Gating PO")

  With pt.PivotFields("Item Availability")
    .ClearAllFilters
    pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
    pt.PivotCache.Refresh
    n = 0
    For Each pItm In .PivotItems
      If Not IsError(Application.Match(pItm, arr, 0)) Then
        n = n + 1
        If n < .PivotItems.Count Then
          pItm.Visible = False
        Else
          MsgBox "You are trying to hide all items"
          .ClearAllFilters
        End If
      End If
    Next
  End With
  pt.PivotCache.MissingItemsLimit = xlMissingItemsDefault
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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