Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 基于两个单元格值筛选数据透视表_Excel_Vba_Pivot Table - Fatal编程技术网

Excel 基于两个单元格值筛选数据透视表

Excel 基于两个单元格值筛选数据透视表,excel,vba,pivot-table,Excel,Vba,Pivot Table,我需要根据两个不同单元格中的值筛选数据透视表。 每个单元格将过滤数据透视表中的不同字段 我知道如何根据一个单元格中的值筛选数据透视表。 但是,我不知道如何添加第二个过滤器 Private Sub Worksheet_Change(ByVal Target As Range) Dim xptable As PivotTable Dim xpfile As PivotField Dim xstr As String On Error Resume Next I

我需要根据两个不同单元格中的值筛选数据透视表。
每个单元格将过滤数据透视表中的不同字段

我知道如何根据一个单元格中的值筛选数据透视表。
但是,我不知道如何添加第二个过滤器

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xptable As PivotTable
    Dim xpfile As PivotField
    Dim xstr As String
    On Error Resume Next
    If Intersect(target, Range("H6:H7")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Set xptable = Worksheets("Sheet11").PivotTables("PivotTable2")
    Set xpfile = xptable.PivotFields("FilterField")
    xstr = target.Text
    xpfile.ClearAllFilters
    xpfile.CurrentPage = xstr
    Application.ScreenUpdating = True
End Sub
数据透视表(
Dim xptable As PivotTable
)可以通过其工作表上的名称或编号来寻址,例如。g

Set xptable = Worksheets("Given Name of Sheet3").PivotTables("PivotTable3")
Set xptable = Worksheets("Given Name of Sheet3").PivotTables(1)
Set xptable = Sheet3.PivotTables(1) ' or just use codename of Sheet3
您的筛选字段(
作为数据透视字段的文件
)可以作为
数据透视字段
页面字段
进行寻址。这几乎是一样的,但为了更好地识别,我建议使用后者。Eg

Set xpfile = xptable.PivotFields("MyFirstFilterFieldName")
Set xpfile = xptable.PageFields("MyFirstFilterFieldName")
Set xpfile = xptable.PageFields(1)
要筛选单个条目,请将页面字段的
CurrentPage
设置为有效的
数据透视项的标题。您必须通过在其所有
数据透视项上循环来确保标题存在(否则您将得到一个错误)


如果要通过对特定单元格的每次更改来自动执行此操作,请将以下代码放置在该工作表的代码模块中,其中单元格输入为(而不是数据透视表)

Target
表示单元格范围,其中发生了更改。由于它可能要大得多(例如,有人从剪贴板复制了一个大范围到工作表),如果单元格发生了更改,则必须通过
Intersect
进行检查,然后使用单个单元格的值

在工作表的代码模块中,您可以通过
Me
引用相应的工作表

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xptable As PivotTable
    Dim xpfile As PivotField
    Dim xpitem As PivotItem

    Set xptable = Sheet3.PivotTables("PivotTable3")

    If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
        Set xpfile = xptable.PageFields(1)
        With xpfile
            .ClearAllFilters
            For Each xpitem In .PivotItems
                If xpitem.Caption = Me.Range("A1").Value Then
                    .CurrentPage = Me.Range("A1").Value
                    Exit For
                End If
            Next xpitem
        End With

    ElseIf Not Intersect(Target, Me.Range("A2")) Is Nothing Then
        ' like above
    End If
End Sub

如果您需要一个包含多个项目的筛选器,那么您必须
启用MultiplePageItems
并隐藏所有不需要的数据透视项-但您必须确保始终至少有一个数据透视项保持可见,例如。g

Dim AtLeastOneVisible As Boolean
With myPivotTable.PageFields(1)
    .ClearAllFilters
    .EnableMultiplePageItems = True

    AtLeastOneVisible = False
    For Each myPivotItem In .PivotItems
        If myPivotItem .Caption = ... Or myPivotItem .Caption = ... Then
            AtLeastOneVisible = True
            Exit For
        End If
    Next myPivotItem

    If AtLeastOneVisible Then
        For Each myPivotItem In .PivotItems
            If myPivotItem .Caption <> ... And myPivotItem .Caption <> ... Then
                pi.Visible = False
            End If
        Next myPivotItem 
    End If
End With
Dim至少显示为布尔值
使用myPivotTable.PageFields(1)
.ClearAllFilters
.EnableMultiplePageItems=True
AtLeastOneVisible=False
对于.PivotItems中的每个myPivotItem
如果myPivotItem.Caption=。。。或myPivotItem。标题=。。。然后
AtLeastOneVisible=True
退出
如果结束
下一个myPivotItem
如果至少有一个是可见的,那么
对于.PivotItems中的每个myPivotItem
如果myPivotItem.Caption。。。和myPivotItem。标题。。。然后
pi.Visible=False
如果结束
下一个myPivotItem
如果结束
以

pivot表上方是否已有2个不同的过滤器?我有。我只是不想用过滤器。我需要表格在值插入单元格时自动更新感谢您花时间编写代码。我真的很感激。这就是我到目前为止所拥有的:Application.ScreenUpdating=trueOK我只有最后一个问题。在我的第一个查询中,我使用xstr=target.Text。在第二个示例中,我设置了xstr=Range(“H6”)xstr2=Range(“H7”)。这两个单元格将输入数据。它基本上会做同样的事情吗。顺便说一句,这对我来说是全新的,所以我不得不问很多问题。好吧,我把你的回答标记为已接受,并删除了我的其他评论。谢谢你的帮助我能再问你一个问题吗?
Dim AtLeastOneVisible As Boolean
With myPivotTable.PageFields(1)
    .ClearAllFilters
    .EnableMultiplePageItems = True

    AtLeastOneVisible = False
    For Each myPivotItem In .PivotItems
        If myPivotItem .Caption = ... Or myPivotItem .Caption = ... Then
            AtLeastOneVisible = True
            Exit For
        End If
    Next myPivotItem

    If AtLeastOneVisible Then
        For Each myPivotItem In .PivotItems
            If myPivotItem .Caption <> ... And myPivotItem .Caption <> ... Then
                pi.Visible = False
            End If
        Next myPivotItem 
    End If
End With