|
供参考,结果和楼主不一样
010820250109.zip
(150.57 KB, 下载次数: 6)
Sub test4() 'by yynrzwh 20250109
With Sheet1
lr = .Cells(Rows.Count, 1).End(3).Row
.[c2].Resize(lr - 1).ClearContents
ar = .[a1].CurrentRegion
br = .[e1].CurrentRegion
cr = .[k1].CurrentRegion
End With
Set d = VBA.CreateObject("scripting.dictionary")
Set ljyl = VBA.CreateObject("scripting.dictionary")
For i = 2 To UBound(cr)
ljyl(cr(i, 1)) = Array(cr(i, 2), cr(i, 3), cr(i, 4))
Next
For i = 2 To UBound(br)
s = br(i, 1)
If Not d.exists(s) Then
Set d(s) = VBA.CreateObject("scripting.dictionary")
End If
d(s)(br(i, 2)) = br(i, 3)
Next
'评估成品价值,从大到小排序
ReDim tar(1 To lr - 1, 1 To 2)
For i = 2 To UBound(ar)
If ar(i, 2) > 0 Then
xsum = 0
s = ar(i, 1)
For Each k In d(s).keys
xsum = xsum + d(s)(k) * ljyl(k)(2)
Next
tar(i - 1, 1) = i
tar(i - 1, 2) = xsum
End If
Next
Call xSort(tar)
'Sheet2.Range("a2").Resize(UBound(tar), 2) = tar
zjz = 0
'按照成品价值,从大到小调整QTY
For i = 1 To UBound(tar)
r = tar(i, 1)
If r > 0 Then
s = ar(r, 1)
'从大到小尝试,如果料件QTY刚好=<余量,则停止
For x = ar(r, 2) To 0 Step -1
If check(s, x, d, ljyl) Then
ar(r, 3) = x
Exit For
End If
Next
'更新料件QTY
For Each k In d(s).keys
t = ljyl(k)
t(1) = t(1) + x * d(s)(k)
ljyl(k) = t
zjz = zjz + x * d(s)(k) * ljyl(k)(2)
Next
End If
Next
Sheet1.[n122] = zjz
Sheet1.[a1].CurrentRegion = ar
End Sub
Private Function check(s, x, d, ljyl) As Boolean
For Each k In d(s).keys
t = ljyl(k)
t(1) = t(1) + x * d(s)(k)
If t(1) > t(0) Then
check = False
Exit Function
End If
Next
check = True
End Function
Sub xSort(arr)
For i1 = 1 To UBound(arr) - 1
For i2 = i1 + 1 To UBound(arr)
If arr(i2, 2) > arr(i1, 2) Then
tem = arr(i2, 2)
arr(i2, 2) = arr(i1, 2)
arr(i1, 2) = tem
tem = arr(i2, 1)
arr(i2, 1) = arr(i1, 1)
arr(i1, 1) = tem
End If
Next
Next
End Sub
|
|