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
Excel VBA不包括过滤器中的项目,当它们';你并不总是在那里_Excel_Vba_Filter_Pivot_Pivot Table - Fatal编程技术网

Excel VBA不包括过滤器中的项目,当它们';你并不总是在那里

Excel VBA不包括过滤器中的项目,当它们';你并不总是在那里,excel,vba,filter,pivot,pivot-table,Excel,Vba,Filter,Pivot,Pivot Table,我试图从数据透视表中自动排除某些项目,有些客户我不想包括在内,我得到了以下VBA,它似乎排除了这些项目,但如果下次运行时数据中没有这些项目,它就找不到要排除的客户,然后停止工作。 如果找不到客户,是否有办法让it忽略他们,然后继续下一个客户 Sub Filtering() ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME"). _ CurrentP

我试图从数据透视表中自动排除某些项目,有些客户我不想包括在内,我得到了以下VBA,它似乎排除了这些项目,但如果下次运行时数据中没有这些项目,它就找不到要排除的客户,然后停止工作。 如果找不到客户,是否有办法让it忽略他们,然后继续下一个客户

Sub Filtering()


    ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME")
        .PivotItems("Customer1").Visible = False
        .PivotItems("Customer2").Visible = False
        .PivotItems("Customer3").Visible = False

    End With
End Sub
如果找不到客户,是否有办法让it忽略他们,然后继续下一个客户

Sub Filtering()


    ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME")
        .PivotItems("Customer1").Visible = False
        .PivotItems("Customer2").Visible = False
        .PivotItems("Customer3").Visible = False

    End With
End Sub
我可以很快想到两种方法。你挑吧:)

方式1

检查透视项目是否存在,然后尝试使用它。此外,最好使用对象。易于管理您的代码

这就是你想要的吗

Option Explicit

Dim pField As PivotField

Sub Filtering()
    Dim ws As Worksheet
    '~~> Change this to relevant sheet
    Set ws = Sheet1
    
    Dim pTbl As PivotTable
    Set pTbl = ws.PivotTables("PivotTable2")
    
    Set pField = pTbl.PivotFields("CUSTOMER NAME")
    
    pField.CurrentPage = "(All)"
    
    With pField
        If DoesPivotItemExist("Customer1") Then .PivotItems("Customer1").Visible = False
        If DoesPivotItemExist("Customer4") Then .PivotItems("Customer4").Visible = False
        If DoesPivotItemExist("Customer3") Then .PivotItems("Customer3").Visible = False
    End With
End Sub

'~~> Function to check if the pivot item exists
Private Function DoesPivotItemExist(pvtItem As String) As Boolean
    Dim pitm As PivotItem
    
    For Each pitm In pField.PivotItems
        If pitm.Name = pvtItem Then
            DoesPivotItemExist = True
            Exit For
        End If
    Next pitm
End Function
方式2

在错误恢复下一步时使用
和错误转到0时使用
隐藏项目,而不检查项目是否存在

比如说

Option Explicit

Dim pField As PivotField

Sub Filtering()
    Dim ws As Worksheet
    '~~> Change this to relevant sheet
    Set ws = Sheet1
    
    Dim pTbl As PivotTable
    Set pTbl = ws.PivotTables("PivotTable2")
    
    Set pField = pTbl.PivotFields("CUSTOMER NAME")
    
    pField.CurrentPage = "(All)"
    
    With pField
       HidePivotItem "Customer1"
       HidePivotItem "Customer2"
       HidePivotItem "Customer4"
    End With
End Sub

Private Sub HidePivotItem(pvtItem As String)
    On Error Resume Next
    pField.PivotItems(pvtItem).Visible = False
    On Error GoTo 0
End Sub
如果找不到客户,是否有办法让it忽略他们,然后继续下一个客户

Sub Filtering()


    ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("CUSTOMER NAME")
        .PivotItems("Customer1").Visible = False
        .PivotItems("Customer2").Visible = False
        .PivotItems("Customer3").Visible = False

    End With
End Sub
我可以很快想到两种方法。你挑吧:)

方式1

检查透视项目是否存在,然后尝试使用它。此外,最好使用对象。易于管理您的代码

这就是你想要的吗

Option Explicit

Dim pField As PivotField

Sub Filtering()
    Dim ws As Worksheet
    '~~> Change this to relevant sheet
    Set ws = Sheet1
    
    Dim pTbl As PivotTable
    Set pTbl = ws.PivotTables("PivotTable2")
    
    Set pField = pTbl.PivotFields("CUSTOMER NAME")
    
    pField.CurrentPage = "(All)"
    
    With pField
        If DoesPivotItemExist("Customer1") Then .PivotItems("Customer1").Visible = False
        If DoesPivotItemExist("Customer4") Then .PivotItems("Customer4").Visible = False
        If DoesPivotItemExist("Customer3") Then .PivotItems("Customer3").Visible = False
    End With
End Sub

'~~> Function to check if the pivot item exists
Private Function DoesPivotItemExist(pvtItem As String) As Boolean
    Dim pitm As PivotItem
    
    For Each pitm In pField.PivotItems
        If pitm.Name = pvtItem Then
            DoesPivotItemExist = True
            Exit For
        End If
    Next pitm
End Function
方式2

在错误恢复下一步时使用
和错误转到0时使用
隐藏项目,而不检查项目是否存在

比如说

Option Explicit

Dim pField As PivotField

Sub Filtering()
    Dim ws As Worksheet
    '~~> Change this to relevant sheet
    Set ws = Sheet1
    
    Dim pTbl As PivotTable
    Set pTbl = ws.PivotTables("PivotTable2")
    
    Set pField = pTbl.PivotFields("CUSTOMER NAME")
    
    pField.CurrentPage = "(All)"
    
    With pField
       HidePivotItem "Customer1"
       HidePivotItem "Customer2"
       HidePivotItem "Customer4"
    End With
End Sub

Private Sub HidePivotItem(pvtItem As String)
    On Error Resume Next
    pField.PivotItems(pvtItem).Visible = False
    On Error GoTo 0
End Sub

我已经发布了一个新编辑的答案。您可能需要刷新页面才能看到编辑。我已经发布了一个带有新编辑的答案。您可能需要刷新页面才能查看编辑