【问题标题】:How to make multiple PivotItems visible in Excel VBA如何在 Excel VBA 中使多个 PivotItems 可见
【发布时间】:2018-05-18 20:46:44
【问题描述】:

我在“主要”工作表中有一个数据透视表。在PivotField“报告过滤器”中,我有“国家代码”,其中包含 200 个国家/地区。我想使用 InputBox 从该过滤器中显示超过 1 个国家/地区。

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

我的代码

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       

【问题讨论】:

  • 不确定我是否理解,您是否收到此代码错误?还是您缺少某个功能?
  • 我认为问题在于他在输入框中使用的数据。 Pivot Table 使用来自 excel 而不是手动输入的实际参考值。 @Deepak:您是否尝试过使用 excel 作为输入项?
  • @shai 我没有收到错误。我需要改进这段代码。我需要删除过滤器中的所有项目。
  • @Deepak 你需要保留一个,否则你会得到一个错误,你想保留哪一个?
  • @shai 是的,你是对的。但我一开始不想选择 1。帮帮我

标签: vba excel excel-2010 pivot-table


【解决方案1】:

您可以遍历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

【讨论】:

  • 您的代码几乎可以运行了。 2 小问题。 1) 当我选择 1 个国家/地区时,它不起作用并且仍然选择 ALL。 2,第二次运行程序时,我需要清除过滤器。如何做到这一点
  • @Deepak 我根据你的帖子写了代码,我也会修改第二个选项。
  • @ 现在当我只输入 1 个国家/地区时出现错误。 unable to set the visible property of the pivotitem class
  • 最后pi.Visible = False
  • @Deepak 请再次检查,如果您使用了ClearAllFilters 并且在InputBox 中输入的值作为PivotItems, you should not get this error. This error will appear when trying to hide the last Visible` Pivot-Items 之一存在
【解决方案2】:

为了使您的过滤器正常工作,您需要为您不想看到的枢轴项提供 False Visible 值以及为您所做的那些提供 True 值,以便您已将所有枢轴项设置为 True 或假的。

这里有一些代码可以给你一个想法;

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

【讨论】:

    猜你喜欢
    • 2019-03-30
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多