Sub 汇总()
Dim ar As Variant, br As Variant
Dim i As Long, r As Long, rs As Long
Dim cr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Sheets("销售")
r = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:l" & r)
End With
With Sheets("库存")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
br = .Range("a1:f" & rs)
End With
ReDim cr(1 To UBound(ar), 1 To 8)
For i = 2 To UBound(ar)
If ar(i, 1) <> "" Then
s = ar(i, 1) & "|" & ar(i, 2) & "|" & ar(i, 3)
t = d(s)
If t = "" Then
k = k + 1
d(s) = k
t = k
cr(k, 1) = ar(i, 2)
cr(k, 2) = ar(i, 1)
cr(k, 3) = ar(i, 3)
End If
cr(t, 4) = cr(t, 4) + ar(i, 6)
cr(t, 5) = cr(t, 5) + ar(i, 8)
cr(t, 6) = cr(t, 6) + ar(i, 10)
End If
Next i
For i = 2 To UBound(br)
If br(i, 1) <> "" Then
s = br(i, 1) & "|" & br(i, 2) & "|" & br(i, 3)
xh = d(s)
If xh <> "" Then
cr(xh, 7) = cr(xh, 7) + br(i, 5)
cr(xh, 8) = cr(xh, 4) / cr(xh, 7)
End If
End If
Next i
With Sheets("透视表")
.UsedRange.Offset(3) = Empty
.[a4].Resize(k, UBound(cr, 2)) = cr
End With
MsgBox "ok!"
End Sub
|