Option Explicit
Sub recap()
Dim fichier, tbl, i, j, wbk As Workbook, ligne, f As Worksheet, dico As Object, cle
Set dico = CreateObject("Scripting.Dictionary")
Application.DisplayAlerts = False
For Each f In Worksheets
If f.Name <> "compil" Then f.Delete
Next
Application.DisplayAlerts = True
' collecte les données du fichier source dans un tableau
fichier = Application.GetOpenFilename("fichier excel (*.xlsx), *.xlsx", , "Sélection de vos fichiers excel", , False)
If fichier = False Then Exit Sub
Set wbk = Workbooks.Open(fichier)
tbl = Cells(1, 1).CurrentRegion
wbk.Close
' crée les onglets
For j = 2 To UBound(tbl, 2)
dico(tbl(1, j)) = ""
Next
For Each cle In dico.keys
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = cle
End With
Sheets(cle).Cells(1, 1) = "PRODUIT"
Sheets(cle).Cells(1, 2) = "QUANTITE"
Next
' traite le tableau
With Sheets("compil")
If Not .ListObjects(1).DataBodyRange Is Nothing Then .ListObjects(1).DataBodyRange.Delete
For i = 2 To UBound(tbl)
For j = 2 To UBound(tbl, 2)
If tbl(i, j) <> "" And tbl(i, j) <> 0 Then
.ListObjects(1).ListRows.Add
ligne = .ListObjects(1).ListRows.Count
.ListObjects(1).DataBodyRange.Cells(ligne, 1) = tbl(i, 1)
.ListObjects(1).DataBodyRange.Cells(ligne, 2) = tbl(1, j)
.ListObjects(1).DataBodyRange.Cells(ligne, 3) = tbl(i, j)
ligne = Sheets(tbl(1, j)).Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets(tbl(1, j)).Cells(ligne, 1) = tbl(i, 1)
Sheets(tbl(1, j)).Cells(ligne, 2) = tbl(i, j)
End If
Next
Next
.Select
End With
End Sub