Private Sub Worksheet_Activate()
Dim d As Object, LO As ListObject, n%, tablo, i&, resu(), nn&, a, j%, c As Range
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare 'la casse est ignorée
For Each LO In Sheets("Données").ListObjects
If LO.Range(1) = "Matériel" And LO.Range(1, 2) = "Quantité" And LO.Range(1, 3) = "Longueur" Then
n = n + 1
tablo = LO.Range 'matrice, plus rapide
For i = 2 To UBound(tablo)
If Not d.exists(tablo(i, 1)) Then d(tablo(i, 1)) = d.Count 'mémorise la ligne
Next i
End If
Next LO
If n = 0 Then Cells.Delete: Exit Sub
ReDim resu(1 To d.Count + 2, 1 To 2 * n + 4)
n = 0
For Each LO In Sheets("Données").ListObjects
If LO.Range(1) = "Matériel" And LO.Range(1, 2) = "Quantité" And LO.Range(1, 3) = "Longueur" Then
n = n + 1
resu(1, 2 * n) = LO.Name
resu(2, 2 * n) = "Quantité"
resu(2, 2 * n + 1) = "Longueur"
tablo = LO.Range 'matrice, plus rapide
For i = 2 To UBound(tablo)
nn = d(tablo(i, 1)) + 3 'récupère la ligne
If IsNumeric(CStr(tablo(i, 2))) Then resu(nn, 2 * n) = resu(nn, 2 * n) + CDbl(tablo(i, 2))
If IsNumeric(CStr(tablo(i, 3))) Then resu(nn, 2 * n + 1) = resu(nn, 2 * n + 1) + CDbl(tablo(i, 3))
Next i
End If
Next LO
'---total---
resu(1, 2 * n + 3) = "Total général"
resu(2, 2 * n + 2) = " " 'pour la largeur de la colonne
resu(2, 2 * n + 3) = "Quantité"
resu(2, 2 * n + 4) = "Longueur"
a = d.keys
For i = 3 To UBound(resu)
resu(i, 1) = a(i - 3)
For j = 1 To n
If IsNumeric(CStr(resu(i, 2 * j))) Then resu(i, 2 * n + 3) = resu(i, 2 * n + 3) + CDbl(resu(i, 2 * j))
If IsNumeric(CStr(resu(i, 2 * j + 1))) Then resu(i, 2 * n + 4) = resu(i, 2 * n + 4) + CDbl(resu(i, 2 * j + 1))
Next j, i
'---restitution---
Application.ScreenUpdating = False
Cells.Delete 'RAZ
With [C2] '1ère cellule de destination
.Resize(UBound(resu), UBound(resu, 2)) = resu
.Cells(3).Resize(UBound(resu) - 2).Borders.Weight = xlThin
For Each c In .EntireRow.SpecialCells(xlCellTypeConstants)
c.Resize(, 2).Merge 'fusionne
c.Resize(UBound(resu), 2).Borders.Weight = xlThin
Next c
.Cells(1, 2).Resize(, UBound(resu, 2) - 1).EntireColumn.HorizontalAlignment = xlCenter
End With
Columns.AutoFit 'ajuste les largeurs
End Sub