|
Sub test()
Dim tmpV As Variant, i%, j%, outArr1() As String, outArr2() As Integer, outArr3() As Byte, resL As Byte, k%, m%, curQty As Byte, curNo%
tmpV = Range("A3:B12").Value
i = UBound(tmpV)
For i = 1 To i
k = Application.RoundUp((IIf(resL > 0, 32 - resL, 0) + tmpV(i, 2)) / 32, 0) 'resL记录托盘余数,如果有余数,用32减去它,就是本次要分配的总量
ReDim Preserve outArr1(1 To j + k) '引入k,可快速确定本次分配需要的托盘数
ReDim Preserve outArr2(1 To j + k)
ReDim Preserve outArr3(1 To j + k)
For m = 1 To k
outArr1(j + m) = tmpV(i, 1) '记录品名
curNo = curNo + IIf(resL > 0, 0, 1) '如果上次分配后有余数,则不启用新托盘
outArr3(j + m) = curNo
curQty = IIf(resL = 0, 32, resL) '上次的余数,就是本次分配的可用量,但如果上次为0,则本次可分配32
outArr2(j + m) = Application.Min(curQty, tmpV(i, 2), 32) '只能取可分配的最小数
resL = curQty - outArr2(j + m) '扣除实际分配量后,即为本次分配的余数
tmpV(i, 2) = tmpV(i, 2) - outArr2(j + m) '扣除后,重置该品名的待分配量
Next
j = j + m - 1 '行号要增加,-1,是因为循环后,m比k大1
Next
ReDim tmpV(1 To j, 1 To 3) '重写数组,一次输出,高效
For k = 1 To j
tmpV(k, 1) = outArr1(k)
tmpV(k, 2) = outArr2(k)
tmpV(k, 3) = outArr3(k)
Next
Range("H3").Resize(k - 1, 3) = tmpV '输出
End Sub
|
|