VBA to Deselect PivotFields Values if it exists

th259

New Member
Joined
Oct 24, 2023
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I have a pivot table called Fiscal53_Data. One PivotFields, the "Check#" is already filtered to show only total check greater than $10K. From the same pivot table, I want to deselect Vendor Names, "Bank of Marin" and "Bank of Mellon" if it exists. I got the following code but the code for deselecting "Bank of Marin" actually makes the pivot table disappeared. How do I fix the code below?
VBA Code:
With ActiveSheet.PivotTables("Fiscal53_Data").PivotFields("Total")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "#,##0.00"                                                               'Format Number
    .Name = "Sum of Total"
'Pivot Table Tabular View
    With ActiveSheet.PivotTables("Fiscal53_Data")
        .InGridDropZones = True
        .RowAxisLayout xlTabularRow
    End With
    ActiveSheet.PivotTables("Fiscal53_Data").PivotFields("Check#").PivotFilters. _
        Add2 Type:=xlValueIsGreaterThan, DataField:=ActiveSheet.PivotTables( _
        "Fiscal53_Data").PivotFields("Sum of Total"), Value1:=10000                          'Filter Check# Column that has a total bigger than $10K
    Columns("D:D").Select
    Selection.Replace What:="(blank)", REPLACEMENT:="Direct Pay", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        End With
    Dim pt As PivotTable, pi As PivotItem
    For Each pt In Data_Pivot.PivotTables
    If pt.Name = "Fiscal53_Data" Then                                                        'check that pivot name exists
        For Each pi In pt.PivotFields("Vendor Name").PivotItems
            If pi.Name = "BANK OF MARIN" Then                          'check that item name exists
                pi.Visible = False: Exit For                                         'Do not show item if it exists
        End If
        Next pi: Exit For
        End If
Next pt
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I tested your code and it works to hide an item.

The only strange thing I noticed is that in this line you have the Data_Pivot object, but I don't see the declaration of the object, it seems strange to me, because in the previous references, it refers to Activesheet, but here it mentions an object.


For Each pt In Data_Pivot.PivotTables

Check if that object is for the same active sheet.
:unsure:
 
Upvote 0
Solution
I got it to work thank you very much! I got the following codes to deselect 3 items if it exists, is there a way to condensed the code? Make the 3 items into an array?

VBA Code:
Dim pt As PivotTable, pi As PivotItem
    For Each pt In ActiveSheet.PivotTables
    If pt.Name = "Fiscal53_Data" Then                                                        'check that pivot name exists
        For Each pi In pt.PivotFields("Vendor Name").PivotItems
            If pi.Name = "HAYWARD UNIFIED SCHOOL DIST" Then                          'check that item name exists
                pi.Visible = False: Exit For                                         'Do not show item if it exists
        End If
        Next pi: Exit For
    End If
        Next pt
    For Each pt In ActiveSheet.PivotTables
    If pt.Name = "Fiscal53_Data" Then                                                        'check that pivot name exists
        For Each pi In pt.PivotFields("Vendor Name").PivotItems
            If pi.Name = "BANK OF MARIN" Then                          'check that item name exists
                pi.Visible = False: Exit For                                         'Do not show item if it exists
        End If
        Next pi: Exit For
        End If
        Next pt
        For Each pt In ActiveSheet.PivotTables
        If pt.Name = "Fiscal53_Data" Then                                                        'check that pivot name exists
        For Each pi In pt.PivotFields("Vendor Name").PivotItems
            If pi.Name = "HAYWARD UNIFIED SCHOOL DIST" Then                          'check that item name exists
                pi.Visible = False: Exit For                                         'Do not show item if it exists
        End If
        Next pi: Exit For
        End If
        Next pt
    For Each pt In ActiveSheet.PivotTables
    If pt.Name = "Fiscal53_Data" Then                                                        'check that pivot name exists
        For Each pi In pt.PivotFields("Vendor Name").PivotItems
            If pi.Name = "BANK OF NEW YORK MELLON" Then                          'check that item name exists
                pi.Visible = False: Exit For                                         'Do not show item if it exists
        End If
        Next pi: Exit For
        End If
        Next pt
 
Upvote 0
Upping this thread!

Is there a way to condense the code above, instead of repeating the same code over again to search for new values to deselect, can I create a array of values to deselect if it exists?

Thank you in advance!
 
Upvote 0
I don't have any data to test this with but see if this works for you.
VBA Code:
    Dim pt As PivotTable, pi As PivotItem
    Dim arrFltr As Variant
    
    arrFltr = Array("HAYWARD UNIFIED SCHOOL DIST", _
                    "BANK OF MARIN", _
                    "HAYWARD UNIFIED SCHOOL DIST", _
                    "BANK OF NEW YORK MELLON")
    
    For Each pt In ActiveSheet.PivotTables
        If pt.Name = "Fiscal53_Data" Then                                                        'check that pivot name exists
            For Each pi In pt.PivotFields("Vendor Name").PivotItems
                If Not IsError(Application.Match(pi.Name, arrFltr, 0)) Then                         'check that item name exists
                    pi.Visible = False                                                     'Do not show item if it exists
            End If
            Next pi: Exit For
        End If
    Next pt
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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