在这件事上,我赢了。我假设设置ws=图纸(“主”)
Option Explicit
Sub pivot()
Dim wst As Worksheet
Dim arr1() As String
Dim j As Long
Dim lastRow As Long
Dim ws As Worksheet
Set ws = Sheets("Main")
Set wst = Sheets("Code")
lastRow = wst.Cells(wst.Rows.Count, 3).End(xlUp).Row
ReDim Preserve arr1(1 To lastRow)
For j = 1 To lastRow
arr1(j) = wst.Cells(j, 3).Value
Next j
Worksheets("Main").PivotTables("MainTable").PivotFields("Country Code").ClearAllFilters
Dim Pi As PivotItem
Dim pvt As PivotTable
Set pvt = ws.PivotTables("MainTable")
With pvt.PivotFields("Country Code")
For Each Pi In .PivotItems
If IsError(Application.Match(Pi.Name, arr1, 0)) Then Pi.Visible = False
Next Pi
End With
End Sub