Sub TEST()
Dim Brr, V4&, V5&, Z, i&, T$, A&, Af&, B$, Bn&, D&, Df, Ra As Range
Set Z = CreateObject("Scripting.Dictionary")
Set Ra = Range([工作表1!H3], [工作表1!A65536].End(3)): Brr = Ra
For i = 1 To UBound(Brr)
T = Brr(i, 3): V4 = Val(Brr(i, 4)): V5 = Val(Brr(i, 5))
If V4 > 0 Then
A = Z(T & "|進"): A = A + V4: Z(T & "|進") = A
Af = Z(T & "|進額"): Af = Af + V4 * Val(Brr(i, 6))
Z(T & "|進額") = Af
End If
Bn = Val(Brr(i, 8))
If Bn > 0 Then
B = Z(T & "|贈敘")
Z(T & "|贈數") = Z(T & "|贈數") + Bn
If B = "" Then
B = Brr(i, 1) & "項_" & Brr(i, 2) & "_贈_" & Bn
Else
B = B & " ★" & Brr(i, 1) & "項_" & Brr(i, 2) & "_贈_" & Bn
End If
Z(T & "|贈敘") = B: B = ""
End If
If V5 > 0 Then
D = Z(T & "|銷"): D = D + V5: Z(T & "|銷") = D
Df = Z(T & "|銷額"): Df = Df + V5 * Val(Brr(i, 6))
Z(T & "|銷額") = Df
End If
Next
Set Ra = Range([工作表1!S2], [工作表1!M65536].End(3)): Brr = Ra
For i = 2 To UBound(Brr)
T = Brr(i, 1)
Brr(i, 2) = Z(T & "|進")
Brr(i, 3) = Z(T & "|銷") + Z(T & "|贈數")
Brr(i, 4) = Brr(i, 2) - Brr(i, 3)
Brr(i, 5) = Z(T & "|進額")
Brr(i, 6) = Z(T & "|銷額")
Brr(i, 7) = Z(T & "|贈敘")
Next
Ra = Brr: [S2] = "備註"
Set Z = Nothing: Erase Brr: Set Ra = Nothing
End Sub作者: hcm19522 時間: 2023-6-28 11:31
Sub TEST()
Dim Brr, V4&, V5&, Z, i&, T$, A&, Af&, B$, Bn&, D&, Df, Ra As Range
Set Z = CreateObject("Scripting.Dictionary")
Set Ra = Range([工作表1!H3], [工作表1!A65536].End(3)): Brr = Ra
For i = 1 To UBound(Brr)
T = Brr(i, 3): V4 = Val(Brr(i, 4)): V5 = Val(Brr(i, 5))
If V4 > 0 Then
A = Z(T & "|進"): A = A + V4: Z(T & "|進") = A
Af = Z(T & "|進額"): Af = Af + V4 * Val(Brr(i, 6))
Z(T & "|進額") = Af
End If
Bn = Val(Brr(i, 8))
If Bn > 0 Then
B = Z(T & "|贈敘")
Z(T & "|贈數") = Z(T & "|贈數") + Bn
If B = "" Then
B = Brr(i, 1) & "項_" & Brr(i, 2) & "_贈_" & Bn
Else
B = B & " ★" & Brr(i, 1) & "項_" & Brr(i, 2) & "_贈_" & Bn
End If
Z(T & "|贈敘") = B: B = ""
End If
If V5 > 0 Then
D = Z(T & "|銷"): D = D + V5: Z(T & "|銷") = D
Df = Z(T & "|銷額"): Df = Df + V5 * Val(Brr(i, 6))
Z(T & "|銷額") = Df
End If
Next
Set Ra = Range([工作表1!V2], [工作表1!N65536].End(3)): Brr = Ra
For i = 2 To UBound(Brr)
T = Brr(i, 1)
Brr(i, 2) = Z(T & "|進")
Brr(i, 3) = Z(T & "|銷") + Z(T & "|贈數")
Brr(i, 4) = Brr(i, 2) - Brr(i, 3)
Brr(i, 5) = Z(T & "|進額")
Brr(i, 6) = Z(T & "|銷額")
Brr(i, 7) = Brr(i, 6) - Brr(i, 5)
Brr(i, 8) = "共贈出: " & Z(T & "|贈數")
Brr(i, 9) = Z(T & "|贈敘")
Next
Ra = Brr: [U2] = "備註"
Set Z = Nothing: Erase Brr: Set Ra = Nothing
End Sub作者: cypd 時間: 2023-6-28 21:15
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xR As Range
With Target
Set xR = Intersect([A:H], ActiveSheet.UsedRange)
If Not Intersect(.Cells, xR) Is Nothing Then Call TEST
End With
Set xR = Nothing
End Sub作者: 星空乂羽翼 時間: 2023-6-29 10:56