如果我编写VBA以自动过滤Excel中的数据透视表,似乎我必须将每条记录指定为visible = True或visible = false。有没有办法只指定我要检查的内容?
例如:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("COB_TITLE")
.PivotItems("CMD GRP").Visible = True
.PivotItems("G1").Visible = True
.PivotItems("G2").Visible = True
.PivotItems("G3/G5/G3 FIRES/REE/G7/HAST/G9").Visible = False
.PivotItems("G4").Visible = False
.PivotItems("G6").Visible = False
.PivotItems("G8").Visible = False
.PivotItems("DIV ENG").Visible = False
End With
而不是
function VisibleList(strSLIDE)
我确信我曾经录制过一个只有我选择的宏。
所以我写了这个字符串.PivotItems("HHBN").Visible = False
.PivotItems("CMD GRP").Visible = False
.PivotItems("G1").Visible = True
.PivotItems("G2").Visible = True
.PivotItems("G3/G5/G3 FIRES/REE/G7/HAST/G9").Visible = True
.PivotItems("DIV ARTY").Visible = False
.PivotItems("G3 FIRES").Visible = False
.PivotItems("G4").Visible = True
,它产生一个这样的列表:
strPivotItems = VisibleList("DIV STAFF")
With ActiveSheet.PivotTables("PivotTable1").PivotFields("COB_TITLE")
strPivotItems
End With
我希望我可以这样嵌入它:
select e.name, e.deptid, count(*) as numreports
from employee e join
reports r
on e.id = r.employeeid
group by e.name, e.deptid;
我假设我可以像嵌入SQL那样做。有没有办法为VBA生成这个?
答案 0 :(得分:0)
不确定这是什么意思,但请在下面给出代码:
Option Explicit
Sub FilterPivotItems()
Dim PvtTbl As PivotTable
Dim PvtFld As PivotField
Dim PvtItm As PivotItem
' set Pivot Table
Set PvtTbl = ActiveSheet.PivotTables("PivotTable1")
' set Pivot Field to "COB_TITLE"
Set PvtFld = PvtTbl.PivotFields("COB_TITLE")
' loop through all Pivot Items in "COB_TITLE" Pivot Field
For Each PvtItm In PvtFld.PivotItems
' check what is the Pivot item Name
Select Case PvtItm.Name
Case "CMD GRP", "G1", "G2" ' <-- visible Filter Items list
PvtItm.Visible = True
Case Else
PvtItm.Visible = False
End Select
Next PvtItm
End Sub