Option Explicit
Sub test()
Dim ws As Worksheet, a, w(), x, y, myMonth As String, flag As Boolean
Dim i As Long, ii As Long, iii As Long, iiii As Long, n As Long, t As Long
With CreateObject("Scripting.Dictionary")
For Each ws In Worksheets
If ws.Name <> "Bilan" Then
a = ws.Range("a1").CurrentRegion.Value
Set .Item(ws.Name) = _
CreateObject("Scripting.Dictionary")
For i = 3 To UBound(a, 1)
myMonth = Format$(a(i, 1), "mm-yyyy")
If Not .Item(ws.Name).exists(myMonth) Then
Set .Item(ws.Name)(myMonth) = _
CreateObject("Scripting.Dictionary")
End If
For ii = 3 To UBound(a, 2)
If a(1, ii) = "" Then a(1, ii) = a(1, ii - 1)
If Not .Item(ws.Name)(myMonth).exists(a(1, ii)) Then
Set .Item(ws.Name)(myMonth)(a(1, ii)) = _
CreateObject("Scripting.Dictionary")
End If
If Not .Item(ws.Name)(myMonth)(a(1, ii)).exists(a(2, ii)) Then
ReDim w(1 To 2)
w(1) = a(2, ii)
Else
w = .Item(ws.Name)(myMonth)(a(1, ii))(a(2, ii))
End If
w(2) = w(2) + a(i, ii)
.Item(ws.Name)(myMonth)(a(1, ii))(a(2, ii)) = w
Next
Next
End If
Next
x = .keys: y = .items
End With
Application.ScreenUpdating = False
With Sheets("Bilan")
.Cells.Clear
For i = 0 To UBound(y)
n = n + 1
With .Cells(n, 1)
.Value = x(i)
With .Resize(, 3)
.HorizontalAlignment = xlCenterAcrossSelection
.Interior.ColorIndex = 33
End With
End With
For ii = 0 To y(i).Count - 1
If ii = 0 Then n = n + 1 Else n = n + 2
With .Cells(n, 1)
.Value = y(i).keys()(ii)
With .Resize(, 3)
.HorizontalAlignment = xlCenterAcrossSelection
.Interior.ColorIndex = 4
End With
End With
For iii = 0 To y(i).items()(ii).Count - 1
For iiii = 0 To y(i).items()(ii).items()(iii).Count - 1
If y(i).items()(ii).items()(iii).items()(iiii)(2) Then
n = n + 1: t = t + 1: flag = True
.Cells(n, 1).Offset(, 1).Resize(, 2).Value = y(i).items()(ii).items()(iii).items()(iiii)
End If
Next
If t > 0 Then
With .Cells(n + 1 - t, 1)
.Value = y(i).items()(ii).keys()(iii)
.Resize(t).Merge
End With
t = 0
End If
Next
With .Cells(n, 1).CurrentRegion
If flag = True Then
.BorderAround Weight:=xlThin
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
.VerticalAlignment = xlCenter
If ii = 0 Then
With .Offset(2).Resize(.Rows.Count - 2)
.HorizontalAlignment = xlCenter
End With
Else
With .Offset(1).Resize(.Rows.Count - 1)
.HorizontalAlignment = xlCenter
End With
End If
Else
With .Resize(, 3)
.BorderAround Weight:=xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
End With
End If
flag = False
End With
Next
n = n + 1
Next
End With
Application.ScreenUpdating = True
End Sub
klin89