Excel 最适合的软件包-VBA请求
我生产15、20、26、40批次的产品Excel 最适合的软件包-VBA请求,excel,vba,excel-formula,Excel,Vba,Excel Formula,我生产15、20、26、40批次的产品 - If I need to fit 80 items in these batches, guess the best fit would be to use 2 of 40 (batches) or 4 of 20 batches.. 有没有一种方法可以在函数或公式中得到它,以便动态地获得最佳拟合,以便我知道最佳拟合,请 您可以尝试以下方法: 板材结构: Sub test() Dim rngBatches As Range, ce
- If I need to fit 80 items in these batches,
guess the best fit would be to use 2 of 40 (batches) or 4 of 20 batches..
有没有一种方法可以在函数或公式中得到它,以便动态地获得最佳拟合,以便我知道最佳拟合,请
您可以尝试以下方法:
板材结构:
Sub test()
Dim rngBatches As Range, cell As Range
Dim No_Items As Long
Dim strResults As String
With ThisWorkbook.Worksheets("Sheet1")
Set rngBatches = .Range("B1:E1")
No_Items = .Range("B2").Value
For Each cell In rngBatches
If No_Items / cell = Int(No_Items / cell) Then
If strResults = "" Then
strResults = "Option(s):" & vbNewLine & No_Items / cell & " batches of " & cell & " items."
Else
strResults = strResults & vbNewLine & No_Items / cell & " batches of " & cell & " items."
End If
End If
Next cell
MsgBox strResults
End With
End Sub
代码:
Sub test()
Dim rngBatches As Range, cell As Range
Dim No_Items As Long
Dim strResults As String
With ThisWorkbook.Worksheets("Sheet1")
Set rngBatches = .Range("B1:E1")
No_Items = .Range("B2").Value
For Each cell In rngBatches
If No_Items / cell = Int(No_Items / cell) Then
If strResults = "" Then
strResults = "Option(s):" & vbNewLine & No_Items / cell & " batches of " & cell & " items."
Else
strResults = strResults & vbNewLine & No_Items / cell & " batches of " & cell & " items."
End If
End If
Next cell
MsgBox strResults
End With
End Sub
请测试这个代码。
批次值将在“B1:E1”范围内。仅批处理值(15、20、26、40)。如果字符串“Batch”必须存在,则可以通过单元格格式(“Batch”###)添加该字符串。
它不会返回所有可能的变体。它尝试返回最小批次数方面的最佳匹配。它可能是可以改进的,但不需要它,我太懒了,也不想这么做
在模块代码的顶部放置:
Option Explicit
Private boolOff As Boolean
在那之后:
Sub testBatchOptimization()
Dim sh As Worksheet, arrB As Variant, chkb As Double
Set sh = ActiveSheet
arrB = sh.Range("B1:E1").Value
chkb = sh.Range("B2").Value
MsgBox recursiveBatch(arrB, chkb, 1)
End Sub
Function recursiveBatch(arrB As Variant, chkb As Double, levelX As Long) As String
Dim nrB As Long, nrBOld As Long, i As Long, Rez As Long, boolF As Boolean
For i = 1 To UBound(arrB, 2)
Select Case levelX
Case 1
If chkb = arrB(1, i) Then
Rez = arrB(1, i): nrB = 1: boolF = True: Exit For
ElseIf chkb Mod arrB(1, i) = 0 Then 'fix division
If nrBOld = 0 Then
nrB = chkb / arrB(1, i): _
Rez = arrB(1, i): boolF = True
Else
If nrBOld >= chkb / arrB(1, i) Then _
nrB = chkb / arrB(1, i): _
Rez = arrB(1, i): boolF = True
End If
nrBOld = chkb / arrB(1, i)
End If
Case 2
If i = UBound(arrB, 2) And chkb > arrB(1, UBound(arrB, 2)) + _
arrB(1, UBound(arrB, 2) - 1) Then recursiveBatch = recursiveBatch(arrB, chkb, 3)
If boolOff Then boolOff = False: Exit Function
If arrB(1, i) >= chkb Then
recursiveBatch = "one batch of " & arrB(1, i)
Exit Function
ElseIf arrB(1, i) < chkb And arrB(1, i + 1) And i < UBound(arrB, 2) >= chkb Then
recursiveBatch = "one batch of " & arrB(1, i + 1)
Exit Function
ElseIf arrB(1, i) + arrB(1, i + 1) >= chkb And chkb > arrB(1, UBound(arrB, 2)) Then
If i = 3 And arrB(1, i + 1) + arrB(1, 1) >= chkb Then
recursiveBatch = "1 batch of " & arrB(1, 1) & " plus 1 batch of " & arrB(1, i + 1)
Exit Function
ElseIf i = 3 And arrB(1, i + 1) + arrB(1, 2) >= chkb Then
recursiveBatch = "1 batch of " & arrB(1, 2) & " plus 1 batch of " & arrB(1, i + 1)
Exit Function
Else
recursiveBatch = "1 batch of " & arrB(1, i) & " plus 1 batch of " & arrB(1, i + 1)
Exit Function
End If
End If
Case 3
If chkb < arrB(1, 1) + arrB(1, 2) + arrB(1, 3) + arrB(1, 3) Then
If arrB(1, 4) + arrB(1, 3) + arrB(1, i) >= chkb Then
recursiveBatch = "1 batch of " & arrB(1, i) & " plus 1 batch of " & arrB(1, 3) & _
" plus 1 batch of " & arrB(1, 4)
boolOff = True: Exit Function
End If
Else
recursiveBatch = interpretCase(Rez, nrB, chkb)
boolOff = True: Exit Function
End If
End Select
Next i
If boolF Then
If Rez = 20 Or Rez = 26 Then
If nrB > 2 Then
recursiveBatch = interpretCase(Rez, nrB, chkb)
boolOff = True: Exit Function
Else
recursiveBatch = nrB & " batches of " & Rez
End If
Exit Function
ElseIf Rez = 15 Then
If nrB > 3 Then
recursiveBatch = interpretCase(Rez, nrB, chkb)
boolOff = True: Exit Function
Else
recursiveBatch = nrB & " batches of " & Rez
End If
Exit Function
End If
Else
recursiveBatch = recursiveBatch(arrB, chkb, 2)
End If
End Function
Private Function interpretCase(Rez As Long, nrB As Long, ByVal chkb As Long) As String
Dim nr40 As Long, nr26 As Long, nr20 As Long, nr15 As Long, rest As Long
nr40 = Int(chkb / 40)
rest = chkb - (nr40 * 40)
If rest <= 15 Then
interpretCase = nr40 & IIf(nr40 = 1, " batch ", " batches ") & "of 40 and 1 batch of 15"
ElseIf rest <= 20 Then
interpretCase = nr40 & IIf(nr40 = 1, " batch ", " batches ") & "of 40 and 1 batch of 20"
ElseIf rest <= 26 Then
interpretCase = nr40 & IIf(nr40 = 1, " batch ", " batches ") & "of 40 and 1 batch of 26"
Else
interpretCase = nr40 + 1 & " batches of 40"
End If
End Function
子测试批处理优化()
Dim sh作为工作表,arrB作为变量,chkb作为双精度
设置sh=ActiveSheet
arrB=sh.Range(“B1:E1”).值
chkb=sh.Range(“B2”).值
MsgBox recursiveBatch(arrB、chkb、1)
端接头
函数recursiveBatch(arrB作为变量,chkb作为Double,levelX作为Long)作为字符串
Dim nrB为长,nrBOld为长,i为长,Rez为长,布尔为布尔
对于i=1到UBound(arrB,2)
选择案例级别X
案例1
如果chkb=arrB(1,i),则
Rez=arrB(1,i):nrB=1:boolF=True:Exit For
ElseIf chkb Mod arrB(1,i)=0,然后“修复除法”
如果nrBOld=0,则
nrB=chkb/arrB(1,i):_
Rez=arrB(1,i):boolF=True
其他的
如果nrBOld>=chkb/arrB(1,i),则_
nrB=chkb/arrB(1,i):_
Rez=arrB(1,i):boolF=True
如果结束
nrBOld=chkb/arrB(1,i)
如果结束
案例2
如果i=UBound(arrB,2)且chkb>arrB(1,UBound(arrB,2))+_
arrB(1,UBound(arrB,2)-1)然后recursiveBatch=recursiveBatch(arrB,chkb,3)
如果boolOff,则boolOff=False:退出函数
如果arrB(1,i)>=chkb那么
recursiveBatch=“一批”&arrB(1,i)
退出功能
如果arrB(1,i)=chkb那么
recursiveBatch=“一批”&arrB(1,i+1)
退出功能
如果arrB(1,i)+arrB(1,i+1)>=chkb和chkb>arrB(1,UBound(arrB,2)),那么
如果i=3且arrB(1,i+1)+arrB(1,1)>=chkb,则
recursiveBatch=“1批”&arrB(1,1)和“加1批”&arrB(1,i+1)
退出功能
如果i=3且arrB(1,i+1)+arrB(1,2)>=chkb,则
recursiveBatch=“1批”&arrB(1,2)和“加1批”&arrB(1,i+1)
退出功能
其他的
recursiveBatch=“1批”&arrB(1,i)和“加1批”&arrB(1,i+1)
退出功能
如果结束
如果结束
案例3
如果chkb=chkb那么
recursiveBatch=“1批”&arrB(1,i)和“加上1批”&arrB(1,3)和_
“加1批”和arrB(1,4)
boolOff=True:退出函数
如果结束
其他的
recursiveBatch=解释案例(Rez、nrB、chkb)
boolOff=True:退出函数
如果结束
结束选择
接下来我
如果是布尔夫那么
如果Rez=20或Rez=26,则
如果nrB>2,则
recursiveBatch=解释案例(Rez、nrB、chkb)
boolOff=True:退出函数
其他的
recursiveBatch=nrB和“批次”&Rez
如果结束
退出功能
ElseIf Rez=15那么
如果nrB>3,则
recursiveBatch=解释案例(Rez、nrB、chkb)
boolOff=True:退出函数
其他的
recursiveBatch=nrB和“批次”&Rez
如果结束
退出功能
如果结束
其他的
recursiveBatch=recursiveBatch(arrB,chkb,2)
如果结束
端函数
私有函数解释器case(Rez为长,nrB为长,ByVal chkb为长)为字符串
调暗nr40为长、nr26为长、nr20为长、nr15为长、休息为长
nr40=Int(chkb/40)
rest=chkb-(nr40*40)
如果是rest,“最佳匹配”的衡量标准是什么?有哪些限制?感谢Tim的回复-没有限制(如尺寸等,没有任何限制)。。只需要最合适,以便它使用最少的批量-换句话说,如果我需要适合45个项目。。我可以用40批和一批15批。。或者两个20批次和一个15批次。如果您需要45个项目,请使用20+26,而不是20+20+15或40+15…Yea。。抱歉,我的不好..您可以使用Solver加载项来解决此类问题。