如何在 VBA 中对我的数据透视图应用过滤器
How to apply a filter to my pivot chart in VBA
我正在尝试编写一个宏来创建数据透视图。我需要将数据透视图作为条形图,将列过滤为仅显示 3 列。这是我目前的代码。
'Define Data Range
LastRow = Dsheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Dsheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = Dsheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange)
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Studies Impacted")
'Insert Column Fields
With ActiveSheet.PivotTables("Studies Impacted").PivotFields("Study")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Row Fields
With ActiveSheet.PivotTables("Studies Impacted").PivotFields("Workstream")
.Orientation = xlRowField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("Studies Impacted").PivotFields("Study")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Status Count"
End With
'Format Pivot
ActiveSheet.PivotTables("Studies Impacted").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Studies Impacted").TableStyle2 = "PivotStyleMedium9"
我试过使用 >add FilterType 但它似乎不起作用。我也尝试将其定义为 PivotField,但同样没有结果。
我想要的结果是一个条形图,每个工作流只显示 3 个研究。相反,我得到了所有的研究。
我假设你的数据透视图和 table 是耦合的。
您最多可以使用
方法过滤两项
pvtField.ActiveFilters.Add2
(excel 2013 年或以后)
或
pvtField.ActiveFilters.Add
(早于 excel 2013 年)
你会运行这样的代码
Sub main()
Dim pvtTable As PivotTable
Dim pvtFields As PivotFields
Dim pvtField As PivotField
Dim ws As Worksheet
Set ws = ActiveSheet
Set pvtTable = ws.PivotTables(1)
pvtTable.AllowMultipleFilters = True
Set pvtFields = pvtTable.PivotFields
Set pvtField = pvtFields.Item(1)
pvtField.PivotFilters.Add2 xlCaptionEquals, Value1:="A", Value2:="B"
End Sub
否则您必须遍历您正在过滤的行的数据透视项
代码:
Option Explicit
Sub main()
Dim ws As Worksheet
Dim pvtTable As PivotTable
Dim pvtItems As PivotItems
Dim pvtItem As PivotItem
Dim Index As Long
Set ws = ActiveSheet
Set pvtTable = ws.PivotTables(1)
Set pvtItems = pvtTable.PivotFields(1).PivotItems
For Index = 1 To pvtItems.Count
Set pvtItem = pvtItems.Item(Index)
If pvtItem.Value = "A" Or pvtItem.Value = "B" Then
pvtItem.Visible = False
End If
Next Index
End Sub
我正在尝试编写一个宏来创建数据透视图。我需要将数据透视图作为条形图,将列过滤为仅显示 3 列。这是我目前的代码。
'Define Data Range
LastRow = Dsheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Dsheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = Dsheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange)
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Studies Impacted")
'Insert Column Fields
With ActiveSheet.PivotTables("Studies Impacted").PivotFields("Study")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Row Fields
With ActiveSheet.PivotTables("Studies Impacted").PivotFields("Workstream")
.Orientation = xlRowField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("Studies Impacted").PivotFields("Study")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Status Count"
End With
'Format Pivot
ActiveSheet.PivotTables("Studies Impacted").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Studies Impacted").TableStyle2 = "PivotStyleMedium9"
我试过使用 >add FilterType 但它似乎不起作用。我也尝试将其定义为 PivotField,但同样没有结果。
我想要的结果是一个条形图,每个工作流只显示 3 个研究。相反,我得到了所有的研究。
我假设你的数据透视图和 table 是耦合的。
您最多可以使用
方法过滤两项pvtField.ActiveFilters.Add2
(excel 2013 年或以后) 或
pvtField.ActiveFilters.Add
(早于 excel 2013 年)
你会运行这样的代码
Sub main()
Dim pvtTable As PivotTable
Dim pvtFields As PivotFields
Dim pvtField As PivotField
Dim ws As Worksheet
Set ws = ActiveSheet
Set pvtTable = ws.PivotTables(1)
pvtTable.AllowMultipleFilters = True
Set pvtFields = pvtTable.PivotFields
Set pvtField = pvtFields.Item(1)
pvtField.PivotFilters.Add2 xlCaptionEquals, Value1:="A", Value2:="B"
End Sub
否则您必须遍历您正在过滤的行的数据透视项
代码:
Option Explicit
Sub main()
Dim ws As Worksheet
Dim pvtTable As PivotTable
Dim pvtItems As PivotItems
Dim pvtItem As PivotItem
Dim Index As Long
Set ws = ActiveSheet
Set pvtTable = ws.PivotTables(1)
Set pvtItems = pvtTable.PivotFields(1).PivotItems
For Index = 1 To pvtItems.Count
Set pvtItem = pvtItems.Item(Index)
If pvtItem.Value = "A" Or pvtItem.Value = "B" Then
pvtItem.Visible = False
End If
Next Index
End Sub