Option Explicit
Sub test()
Dim Brr, Crr, i&, x, Y, K&
Dim 部門$, 原價&, 本年&, 累計&, 金額&, 增減$
Set Y = CreateObject("Scripting.Dictionary")
Brr = Range([含公式!P1], [含公式!A65536].End(3))
For i = 5 To UBound(Brr)
部門 = Brr(i, 1)
原價 = Brr(i, 8)
本年 = Brr(i, 9)
累計 = Brr(i, 10)
金額 = Brr(i, 11)
增減 = Brr(i, 16)
If Y.Exists(部門) = False Then
Set Y(部門) = CreateObject("Scripting.Dictionary")
End If
If 增減 = "" Or 增減 = "增加" Then
Y(部門)(1) = Y(部門)(1) + 原價
Y(部門)(2) = Y(部門)(2) + 本年
Y(部門)(3) = Y(部門)(3) + 累計
Y(部門)(4) = Y(部門)(4) + 金額
If 增減 = "增加" Then
Y(部門)(5) = Y(部門)(5) + 原價
End If
ElseIf 增減 = "減少" Then
Y(部門)(6) = Y(部門)(6) + 原價
Y(部門)(7) = Y(部門)(7) + 累計
Y(部門)(9) = Y(部門)(7)
Y(部門)(10) = Y(部門)(10) + 金額
End If
Next
ReDim Crr(1 To Y.Count + 1, 1 To 11)
For Each x In Y.KEYS
K = K + 1
Crr(K, 1) = x
For i = 2 To 11
Crr(K, i) = Y(x)(i - 1)
Crr(UBound(Crr), i) = Crr(UBound(Crr), i) + Y(x)(i - 1)
Next
Next
Crr(UBound(Crr), 1) = "合計"
[T5].Resize(UBound(Crr), 11) = Crr
Set Y = Nothing
Set Brr = Nothing
Set Crr = Nothing
End Sub作者: cypd 時間: 2022-11-24 17:15