【发布时间】:2018-07-20 12:17:16
【问题描述】:
我有一张 Excel 表格,其中包含人员及其相关部门的列表。在另一个工作表上的数据透视表中,我想过滤我的结果,以便显示给定部门中任何人“分配给”的所有项目。
到目前为止,我的代码可以将人员列表过滤到所需部门,并创建一个包含所有这些人员姓名的数组。然后,我尝试过滤包含这些名称列表的 PivotItems,使其可见,而所有其他名称都隐藏,但是当我尝试运行宏时,它只是在不断思考。有没有更简单的方法来做到这一点?
ActiveSheet.Range("$A$1:$E$175").AutoFilter Field:=4, Criteria1:= _
"DEPARTMENT NAME"
'Selects first visible row of filtered data set & _
create array that contains all filtered names
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(3, 2).Select
employeerange = "C" & ActiveCell.Row & ":C" & ActiveSheet.Rows.Count
Dim employeearray As Variant
employeearray = Range(employeerange).Value
'Cycle through all possible items for the given Pivot Field and compare to _
each of the names in the employee array. Set items that match to visible _
and all others to hidden.
Dim PI As PivotItem
Dim element As Variant
With ActiveSheet.PivotTables("PivotTable2").PivotFields("PIVOT FIELD")
For Each PI In .PivotItems
For Each element In employeearray
If PI Like "*" & CStr(element) & "*" Then
PI.Visible = True
Else
PI.Visible = False
End If
Next element
Next PI
End With
【问题讨论】:
标签: excel vba foreach pivot-table