|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 分类求和()
Application.ScreenUpdating = False
Dim ar As Variant, br As Variant
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Sheets("数据源")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "数据源为空!": End
ar = .Range("a1:g" & r)
End With
With Sheets("效果")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
If rs > 1 Then .Range("a2:e" & rs).Borders.LineStyle = 0: .Range("a2:e" & rs) = Empty
rr = Split(.[e1], "+")
h = UBound(ar)
br = .Range("a1:e" & h)
For j = 2 To UBound(br, 2) - 1
If Trim(br(1, j)) <> "" Then
d(Trim(br(1, j))) = j
End If
Next j
k = 1
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) <> "" Then
If IsDate(ar(i, 1)) Then
t = d(ar(i, 1))
If t = "" Then
k = k + 1
d(ar(i, 1)) = k
t = k
br(k, 1) = ar(i, 1)
End If
For j = 2 To 6 Step 2
If Trim(ar(i, j)) <> "" Then
If j < 6 Then
lh = d(Trim(ar(i, j)))
If lh <> "" Then
br(t, lh) = br(t, lh) + ar(i, j + 1)
End If
ElseIf j = 6 Then
If Trim(ar(i, j)) = Trim(rr(0)) Or Trim(ar(i, j)) = Trim(rr(1)) Then
br(t, 5) = br(t, 5) + ar(i, j + 1)
End If
End If
End If
Next j
End If
End If
Next i
.[a1].Resize(k, UBound(br, 2)) = br
.[a1].Resize(k, UBound(br, 2)).Borders.LineStyle = 1
End With
MsgBox "ok!"
End Sub
|
|