Sub ex()
Dim X%, Y%, K%, Z%, Count%, Check$, a As Object, d As Object
Set d = CreateObject("Scripting.Dictionary")
Sheets("附表1").UsedRange.ClearContents
Sheets("附表1").[a1:L1] = Array("Line", "RP", "CP", "job 1", "job 2", "job 3", "item", " ", "f", "t", "QTY", "group")
For Each a In Sheets("要處理的資料").Range([L2], [L2].End(4))
d(a.Value) = a.Offset(, 1)
Next
For Each a In Sheets("要處理的資料").Range([D2], [D65535].End(3))
If Check = "" Then
Check = a.Value
ElseIf Check <> a.Value Then
Z = 4 - (Count Mod 4)
Count = 0
Check = a.Value
End If
If d.exists(a.Value) Then
With Sheets("附表1")
K = 1: X = 0
If a.Offset(, 1) > d(a.Value) Then
For Y = 1 To WorksheetFunction.Quotient(a.Offset(, 1), d(a.Value))
a.Offset(, -3).Resize(, 3).Copy .[a65535].End(3).Offset(1 + Z).Resize(, 3)
.[g65535].End(3).Offset(1 + Z).Resize(, 6) = Array(a, a.Offset(, 1), K, X + d(a.Value), d(a.Value), a.Offset(, 5))
K = .[I65535].End(3) + d(a.Value)
X = .[J65535].End(3)
Z = 0
Next
a.Offset(, -3).Resize(, 3).Copy .[a65535].End(3).Offset(1 + Z).Resize(, 3)
.[g65535].End(3).Offset(1 + Z).Resize(, 6) = Array(a, a.Offset(, 1), d(a.Value) + .[I65535].End(3), a.Offset(, 1), a.Offset(, 1) Mod d(a.Value), a.Offset(, 5))
Count = Y + Count
Else
a.Offset(, -3).Resize(, 3).Copy .[a65535].End(3).Offset(1 + Z).Resize(, 3)
.[g65535].End(3).Offset(1 + Z).Resize(, 6) = Array(a, a.Offset(, 1), 1, a.Offset(, 1), a.Offset(, 1), a.Offset(, 5))
Count = Count + 1
Z = 0
End If
End With
End If
Next
Set d = Nothing
End Sub作者: 准提部林 時間: 2021-3-13 10:34
Option Explicit
Sub TEST()
Dim Brr, Crr, Z, V&, Q&, R&, i&, ii&, j%, T$, T1$
Set Z = CreateObject("Scripting.Dictionary")
Brr = Range([M2], [L65536].End(xlUp))
For i = 1 To UBound(Brr): Z(Format(Brr(i, 1), "000")) = Val(Brr(i, 2)): Next
Brr = Range([I1], [A65536].End(xlUp)(2))
ReDim Crr(1 To 10000, 1 To 9)
For j = 1 To 9: Crr(1, j) = Brr(1, j): Next: R = 1
For i = 2 To UBound(Brr) - 1
T = Format(Brr(i, 4), "000"): T1 = Format(Brr(i + 1, 4), "000")
V = Z(T): Q = Brr(i, 8)
For ii = 0 To Q \ V
R = R + 1
For j = 1 To 9: Crr(R, j) = Brr(i, j): Next
Crr(R, 4) = "'" & T
Crr(R, 6) = V * ii + 1
Crr(R, 7) = V * (ii - (ii <> Q \ V)) - (ii = Q \ V) * (Q Mod V)
Crr(R, 8) = V * -(ii <> Q \ V) - (ii = Q \ V) * (Q Mod V)
Next
If T <> T1 Then R = (((R - 1) \ 4) - ((R - 1) Mod 4 <> 0)) * 4 + 1
Next
Workbooks.Add
[A1].Resize(R, 9) = Crr
Set Z = Nothing: Erase Brr, Crr
End Sub