|
Sub 分类汇总()
Application.ScreenUpdating = False
Dim ar As Variant, br As Variant
Dim i As Long, r As Long, rs As Long
Dim rr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
With Sheets("数据库")
r = .Cells(Rows.Count, 2).End(xlUp).Row
If r < 2 Then MsgBox "数据库工作表为空,请先导入数据!": End
ar = .Range("a1:q" & r)
End With
ReDim rr(1 To UBound(ar))
m = 1
rr(m) = 3
With Sheets("数量汇总")
.UsedRange.Offset(3).Borders.LineStyle = 0
.UsedRange.Offset(3) = Empty
br = .Range("a3:j" & r)
ks = .[d1]
js = .[f1]
For i = 2 To UBound(ar)
If ar(i, 2) <> "" Then
If IsDate(ar(i, 2)) Then
If ar(i, 2) >= ks And ar(i, 2) <= js Then
If ar(i, 4) <> "" Then
s1 = ar(i, 4)
s2 = ar(i, 5)
s3 = ar(i, 6)
s4 = ar(i, 7)
If Not d.exists(s1) Then Set d(s1) = CreateObject("scripting.dictionary")
If Not d(s1).exists(s2) Then Set d(s1)(s2) = CreateObject("scripting.dictionary")
If Not d(s1)(s2).exists(s3) Then Set d(s1)(s2)(s3) = CreateObject("scripting.dictionary")
d(s1)(s2)(s3)(s4) = d(s1)(s2)(s3)(s4) + ar(i, 8)
End If
End If
End If
End If
Next i
For j = 5 To UBound(br, 2)
dc(br(1, j)) = j
Next j
n = 1
For Each k In d.keys
For Each kk In d(k).keys
For Each kkk In d(k)(kk).keys
n = n + 1
br(n, 2) = k
br(n, 3) = kk
br(n, 4) = kkk
For Each kkkk In d(k)(kk)(kkk).keys
lh = dc(kkkk)
br(n, lh) = d(k)(kk)(kkk)(kkkk)
Next kkkk
Next kkk
n = n + 1
br(n, 1) = "小计"
m = m + 1
rr(m) = n + 2
Next kk
Next k
n = n + 2
br(n - 1, 1) = "入库总计"
br(n, 1) = "出库总计"
.[a3].Resize(n, UBound(br, 2)) = br
.[a3].Resize(n, UBound(br, 2)).Borders.LineStyle = 1
w = n + 2
For i = 2 To m
ks = rr(i - 1) + 1
js = rr(i) - 1
For j = 5 To UBound(br, 2)
.Cells(js + 1, j) = Application.Sum(.Range(.Cells(ks, j), .Cells(js, j)))
If .Cells(js - 1, 3) = "入库" Then .Cells(w - 1, j) = .Cells(w - 1, j) + .Cells(js + 1, j)
If .Cells(js - 1, 3) = "出库" Then .Cells(w, j) = .Cells(w, j) + .Cells(js + 1, j)
Next j
Next i
End With
Set d = Nothing
Set dc = Nothing
MsgBox "ok!"
End Sub
|
|