VBA
Sub test()
Dim a, i As Long, ii As Long, myMonth As String, temp As Date, SL As Object, w
Set SL = CreateObject("System.Collections.SortedList")
With Sheets("sheet1")
a = .Range("a1", .Cells.SpecialCells(11)).Value
End With
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For ii = 2 To UBound(a, 2)
If a(4, ii) = "" Then Exit For
Set .Item(a(4, ii)) = CreateObject("Scripting.Dictionary")
Next
For i = 5 To UBound(a, 1)
If IsDate(a(i, 1)) Then
temp = DateAdd("d", -Day(a(i, 1)) + 1, a(i, 1))
myMonth = Format$(a(i, 1), "mmm ""(""yyyy"")")
If Not SL.Contains(temp) Then SL(temp) = myMonth
For ii = 2 To UBound(a, 2)
If (a(i, ii) <> "") * (.exists(a(4, ii))) Then
If Not .Item(a(4, ii)).exists(myMonth) Then
ReDim w(1 To 2)
Else
w = .Item(a(4, ii))(myMonth)
End If
w(1) = w(1) + a(i, ii): w(2) = w(2) + 1
.Item(a(4, ii))(myMonth) = w
End If
Next
End If
Next
ReDim a(1 To .Count + 1, 1 To SL.Count + 1)
For ii = 0 To SL.Count - 1
a(1, ii + 2) = SL.GetByIndex(ii)
Next
For i = 0 To .Count - 1
a(i + 2, 1) = .keys()(i)
For ii = 2 To UBound(a, 2)
If .items()(i).exists(a(1, ii)) Then
a(i + 2, ii) = .items()(i)(a(1, ii))(1) / .items()(i)(a(1, ii))(2)
End If
Next
Next
End With
Sheets.Add.Cells(1).Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub
Bookmarks