|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 分类累计()
Application.ScreenUpdating = False
Dim ar As Variant
Dim d As Object
Dim br()
Set d = CreateObject("scripting.dictionary")
With Sheets("原数据")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "原数据为空!": End
ar = .Range("a1:d" & r)
End With
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) <> "" Then
d(Trim(ar(i, 1))) = ""
End If
Next i
With Sheets("目标输出")
.[a1].CurrentRegion.Offset(1) = Empty
For Each k In d.keys
n = 0
ReDim br(1 To UBound(ar), 1 To UBound(ar, 2) + 2)
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) = k Then
n = n + 1
For j = 1 To UBound(ar, 2)
br(n, j) = ar(i, j)
Next j
If n = 1 Then
br(n, 5) = ar(i, 3)
br(n, 6) = ar(i, 4)
Else
br(n, 5) = br(n - 1, 5) + ar(i, 3)
br(n, 6) = br(n - 1, 6) + ar(i, 4)
End If
End If
Next i
rs = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Cells(rs, 1).Resize(n, UBound(br, 2)) = br
Next k
End With
Set d = Nothing
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|