如何在Excel VBA中显示多个PivotItem

时间:2017-12-05 08:47:01

标签: excel vba excel-vba excel-2010 pivot-table

我在表单“Main”中有一个Pivot表。在PivotField“报告过滤器”我有“国家/地区代码”,其中包含200个国家/地区。我想使用InputBox从该过滤器中看到多个国家/地区。

问题是我需要手动选择最小的一个国家或全部过滤器并运行此程序。这样做我无法获得正确的数据。 我需要取消选择所有国家,然后我需要运行。

我的代码

Sub Addcountries()

Dim ws As Worksheet
Dim str1 As Variant
Dim Data As Variant
Dim pf As PivotField
Dim target As PivotTable

Set ws = Sheets("Main")
str1 = Application.InputBox("Enter the Country - comma separated")

If str1 = False Then
    MsgBox "Please Enter one Country", , "Filter Country"
    Exit Sub
Else
    If InStr(1, str1, ",") > 0 Then
        Data = Split(str1, ",")
        For i = LBound(Data) To UBound(Data)
            ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(Data(i)).Visible = True
        Next i
    Else
        ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(str1).Visible = True
    End If
End If

End Sub       

2 个答案:

答案 0 :(得分:2)

您可以循环浏览PivotItems集合,并检查每个PivotItem.Name是否与Data数组中选定的某个数字相匹配 - 您可以使用Match完成此操作功能

<强>代码

If str1 = False Then
    MsgBox "Please Enter one Country", , "Filter Country"
    Exit Sub
Else
    If InStr(1, str1, ",") > 0 Then ' more than 1 country >> create array
        Data = Split(str1, ",")
    Else ' single country
        Data = Array(str1) '<-- create array with 1 element (for Match to work)
    End If

    ' === You need a different loop, loop through all Pivot-Items, then look for a match with Data (array) ===
    Dim pi As PivotItem

    ' clear previous Filter
    ws.PivotTables("MainTable").PivotFields("Country Code").ClearAllFilters

    For Each pi In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems
        ' check if current Pivot-Item equals one of the elements of the array (use Match function)
        If Not IsError(Application.Match(pi.Name, Data, 0)) Then ' Match successful
            pi.Visible = True
        Else
            pi.Visible = False
        End If
    Next pi
End If

' rest of your code

答案 1 :(得分:0)

为了使您的过滤器能够正常工作,您需要为您不想看到的Pivot项目提供虚假可见值以及为您所做的设置提供真值。枢轴项目是对还是错。

这是一些给你一个想法的代码;

Sub Addcountries()
    Dim ws As Worksheet
    Dim str1 As Variant
    Dim Data() As Variant
    Dim pf As PivotField
    Dim target As PivotTable
    Dim PivotItem As Object
    Dim ShowMe As Boolean


    Set ws = Sheets("Main")
    str1 = Application.InputBox("Enter the Country - comma separated")

    If str1 = False Then
        MsgBox "Please Enter one Country", , "Filter Country"
        Exit Sub
    Else
        If InStr(1, str1, ",") > 0 Then
            Data = Split(str1, ",")
        Else
            'Make Single Item Array
            ReDim Data(1)
            Data(0) = str1
        End If
        For Each PivotItem In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems
            'Default Visibility Is False
            ShowMe = False
            For i = LBound(Data) To UBound(Data)
                'Loop Through Each Item In Data To See If You Should ShowMe
                ShowMe = (PivotItem.Name = Data(i))
                If ShowMe Then
                    'Quit Early If You ShowMe
                    Exit For
                End If
            Next i
            PivotItem.Visible = ShowMe
        Next PivotItem
    End If

End Sub