A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi,
scenario
the pivot tbl exists
(in one sheet, source data range and Piv.tbl)
using autofilter in data range
result pic-1
after run the below code
result pic-2
vba macro
Sub PivTbl_Hide_Items()
Dim ws1 As Worksheet
Set ws1 = Sheets("Sheet1") ' sht name
Dim rng As Range, rFind As Range, rngField As Range, r As Range
Dim row1, nRow, nCol
Set rng = ws1.Range("A1:H19") '<< data range
row1 = rng.Item(1).Row
nRow = rng.Rows.Count
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ws1.PivotTables(1)
Dim x, i
Dim fg As Boolean
For Each pf In pt.PivotFields
pf.ClearAllFilters
Next
With pt
.PivotCache.MissingItemsLimit = xlMissingItemsNone
.PivotCache.Refresh
End With
Application.ScreenUpdating = False
i = 1
For Each pf In pt.PivotFields
If ws1.AutoFilter.Filters(i).On Then
Set rFind = rng.Item(1).Resize(, nCol).Find(pf.Name)
Set rngField = ws1.Cells(row1 + 1, rFind.Column).Resize(nRow - 1)
For x = 2 To pf.PivotItems.Count
pf.PivotItems(x).Visible = False
Next x
fg = False
For Each r In rngField.SpecialCells(xlCellTypeVisible)
If pf.PivotItems(1).Value = r.Value Then
fg = True
End If
pf.PivotItems(r.Value).Visible = True
Next r
If fg = False Then pf.PivotItems(1).Visible = False
End If
i = i + 1
Next pf
Application.ScreenUpdating = True
End Sub