vbscript 过滤同一工作表上的所有数据透视表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript 过滤同一工作表上的所有数据透视表相关的知识,希望对你有一定的参考价值。

Private Sub CommandButton1_Click()

Dim month_a, month_b, month_c, j As Integer
Dim currentm, defaultm As Long
Dim pvt As PivotTable
Dim coll As Collection
Dim PivotRefs, i As Variant

Application.ScreenUpdating = False

On Error Resume Next

'Identify all PivotTables within active worksheet and transform into an array

Set coll = New Collection

For Each pvt In ActiveSheet.PivotTables

coll.Add pvt.Name

Next

ReDim PivotRefs(0 To coll.Count - 1)

For j = 1 To coll.Count
        PivotRefs(j - 1) = coll.Item(j)
Next

'Start collecting data on what you wish to filter

currentm = Application.InputBox(prompt:="I'd like to check whether you're a robot. How many months have this project been running since January 2014?", Type:=1)

month_a = Application.InputBox(prompt:="Enter the first month you'd like to filter by", Type:=1)

month_b = Application.InputBox(prompt:="Enter the second month you'd like to filter by (Click 'cancel' if none)", Type:=1)

month_c = Application.InputBox(prompt:="Enter the third month you'd like to filter by (Click 'cancel' if none)", Type:=1)

defaultm = 1
 
'Uncheck every filter
 
For i = 0 To coll.Count - 1
    For defaultm = 1 To currentm
        ActiveSheet.PivotTables(PivotRefs(i)).PivotFields("MONTH").PivotItems(defaultm).Visible = False
    Next defaultm
Next i

'Debugging Area

For i = 0 To coll.Count - 1
    Debug.Print PivotRefs(i)
Next i

Debug.Print coll.Count

'Check required filter
For i = 0 To coll.Count - 1
    With ActiveSheet.PivotTables(PivotRefs(i)).PivotFields("MONTH")
        .PivotItems(month_a).Visible = True
        .PivotItems(month_b).Visible = True
        .PivotItems(month_c).Visible = True
    End With
Next i
End Sub

以上是关于vbscript 过滤同一工作表上的所有数据透视表的主要内容,如果未能解决你的问题,请参考以下文章

如何在数据透视表上应用过滤器?

基于同一表上的另一个查询过滤记录的 SQL

同步数据透视表 - VBA 代码不起作用

Excel 2013 - 在基于 OLAP 的数据透视表上更改页面过滤器时记录的宏错误

使用表格从数据透视表中过滤数据

数据透视表上的t-sql辅助数据透视表