Sub 分类汇总()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long, rs As Long
Dim br(), Cr(), dr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With ActiveSheet
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "数据源为空!": End
ar = .Range("A1:G" & r)
ReDim br(1 To UBound(ar), 1 To 3)
ReDim Cr(1 To UBound(ar), 1 To 3)
ReDim dr(1 To UBound(ar), 1 To 3)
For i = 2 To UBound(ar)
If ar(i, 2) <> "" Then
t1 = d(ar(i, 2))
If t1 = "" Then
k1 = k1 + 1
d(ar(i, 2)) = k1
t1 = k1
br(k1, 1) = ar(i, 2)
End If
br(t1, 2) = br(k1, 2) + 1
br(t1, 3) = br(t1, 3) + ar(i, 6)
End If
If ar(i, 3) <> "" Then
t2 = d(ar(i, 3))
If t2 = "" Then
k2 = k2 + 1
d(ar(i, 3)) = k2
t2 = k2
Cr(k2, 1) = ar(i, 3)
End If
Cr(t2, 2) = Cr(t2, 2) + 1
Cr(t2, 3) = Cr(t2, 3) + ar(i, 6)
End If
If ar(i, 5) <> "" Then
t3 = d(ar(i, 5))
If t3 = "" Then
k3 = k3 + 1
d(ar(i, 5)) = k3
t3 = k3
dr(k3, 1) = ar(i, 5)
End If
dr(t3, 2) = dr(t3, 2) + 1
dr(t3, 3) = dr(t3, 3) + ar(i, 6)
End If
Next i
.[p1].CurrentRegion.Offset(1) = Empty
x = Application.Max(k1, k2, k3) + 1
.[p2].Resize(k1, UBound(br, 2)) = br
.Cells(x, 16) = "合计"
.Cells(x, 17) = Application.Sum(Application.Index(br, 0, 2))
.Cells(x, 18) = Application.Sum(Application.Index(br, 0, 3))
.[s2].Resize(k2, UBound(Cr, 2)) = Cr
.Cells(x, 19) = "合计"
.Cells(x, 20) = Application.Sum(Application.Index(Cr, 0, 2))
.Cells(x, 21) = Application.Sum(Application.Index(Cr, 0, 3))
.[v2].Resize(k3, UBound(dr, 2)) = dr
.Cells(x, 22) = "合计"
.Cells(x, 23) = Application.Sum(Application.Index(dr, 0, 2))
.Cells(x, 24) = Application.Sum(Application.Index(dr, 0, 3))
End With
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|