|
Sub test()
Dim Arr, brr, d As Object, d1 As Object, dd
Dim k, mx
Dim i%, j%, m%, n%
Dim s$, ss$
Dim sm As Double
Set d = CreateObject("Scripting.Dictionary")
Set dd = CreateObject("Scripting.Dictionary")
Set d1 = CreateObject("Scripting.Dictionary")
Dim yy
yy = 0
With Sheet1
Arr = .[a1].CurrentRegion
ReDim brr(1 To UBound(Arr), 1 To 1000)
For i = 2 To UBound(Arr) '
If Arr(i, 3) = "STR" Then
s = Left(Arr(i, 1), 4): ss = Arr(i, 2) & ""
If Val(s) > yy Then yy = Val(s)
If Not d.exists(s) Then Set d(s) = CreateObject("Scripting.Dictionary")
If Not dd.exists(ss) Then
Set dd(ss) = CreateObject("Scripting.Dictionary")
End If
If Not dd(ss).exists(s) Then
Set dd(ss)(s) = CreateObject("Scripting.Dictionary")
End If
dd(ss)(s)(Val(Arr(i, 1))) = dd(ss)(s)(Val(Arr(i, 1))) + Arr(i, 4)
zz = dd(ss)(s).keys
d(s)(ss) = Arr(i, 4) + d(s)(ss)
d1(ss) = ""
End If
Next
ReDim brr(1 To UBound(Arr), 1 To 5000)
m = 1: n = 1
brr(1, 1) = "金额"
For Each k In d.keys
n = n + 1
brr(1, n) = k
Next
For Each k In d1.keys
m = m + 1
brr(m, 1) = k
Next
On Error Resume Next
'导入到结果表区域
.[l1].Resize(m, n) = brr
Arr = .[l1].Resize(m, n + 3)
Arr(1, n + 1) = "今年最后一笔STR的金额"
Arr(1, n + 2) = "今年所有STR的金额合计"
Arr(1, n + 3) = "所有年份STR的金额合计"
For i = 2 To UBound(Arr)
For j = 2 To n
ss = Arr(i, 1)
s = Left(Arr(1, j), 4)
Arr(i, j) = d(s)(ss)
Next
s = yy & ""
ss = Arr(i, 1) & ""
If dd(Arr(i, 1)).exsits(s) Then
wrr = dd(ss)(s).keys
mx = WorksheetFunction.Max(dd(ss)(s).keys)
Arr(i, n + 1) = dd(ss)(s)(mx)
Arr(i, n + 2) = WorksheetFunction.Sum(dd(ss)(s).items)
sm = 0
For Each k In dd(ss).keys
sm = sm + WorksheetFunction.Sum(dd(ss)(k).items)
Next k
Arr(i, n + 3) = sm
End If
Next
.[l1].Resize(m, n + 3) = Arr
End With
End Sub |
|