macro to refresh pivot table and reapply filter

JoaoGabriel

New Member
Joined
Jun 6, 2021
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
How do I make a macro to update the pivot table and keep the filter selecting all values except #N/D as in the images below? Thanks in advance

1706186154405.png


I inserted a new row and updated the pivot table, but the value 20 should be selected in the filter
1706186122971.png
 

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.
This should get you started, you can add a button to the sheet or call it on open.

VBA Code:
Sub UpdatePivotTable()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem

    ' Set the worksheet with your pivot table
    Set ws = Worksheets("YourSheetName")

    ' Set the pivot table
    Set pt = ws.PivotTables("YourPivotTableName")

    ' Set the pivot field with the filter
    Set pf = pt.PivotFields("YourFilterFieldName")

    ' Clear all filters first
    pf.ClearAllFilters

    ' Loop through pivot items and exclude #N/D
    For Each pi In pf.PivotItems
        If pi.Name <> "#N/D" Then
            pi.Visible = True
        Else
            pi.Visible = False
        End If
    Next pi

    ' Update the pivot table

    pt.RefreshTable
End Sub

t0ny84
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,453
Members
452,514
Latest member
cjkelly15

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