【问题标题】:How to filter a filter in a pivot table如何过滤数据透视表中的过滤器
【发布时间】:2019-05-17 14:36:45
【问题描述】:

我正在尝试使用 VBA 在 Excel 数据透视表中过滤过滤器,但该过程花费了大量时间。用户在文本框中键入并单击提交按钮以开始操作。我的过滤器有超过 2.000 个值。这是我在这种情况下使用的代码。

是否存在最快的过滤方式?

Sub a()
Dim txt1 As String

txt1 = Worksheets("Planilha1").TextBox1.Value

If Not txt1 = "" Then
    Set ws = Sheets("Planilha1")
    Set pt = ws.PivotTables(1)
    Set pf = pt.PageFields(1)

    For Each Pi In pf.PivotItems
      If Not Pi = txt1 Then
        pf.PivotItems(CStr(Pi)).Visible = False
      End If
    Next Pi
End If
End Sub

【问题讨论】:

  • 你有没有试过 Application.ScreenUpdating = False 在循环之前和 Application.ScreenUpdating = True 在循环之后?也许是导致问题的渲染
  • 不,我没有尝试。但是我看到这个问题是关于它的(渲染)。你的答案和下面的答案对我有用!谢谢你:)

标签: excel vba filter pivot-table


【解决方案1】:

这明显更快:

Sub a()
    Dim txt1 As String, ws As Worksheet, pt As PivotTable, pf As PivotField, pi As PivotItem
    Dim t


    txt1 = "Var_1099"

    If Not txt1 = "" Then
        Set ws = ActiveSheet
        Set pt = ws.PivotTables(1)
        Set pf = pt.PivotFields("Col1")

        t = Timer

        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        pt.ManualUpdate = True

        For Each pi In pf.PivotItems
            pi.Visible = (pi = txt1)
        Next pi

        Application.Calculation = xlCalculationAutomatic
        pt.ManualUpdate = False
        pt.Update

        Debug.Print Timer - t
    End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2023-03-13
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多