1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| Option Explicit
Sub test()
Dim a, e, v, w(), i As Long, j As Long, n As Long, dico As Object
Set dico = CreateObject("Scripting.Dictionary")
dico.CompareMode = 1
With Sheets("Base de donnée")
a = .Range("a1").CurrentRegion.Value
For i = 2 To UBound(a, 1)
If Not dico.exists(a(i, 4)) Then
Set dico(a(i, 4)) = CreateObject("Scripting.Dictionary")
dico(a(i, 4)).CompareMode = 1
End If
If Not dico(a(i, 4)).exists(a(i, 1)) Then
ReDim w(1 To UBound(a, 2) - 2, 1 To 3)
Else
w = dico(a(i, 4))(a(i, 1))
ReDim Preserve w(1 To UBound(w, 1), 1 To UBound(w, 2) + 1)
End If
w(1, UBound(w, 2) - 2) = a(i, 4)
w(2, UBound(w, 2) - 2) = a(i, 1)
For j = 3 To UBound(w, 1)
w(j, UBound(w, 2) - 2) = a(i, j + 2)
Next
dico(a(i, 4))(a(i, 1)) = w
Next
For Each e In dico.keys
For Each v In dico(e).keys
w = dico.Item(e).Item(v)
w(1, UBound(w, 2) - 1) = e: w(1, UBound(w, 2)) = e
w(2, UBound(w, 2) - 1) = v: w(2, UBound(w, 2)) = v
For i = 3 To UBound(w, 1)
w(i, UBound(w, 2) - 1) = _
Application.Sum(Application.Index(w, i, Evaluate("row(1:" & UBound(w, 2) - 2 & ")")))
Next
For i = 4 To UBound(w, 1) - 1
w(i, UBound(w, 2)) = _
Application.Average(Application.Index(w, Evaluate("row(" & i - 1 & ":" & i + 1 & ")"), UBound(w, 2) - 1))
Next
dico.Item(e).Item(v) = w
Next
Next
End With
'Restitution
With Sheets("Feuil1").Range("a1")
.Parent.Cells.Clear
n = 1
For i = 0 To dico.Count - 1
For j = 0 To dico.items()(i).Count - 1
With .Offset(n).Resize(1, UBound(dico.items()(i).items()(j), 1))
.Value = _
Application.Transpose(Application.Index(dico.items()(i).items()(j), , UBound(dico.items()(i).items()(j), 2)))
End With
n = n + 1
Next
With .Offset(n - 1).CurrentRegion.Resize(dico.items()(i).Count, UBound(dico.items()(0).items()(0), 1))
.BorderAround Weight:=xlThin
End With
n = n + 1
Next
End With
Set dico = Nothing
End Sub |
Partager