选择与另一个Excel工作表中的项目列表匹配的所有数据透视项目 [英] Select all Pivot Items that match list of items in another Excel sheet

查看:376
本文介绍了选择与另一个Excel工作表中的项目列表匹配的所有数据透视项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel工作表,其中包含人员及其相关部门的列表.在另一个工作表上的数据透视表中,我想过滤结果,以便显示分配给"给定部门中任何人员的所有项目.

I have one Excel sheet that contains a list of people and their associated departments. In a PivotTable on another sheet, I would like to filter my results so that all items "Assigned To" any of the people in a given department will be displayed.

到目前为止,我有代码可以将人员列表过滤到所需部门,并创建一个包含所有人员姓名的数组.然后,我尝试过滤包含这些名称列表的PivotItem,这些列表是可见的,而所有其他列表则是隐藏的,但是当我尝试运行宏时,它只是在不断思考.有没有更简单的方法可以做到这一点?

So far, I have code that will filter the list of people to the desired department, and will create an array that contains the names of all of those people. I have then tried to filter the PivotItems that contain these lists of names to be visible and all others to be hidden, but when I try to run the macro it is just continuously thinking. Is there an easier way to do this?

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

推荐答案

在PivotItems上进行迭代时,您要避免几个瓶颈和陷阱.请参阅我的帖子,网址为 http://dailydoseofexcel .com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/,以了解更多信息.

When iterating over PivotItems, there's a couple of bottlenecks and gotchas that you want to avoid. See my post at http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/ for more on this.

除其他事项外,您希望在执行迭代时将数据透视表的ManualUpdate属性设置为TRUE,然后在完成时将其设置为FALSE.否则,每次您更改数据透视表的可见性时,Excel都会尝试更新数据透视表.而且您还想确保至少有一个项目始终保持可见状态.我使用的是这样的:

Among other things, you want to set the PivotTable's ManualUpdate property to TRUE while you do the iteration and then back to FALSE when you're done. Otherwise Excel will try to update the PivotTable each time you change the visibility of a PivotItem. And you also want to ensure that at least one item remains visible at all times. I use something like this:

Option Explicit

Sub FilterPivot()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim i As Long
Dim vItem As Variant
Dim vCountries As Variant

Set pt = ActiveSheet.PivotTables("PivotTable1")
Set pf = pt.PivotFields("CountryName")

vCountries = Array("FRANCE", "BELGIUM", "LUXEMBOURG")

pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed

With pf

    'At least one item must remain visible in the PivotTable at all times, so make the first
    'item visible, and at the end of the routine, check if it actually  *should* be visible        
    .PivotItems(1).Visible = True

    'Hide any other items that aren't already hidden.
    'Note that it is far quicker to check the status than to change it.
    ' So only hide each item if it isn't already hidden
    For i = 2 To .PivotItems.Count
        If .PivotItems(i).Visible Then .PivotItems(i).Visible = False
    Next i

    'Make the PivotItems of interest visible
    On Error Resume Next 'In case one of the items isn't found
    For Each vItem In vCountries
        .PivotItems(vItem).Visible = True
    Next vItem
    On Error GoTo 0

    'Hide the first PivotItem, unless it is one of the countries of interest
    On Error Resume Next
    If InStr(UCase(Join(vCountries, "|")), UCase(.PivotItems(1))) = 0 Then .PivotItems(1).Visible = False
    If Err.Number <> 0 Then
        .ClearAllFilters
        MsgBox Title:="No Items Found", Prompt:="None of the desired items was found in the Pivot, so I have cleared the filter"
    End If
    On Error GoTo 0

End With

pt.ManualUpdate = False

End Sub

这篇关于选择与另一个Excel工作表中的项目列表匹配的所有数据透视项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆