Option Explicit
Sub TEST()
Dim Arr, Brr, Crr, Z, Q, R&, i&, j%, T$, 費用項%
If [E2] <> "" Then Range([E2], [E65536].End(3)).ClearContents: Exit Sub
Range([E2], [E65536].End(3)(2)).ClearContents
Set Z = CreateObject("Scripting.Dictionary")
Brr = Range([N1], [K65536].End(3))
Crr = Range([E1], [A65536].End(3)): R = UBound(Crr)
ReDim Arr(1 To R, 1 To 10)
For i = 2 To UBound(Brr)
If Z(Brr(i, 2)) = "" Then 費用項 = 費用項 + 1: Z(Brr(i, 2)) = 0: Arr(1, 費用項) = Brr(i, 2)
T = Brr(i, 1) & "|" & Brr(i, 2)
Z(T) = Brr(i, 3)
For Each Q In Split(Brr(i, 4), ",")
If Q <> "" Then Z(T & "|" & Q) = Brr(i, 3)
Next
Next
For i = 2 To R
For j = 1 To 費用項
T = Crr(i, 1) & "|" & Arr(1, j)
Arr(i, j) = Z(T) - Z(T & "|" & Crr(i, 2))
If Arr(i, j) <> 0 Then Z(T & "/t") = Z(T & "/t") + Crr(i, 4)
Next
Next
For i = 2 To R
For j = 1 To 費用項
T = Crr(i, 1) & "|" & Arr(1, j)
If Z(T & "/t") > 0 Then Crr(i - 1, 1) = Val(Crr(i - 1, 1)) + Arr(i, j) * (Crr(i, 4) / Z(T & "/t"))
Next
Next
[E2].Resize(R - 1, 1) = Crr
Set Z = Nothing: Erase Arr, Brr, Crr
End Sub