Search code examples
excelvbaruntime-error

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


So I'm trying to filter the same date of a couple of pivot tables. It works to the first pivot table, but then the others 5, gives the error of "run-time error 1004..."

They're 6 pivot tables that has the same data base. And the code is in module form of the workbook.

Code below:

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

Solution

  • Clear the existing filter before adding the new one.

    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