使枢轴项在固定范围内可见

Making pivot items visible from a fixed range

我在 Sheet "Code" 中有一个 table。在 C 列中,我有一些国家/地区代码,例如 DE、FR、GB...我只想 select 所有值并使其在主 [=16] 中可用的枢轴 table 中可见=].我使用了下面的代码但没有工作并且没有显示错误。帮我改一下代码。

Sub pivot()
Dim wst,ws As Worksheet
Dim arr1() As String
Dim j As Long

Set wst = Sheets("Code")
LastCol = wst.Cells(wst.Rows.Count, 3).End(xlUp).Row
ReDim Preserve arr1(1 To LastCol)
  For j = 1 To LastCol
     arr1(j) = wst.Cells(j, 3).Value
Next j

Set ws = Worksheets("Main")    
 ws.PivotTables("MainTable").PivotFields("Country Code").ClearAllFilters
    With ws.PivotTables("MainTable").PivotFields("Country Code")
      For Each pi In .PivotItems
        pi.Visible = InStr(1, arr1, pi.Name) > 0
      Next
End With

End Sub

Shai 在这方面先发制人。我假设 Set ws = Sheets("Main")

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