运行-时间错误“1004”:应用程序定义或对象定义的错误 - 按日期过滤一对枢轴 table

Run-time error '1004': Application defined or object-defined error - filter couple of pivot table by date

所以我正在尝试过滤几个枢轴 table 的相同日期。 它适用于第一个枢轴 table,但随后是其他 5 个,给出错误“运行-time error 1004...”

它们是具有相同数据库的 6 个枢轴 table。 代码是工作簿的模块形式。

代码如下:

Sub filter()

Windows("SAFE.xlsm").Activate
Sheets("Dinamic").Activate

Dim Datainicial As String
Dim Datafinal As String

Datainicial = Range("A2").Text
Datafinal = Range("C2").Text

If Range("A2").Text = "" Or Range("C2").Text = "" Then

MsgBox "Veuillez sélectionner la période souhaitée pour l'analyse.", vbCritical, "Date d'insertion"
Exit Sub
End If


Dim Tabela1 As PivotTable
Dim Tabela2 As PivotTable
Dim Tabela3 As PivotTable
Dim Tabela4 As PivotTable
Dim Tabela5 As PivotTable
Dim Tabela6 As PivotTable

Set Tabela1 = ActiveSheet.PivotTables("Dist")
Set Tabela2 = ActiveSheet.PivotTables("Protection")
Set Tabela3 = ActiveSheet.PivotTables("IGtraité")
Set Tabela4 = ActiveSheet.PivotTables("PIMOF")
Set Tabela5 = ActiveSheet.PivotTables("IGencours")
Set Tabela6 = ActiveSheet.PivotTables("IGouvert")

'Tabela1 - Distribuição afetação TOP 5
Tabela1.ClearAllFilters
Tabela1.PivotFields("Code NITG").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="(em branco)"
Tabela1.PivotFields("Dernière source intégrée").PivotFilters.Add Type:=xlCaptionEquals, Value1:="DRG"
 Range("T7").Select
    ActiveSheet.PivotTables("Dist").PivotFields("Libellé NITG").AutoSort _
        xlDescending, "Quantité", ActiveSheet.PivotTables("Dist").PivotColumnAxis. _
        PivotLines(1), 1
        ActiveSheet.PivotTables("Dist").PivotFields("Code NITG").ClearAllFilters
    ActiveSheet.PivotTables("Dist").PivotFields("Code NITG").PivotFilters.Add2 _
        Type:=xlTopCount, DataField:=ActiveSheet.PivotTables("Dist").PivotFields( _
        "Quantité"), Value1:=5
Tabela1.PivotFields("Date d'insertion").PivotFilters.Add Type:=xlDateBetween, Value1:=Datainicial, Value2:=Datafinal


'Tabela4 - PIMOF
Tabela4.PivotFields("Date d'insertion").PivotFilters.Add Type:=xlDateBetween, Value1:=Datainicial, Value2:=Datafinal

'Tabela5 - IG en cours
Tabela5.PivotFields("Date d'insertion").PivotFilters.Add Type:=xlDateBetween, Value1:=Datainicial, Value2:=Datafinal

'Tabela6 - IG ouverts
Tabela6.PivotFields("Date d'insertion").PivotFilters.Add Type:=xlDateBetween, Value1:=Datainicial, Value2:=Datafinal


'Tabela3 - IG traité
Tabela3.PivotFields("Date d'insertion").PivotFilters.Add Type:=xlDateBetween, Value1:=Datainicial, Value2:=Datafinal

MsgBox "Période d'analyse souhaitée définie."

End Sub

在添加新过滤器之前清除现有过滤器。

Option Explicit

Sub filter()

    Windows("SAFE.xlsm").Activate
    Sheets("Dinamic").Activate
    
    Dim Datainicial As String, Datafinal As String
    Datainicial = Range("A2").Text
    Datafinal = Range("C2").Text
    
    If Range("A2").Text = "" Or Range("C2").Text = "" Then
        MsgBox "Veuillez sélectionner la période souhaitée pour l'analyse.", vbCritical, "Date d'insertion"
        Exit Sub
    End If
    
    Dim Tabel As PivotTable, ar, tbl
    ar = Array("Dist", "Protection", "IGtraité", "PIMOF", "IGencours", "IGouvert")
    For Each tbl In ar
       Set Tabel = ActiveSheet.PivotTables(tbl)
       With Tabel
           If tbl = "Dist" Then
               .ClearAllFilters
               .PivotFields("Code NITG").PivotFilters.Add _
                    Type:=xlCaptionDoesNotContain, Value1:="(em branco)"
    
               .PivotFields("Dernière source intégrée").PivotFilters.Add _
                    Type:=xlCaptionEquals, Value1:="DRG"
    
               .PivotFields("Libellé NITG").AutoSort xlDescending, "Quantité", _
                    .PivotColumnAxis.PivotLines(1), 1

               .PivotFields("Code NITG").PivotFilters.add2 _
                    Type:=xlTopCount, DataField:=.PivotFields("Quantité"), Value1:=5
            Else
                .PivotFields("Date d'insertion").ClearAllFilters
            End If
            .PivotFields("Date d'insertion").PivotFilters.Add _
                Type:=xlDateBetween, Value1:=Datainicial, Value2:=Datafinal
       End With
    Next
    
    MsgBox "Période d'analyse souhaitée définie."

End Sub