我有一张 Excel 表,其中列出了采购订单,每个采购订单都有一个关联的案例值。例如:
3156922129 10
756941986 13
1356902364 15
3156892373 15
9156912182 17
3756902389 22
我没有发布完整的列表,但其中有 42 个上述示例。
我们现在必须手动查看采购订单数量,并得到加起来等于 75 的组合。这是我们可以在托盘上放置的箱子数量。最后,我们得到了 26 个托盘,其采购订单等于或接近 75 件的目标。有没有办法利用这个列表,让 excel 告诉你哪些采购订单等于或接近期望的 75 件目标?
答案1
使用列中发布的数据A和乙,运行这个简短的宏:
Sub kombo()
Dim N As Long, i As Long, nBits As Long
Dim s As String, j As Long, zum As Long
nBits = 6
N = 2 ^ nBits - 1
For i = 1 To N
s = Application.WorksheetFunction.Dec2Bin(i, nBits)
For j = 1 To nBits
Cells(j, 3) = Mid(s, j, 1)
Next j
zum = Evaluate("SUMPRODUCT((B1:B6)*(C1:C6))")
If zum = 75 Then Exit Sub
Next i
End Sub
将以二进制从 1 计数到 63,将 0 和 1 的模式写入列C。每个模式代表采购订单的一个子集。每个子集都加起来。当总和达到75,程序停止。
这是仅为 6 个项目编写的演示代码。要处理 42 个项目,需要修改代码以生成更大的 42 位模式集和更大的循环变量。
编辑#1:
我更新了代码,但只测试了12项目。随着项目的增加,代码变得非常慢(除非你很幸运)。代码使用了 Rick Rothstein 在此处发布的函数:
Sub kombo_2()
Dim n As Double, i As Double, nBits As Long
Dim s As String, j As Long, zum As Long
nBits = 12
n = 2 ^ nBits - 1
Range("C:C").ClearContents
Application.ScreenUpdating = False
For i = 1 To n
s = DecToBin(i, nBits)
For j = 1 To nBits
Cells(j, 3) = Mid(s, nBits - j + 1, 1)
Next j
zum = Evaluate("SUMPRODUCT((B1:B100)*(C1:C100))")
If zum = 75 Then Exit For
Next i
Application.ScreenUpdating = True
End Sub
'
' https://www.mrexcel.com/forum/excel-questions/578667-use-dec2bin-function-visual-basic-applications-edit-macro.html
'
'The DecimalIn argument is limited to 79228162514264337593543950245
' (approximately 96-bits) - large numerical values must be entered
' as a String value to prevent conversion to scientific notation. Then
' optional NumberOfBits allows you to zero-fill the front of smaller
' values in order to return values up to a desired bit level.
Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
DecToBin = ""
DecimalIn = CDec(DecimalIn)
Do While DecimalIn <> 0
DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(DecToBin) > NumberOfBits Then
DecToBin = "Error - Number too large for bit size"
Else
DecToBin = Right$(String$(NumberOfBits, "0") & DecToBin, NumberOfBits)
End If
End If
End Function