Sub SituReste()
Dim d As Object, de As Object, k, ke, ech, trp, n%, i%, r%, Techr(), f As Worksheet
Set d = CreateObject("Scripting.Dictionary")
Set de = CreateObject("Scripting.Dictionary")
For Each f In Worksheets
Select Case f.Name
Case "Statistique", "Liste donnée", "Liste de choix"
Case Else
With f
Set ici = .Range("C:D").Find("Total", lookat:=xlPart) 'recherche de la ligne "Total..."
If Not ici Is Nothing Then
n = ici.Row
End If
' n = .Cells(.Rows.Count, 3).End(xlUp).Row - 1
' Do Until .Cells(n + 1, 3) Like "Total*"
' n = n - 1
' Loop
For i = 2 To n
If .Cells(i, 3) <> "" Then
r = .Cells(i, 5) - .Cells(i, 6)
If r > 0 Then
k = .Cells(i, 1): ke = k & "|" & .Cells(i, 3)
If de.exists(ke) Then
trp = de(ke)
trp(1) = CInt(trp(1)) + r
de(ke) = trp
Else
trp = Array(.Cells(i, 4), r, .Cells(i, 7))
d(k) = d(k) & "|" & .Cells(i, 3)
de(ke) = trp
End If
End If
End If
Next i
End With
End Select
Next f
ReDim Techr(de.Count - 1, 5): n = 0
For Each k In d.keys
ech = Split(d(k), "|")
Techr(n, 1) = k
For i = 1 To UBound(ech)
ke = k & "|" & ech(i)
trp = de(ke)
Techr(n, 0) = k: Techr(n, 2) = ech(i): Techr(n, 3) = trp(0)
Techr(n, 4) = CInt(trp(1)): Techr(n, 5) = Val(Replace(trp(2), ",", "."))
n = n + 1
Next i
Next k
Application.ScreenUpdating = False
With Worksheets("Statistique")
.Range("A1").CurrentRegion.Offset(1).Clear
With .Range("A2").Resize(n, 6)
.Value = Techr
.Borders.Weight = xlThin
.Columns("F").NumberFormat = "# ##0.00 €"
With .Columns("B")
.VerticalAlignment = xlCenter
.WrapText = True
Application.DisplayAlerts = False
For i = 1 To n
If .Cells(i, 1) <> "" Then
For r = i + 1 To n
If .Cells(r, 1) <> "" Then Exit For
Next r
With Range(.Cells(i, 1), .Cells(r - 1, 1))
.Merge
End With
With .Cells(i, 1).MergeArea.Resize(, 5)
.BorderAround xlContinuous, xlMedium
.Borders(xlInsideVertical).Weight = xlMedium
End With
If r > n Then Exit For Else i = r - 1
End If
Next i
End With
End With
End With
End Sub