没有条件与过滤的数据透视表匹配

时间:2015-10-07 16:10:53

标签: vba excel-vba excel

我有VBA代码在物理库存计数期间过滤两个数据透视表,以将计数结果与特定binaisle位置中的先前库存数据进行比较。除非特定位置没有计数数据,否则它的效果很好。然后它列出了表格中的所有内容。如何对其进行编码以在表中显示数据而不是所有数据?

我的代码如下:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Range("B2:B3")) Is Nothing Then Exit Sub

Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String

Set pt = Worksheets("Dbl Chk").PivotTables("PivotTable1")
Set Field = pt.PivotFields("iblc_binaisle")
NewCat = Worksheets("Dbl Chk").Range("B2").Value

On Error Resume Next

With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With


Set pt1 = Worksheets("Dbl Chk").PivotTables("PivotTable2")
Set Field1 = pt1.PivotFields("iblt_binaisle")

On Error Resume Next

With pt1
Field1.ClearAllFilters
Field1.CurrentPage = NewCat
pt.RefreshTable
End With


End Sub

1 个答案:

答案 0 :(得分:0)

未经测试,因为我没有任何数据透视表方便,但我认为这应该有用。您可能需要仔细检查在pivot字段中选择空白的语法。这也应该否定On Error Resume Next的必要性。只有在你绝对拥有的时候才使用它......甚至然后仔细检查是否有办法避免它:)。如果您确实使用它,请确保在想要绕过错误后立即设置On Error GoTo 0

Dim pt As PivotTable, Field As PivotField, NewCat As String, pItem As PivotItem, bFound As Boolean

    Set pt = Worksheets("Dbl Chk").PivotTables("PivotTable1")
    Set Field = pt.PivotFields("iblc_binaisle")
    NewCat = Worksheets("Dbl Chk").Range("B2").Value

    bFound = False
    For Each pItem In Field.PivotItems

        If pItem.Value = NewCat Then
            bFound = True
            Exit For
        End If

    Next

    With Field
        .ClearAllFilters
        If bFound Then .CurrentPage = NewCat Else: .CurrentPage = "<>" 'i think this is syntax for blanks
    End With

    pt.RefreshTable