Option Explicit
Sub FiltreSimple()
Dim Fs As Worksheet, Fc As Worksheet, dico As Object, c, i As Long, Tb()
Dim X As Long, Y As Long, Z As Double
Set Fs = ThisWorkbook.Worksheets("saisie")
Set Fc = ThisWorkbook.Worksheets("CommeTCD")
Set dico = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
'on affiche toutes les données s'il y a un filtre actif
ThisWorkbook.Worksheets("saisie").ListObjects("TbSaisie").AutoFilter.ShowAllData
'on efface toutes les lignes de la nouvelle feuille nommée commetcd
Fc.Range("A1").CurrentRegion.Offset(1).Clear
'on récupère toutes les données dans un tableau
Tb = [Tbsaisie].Value
'Avec un dictionnaire on récupre tous les cilents sans doublons
For i = 1 To UBound(Tb)
If Tb(i, 2) <> "" Then
dico(Tb(i, 2)) = ""
End If
Next i
If dico.Count > 0 Then
i = 2
With Worksheets("saisie")
'on fait une boucle pour filtrer les lignes sur chaque client
For Each c In dico.keys
.ListObjects("Tbsaisie").Range.AutoFilter Field:=2, Criteria1:=c
X = Range("Tbsaisie").ListObject.ListColumns(2).DataBodyRange.SpecialCells(xlCellTypeVisible).Count
Y = WorksheetFunction.Subtotal(103, [Tbsaisie[Rebut ]])
Z = (Y / X)
With Fc
.Cells(i, 1) = c
.Cells(i, 2) = X
.Cells(i, 3) = Y
.Cells(i, 4) = Z
.Cells(i, 4).NumberFormat = "0.00%"
i = i + 1
End With
Next
End With
End If
ThisWorkbook.Worksheets("saisie").ListObjects("TbSaisie").AutoFilter.ShowAllData
MsgBox "Traitement terminé!"
Application.ScreenUpdating = True
Set Fs = Nothing
Set Fc = Nothing
Set dico = Nothing
End Sub