|
Sub 汇总()
Dim ar As Variant, br As Variant
Dim i As Long, r As Long, rs As Long
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
With Sheets("源数据表")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:h" & rs)
End With
With Sheets("日报表")
r = .Cells(Rows.Count, 2).End(xlUp).Row
If r > 3 Then
.Range("a4:i" & r).Borders.LineStyle = 0
.Range("a4:i" & r) = Empty
End If
br = .Range("a3:i" & rs)
For j = 3 To UBound(br, 2) - 2
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)) <> "" And Trim(ar(i, 2)) <> "" Then
zf = ar(i, 1) & "|" & Trim(ar(i, 2))
t = d(zf)
If t = "" Then
k = k + 1
d(zf) = k
t = k
br(k, 1) = ar(i, 1)
br(k, 2) = ar(i, 2)
dc(br(k, 1)) = ""
End If
lh = d(Trim(ar(i, 3)))
If lh <> "" Then
br(t, lh) = br(t, lh) + ar(i, 8)
End If
End If
Next i
For i = 2 To k
For j = 3 To UBound(br, 2) - 2
If br(i, j) = "" Then br(i, j) = 0
br(i, 8) = br(i, 8) + br(i, j)
Next j
Next i
For Each kc In dc.keys
n = 0
ReDim arr(1 To k, 1 To UBound(br, 2))
For i = 2 To k
If br(i, 1) = kc Then
n = n + 1
For j = 1 To UBound(br, 2)
arr(n, j) = br(i, j)
Next j
End If
Next i
ws = .Cells(Rows.Count, 2).End(xlUp).Row + 1
.Cells(ws, 1).Resize(n, UBound(arr, 2)) = arr
.Cells(ws + n, 2) = "小计"
For j = 3 To UBound(br, 2) - 1
.Cells(ws + n, j) = Application.Sum(Application.Index(arr, 0, j))
Next j
.Cells(ws, 1).Resize(n + 1, UBound(arr, 2)).Borders.LineStyle = 1
Next kc
End With
MsgBox "ok!"
End Sub
|
|