【问题标题】:Filter items with certain text in a Pivot Table using VBA使用 VBA 在数据透视表中过滤具有特定文本的项目
【发布时间】:2017-03-21 14:08:55
【问题描述】:

我一直在尝试构建一个代码来过滤数据透视表中包含特定文本片段的所有项目。我最初认为我可以使用星号 (*) 来表示文本之前或之后的任何字符串,但 VBA 将其读取为字符。这是在用户窗体列表框中显示数据透视表数组所必需的。看看我尝试了什么:

Sub FilterCstomers()

    Dim f As String: f = InputBox("Type the text you want to filter:")

    With Sheets("Customers Pivot").PivotTables("Customers_PivotTable")
        .ClearAllFilters
        .PivotFields("Concatenation for filtering").CurrentPage = "*f*"
        End With

End Sub

【问题讨论】:

    标签: vba excel filter pivot-table


    【解决方案1】:

    尝试使用下面的代码过滤“过滤连接”字段中的所有项目,这些项目是Like 通配符* 和从InputBox 收到的字符串f

    Option Explicit
    
    Sub FilterCstomers()
    
        Dim PvtTbl      As PivotTable
        Dim PvtItm      As PivotItem
        Dim f           As String
    
        f = InputBox("Type the text you want to filter:")
    
        ' set the pivot table
        Set PvtTbl = Sheets("Customers Pivot").PivotTables("Customers_PivotTable")
    
        With PvtTbl.PivotFields("Concatenation for filtering")
            .ClearAllFilters
    
            For Each PvtItm In .PivotItems
                If PvtItm.Name Like "*" & f & "*" Then
                    PvtItm.Visible = True
                Else
                    PvtItm.Visible = False
                End If
            Next PvtItm
        End With
    
    End Sub
    

    【讨论】:

    • 效果很好!但是,完成需要很长时间!有没有办法在过滤器搜索字段中输入文本并选择“(全部)”项?
    • .ClearAllFilters 实际上是这样做的
    • 不是为了清除过滤器,而是对多个项目实际应用过滤器,而不是一一分析!?
    【解决方案2】:

    为什么不只是:

    .PivotFields("PivotFieldName").PivotFilters.Add2 Type:=xlCaptionContains, Value1:="X"

    【讨论】:

      【解决方案3】:

      您可以通过以下方式调整 Shai 的答案以显着加快速度:

      1. 删除 IF 的 TRUE 分支,因为它不需要
      2. 在代码执行时将 ManualUpdate 设置为 TRUE,以停止 每次更改可见时重新计算数据透视表 任何 PivotItem 的状态。
      3. 关闭屏幕更新和计算(如果有 工作簿中的 volatile 函数),直到你完成

        如果您希望比较不区分大小写,您可能还想在其中放置一个 Option CompareText。

      如果用户键入数据透视表中不存在的内容,您可能需要一些错误处理。

      您可能希望阅读我的blogpost on this stuff,因为数据透视表的过滤速度非常慢,并且它讨论了许多加快处理速度的方法

      这里是 Shai 代码的修改示例:

      Option Explicit
      Option Compare Text
      
      Sub FilterCstomers()
      
          Dim pt  As PivotTable
          Dim pf  As PivotField
          Dim pi  As PivotItem
          Dim f   As String
      
          f = InputBox("Type the text you want to filter:")
          With Application
              .ScreenUpdating = False
              .Calculation = xlCalculationManual
          End With
      
          Set pt = Sheets("Customers Pivot").PivotTables("Customers_PivotTable")
          Set pf = pt.PivotFields("Concatenation for filtering")
          pt.ManualUpdate = True
          With pf
              .ClearAllFilters
              On Error GoTo ErrHandler
              For Each pi In .PivotItems
                  If Not pi.Name Like "*" & f & "*" Then
                      pi.Visible = False
                  End If
              Next pi
      
          End With
      ErrHandler:
              If Err.Number <> 0 Then pf.ClearAllFilters
              pt.ManualUpdate = False
              On Error GoTo 0
              With Application
                  .ScreenUpdating = True
                  .Calculation = xlCalculationAutomatic
              End With
          End Sub
      

      【讨论】:

        猜你喜欢
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-19
        • 2021-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多