|
Option Explicit
Sub TEST10()
Dim ar, br, cr, i&, j&, r&, n&, t#, dic As Object, vKey
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set dic = CreateObject("Scripting.Dictionary")
ar = [B2].CurrentRegion: t = Timer
For i = 2 To UBound(ar)
If Not dic.exists(ar(i, 1)) Then
Set dic(ar(i, 1)) = CreateObject("Scripting.Dictionary")
End If
dic(ar(i, 1))(ar(i, 2)) = dic(ar(i, 1))(ar(i, 2)) + ar(i, 3)
Next i
ReDim br(1 To 10 ^ 3, 1 To 4)
r = 1
cr = Split("大类 合计1 子类 合计2")
For j = 0 To UBound(cr)
br(r, j + 1) = cr(j)
Next j
For Each vKey In dic.keys
n = 0
For j = 0 To dic(vKey).Count - 1
r = r + 1
br(r, 1) = vKey
br(r, 3) = dic(vKey).keys()(j)
br(r, 4) = dic(vKey).items()(j)
n = n + br(r, 4)
Next j
br(r, 2) = n
Next
Columns("G:J").Clear
With [G2].Resize(r, 4)
.Value = br
br = .Resize(.Rows.Count + 1)
r = 2
For i = 2 To UBound(br) - 1
If br(i + 1, 1) <> br(r, 1) Then
For j = 1 To 2
Range(.Cells(r, j), .Cells(i, j)).Cells.Merge
Next j
r = i + 1
End If
Next i
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Borders.LineStyle = xlContinuous
End With
Set dic = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "执行完毕!_用时: " & Format(Timer - t, "0.00") & " 秒", 64
End Sub
|
|