Sub test()
Set d = CreateObject("scripting.dictionary")
With Sheets("数据源")
rs = .Cells(Rows.Count, 3).End(xlUp).Row
ar = .Range("a1:bd" & rs)
ReDim br(1 To rs, 1 To UBound(ar, 2))
For i = 6 To rs
m = Application.Sum(.Range(.Cells(i, 9), .Cells(i, "bd")))
If m <> 0 Then
n = n + 1
For j = 1 To UBound(ar, 2)
br(n, j) = ar(i, j)
Next j
If Trim(ar(i, 3)) <> "" Then
d(Trim(ar(i, 3))) = ""
End If
End If
Next i
End With
Sheets("结果表").UsedRange.Offset(5).Clear
For Each k In d.keys
m = 0
t = t + 1
ReDim cr(1 To n, 1 To UBound(br, 2))
For i = 1 To n
If Trim(br(i, 3)) = k Then
m = m + 1
cr(m, 2) = m
For j = 3 To UBound(br, 2)
cr(m, j) = br(i, j)
Next j
End If
Next i
With Sheets("结果表")
ws = .Cells(Rows.Count, 3).End(xlUp).Row + 1
If ws = 5 Then
ws = 6
Else
ws = ws
End If
y = .Cells(Rows.Count, 3).End(xlUp).Row
.Cells(ws, 1).Resize(n, UBound(cr, 2)) = cr
.Cells(ws + m, 3) = "合计"
For j = 9 To UBound(cr, 2)
.Cells(ws + m, j) = Application.Sum(Application.Index(cr, 0, j))
If t = 1 Then
.Cells(ws + m + 1, j) = Application.Sum(Application.Index(cr, 0, j))
Else
Cells(ws + m + 1, j) = .Cells(y, j) + Application.Sum(Application.Index(cr, 0, j))
End If
Next j
.Cells(ws + 1 + m, 3) = "累计"
End With
Next k
End Sub
|