Sub GrouperPeriode()
Dim xrg As Range, t0, t, der&, i&, i0
Dim som, ref, j&, n&, datfin As Date, deb
Application.ScreenUpdating = False: deb = Timer
With Sheets("Extraction")
If .FilterMode Then .ShowAllData
Set xrg = Intersect(.Range("a1").CurrentRegion, .Columns("a:i"))
t0 = xrg.Value
xrg.Sort key1:=.Range("a1"), order1:=xlAscending, key2:=.Range("g1"), order2:=xlAscending, _
key3:=.Range("h1"), order3:=xlAscending, MatchCase:=False, Header:=xlYes
t = xrg.Resize(xrg.Rows.Count + 1, xrg.Columns.Count).Value
xrg.Value = t0: Erase t0
End With
t(1, 1) = "Matricule": t(1, 2) = "Code abs": t(1, 3) = "Date debut": t(1, 4) = "Date fin": t(1, 5) = "Qté abs"
For i = 2 To UBound(t): t(i, 2) = t(i, 8): t(i, 3) = t(i, 7): t(i, 4) = t(i, 7): t(i, 5) = t(i, 9): Next
ReDim Preserve t(1 To UBound(t), 1 To 5)
n = 1: i0 = 2
For i = 3 To UBound(t)
If t(i, 1) <> t(i0, 1) Or t(i, 2) <> t(i0, 2) Or t(i, 3) <> t(i - 1, 3) + 1 Then
n = n + 1
For j = 1 To 5: t(n, j) = t(i0, j): Next
i0 = i
Else
t(i0, 4) = t(i, 4): t(i0, 5) = t(i0, 5) + t(i, 5)
End If
Next i
With Worksheets("synthèse")
.Range("a1").CurrentRegion.Clear
.Range("a1").Resize(n, 5) = t
.Range("e1").Resize(n).NumberFormat = "0.00"
Application.Goto .Range("a1"), True
End With
MsgBox Format(Timer - deb, "0.00\ sec.")
End Sub