SolverAdd 函数忽略了我的约束
SolverAdd function is ignoring my constraints
我一直在尝试为我试图找到解决方案的特定问题添加约束,但 SolverAdd 只是忽略了我的代码。我正在编写一个宏,因为这个动作需要尽可能简单地完成,我正在考虑将这个宏链接到一个 ActiveX 按钮。
我已经尝试在我的限制中添加引号,以及删除它们,但这并不能解决问题。我也关闭了工作簿并重新打开它,但它仍然不起作用。此外,我试过在工作簿的单元格中设置我的约束值,并用范围引用它们,但它也不起作用。
我最后启用了 Solver DialogBox,它声称我的所有约束都得到满足,但事实并非如此。
Sub OptimizePrice()
Application.ScreenUpdating = False
SolverReset
SolverOk SetCell:=Range("N64"), _ 'Gross Profit
MaxMinVal:=1, _
ByChange:=Range("E59,I59,M59"), _ 'Prices that need to be optimized
Engine:=1
SolverAdd cellRef:=Range("E59,I59,M59"), _
relation:=1, _
formulaText:=Range("H30") ' Tried 80 and "80"
SolverAdd cellRef:=Range("E59,I59,M59"), _
relation:=3, _
formulaText:=Range("H29") 'Tried 50 and "50"
SolverSolve 'userFinish:=True
Application.ScreenUpdating = True
End Sub
我希望得到 50 到 80 之间的值,但我得到的值高达 137
好的。没想到这么奇怪。这是我的解决方案:
Sub OptimizePrice()
Application.ScreenUpdating = False
SolverReset
SolverOk SetCell:=Range("N64"), _ 'Gross Profit
MaxMinVal:=1, _
ByChange:=Range("E59,I59,M59"), _
Engine:=1
'Lower limits (H29=50). One constraint per cell.
SolverAdd CellRef:=Range("E59"), _
Relation:=3, _
FormulaText:=Range("H29")
SolverAdd CellRef:=Range("I59"), _
Relation:=3, _
FormulaText:=Range("H29")
SolverAdd CellRef:=Range("M59"), _
Relation:=3, _
FormulaText:=Range("H29")
'Upper Limits (H30=80). Again, one constraint per cell.
SolverAdd CellRef:=Range("E59"), _
Relation:=1, _
FormulaText:=Range("H30")
SolverAdd CellRef:=Range("I59"), _
Relation:=1, _
FormulaText:=Range("H30")
SolverAdd CellRef:=Range("M59"), _
Relation:=1, _
FormulaText:=Range("H30") '
SolverSolve userFinish:=True
Application.ScreenUpdating = True
End Sub
我一直在尝试为我试图找到解决方案的特定问题添加约束,但 SolverAdd 只是忽略了我的代码。我正在编写一个宏,因为这个动作需要尽可能简单地完成,我正在考虑将这个宏链接到一个 ActiveX 按钮。
我已经尝试在我的限制中添加引号,以及删除它们,但这并不能解决问题。我也关闭了工作簿并重新打开它,但它仍然不起作用。此外,我试过在工作簿的单元格中设置我的约束值,并用范围引用它们,但它也不起作用。
我最后启用了 Solver DialogBox,它声称我的所有约束都得到满足,但事实并非如此。
Sub OptimizePrice()
Application.ScreenUpdating = False
SolverReset
SolverOk SetCell:=Range("N64"), _ 'Gross Profit
MaxMinVal:=1, _
ByChange:=Range("E59,I59,M59"), _ 'Prices that need to be optimized
Engine:=1
SolverAdd cellRef:=Range("E59,I59,M59"), _
relation:=1, _
formulaText:=Range("H30") ' Tried 80 and "80"
SolverAdd cellRef:=Range("E59,I59,M59"), _
relation:=3, _
formulaText:=Range("H29") 'Tried 50 and "50"
SolverSolve 'userFinish:=True
Application.ScreenUpdating = True
End Sub
我希望得到 50 到 80 之间的值,但我得到的值高达 137
好的。没想到这么奇怪。这是我的解决方案:
Sub OptimizePrice()
Application.ScreenUpdating = False
SolverReset
SolverOk SetCell:=Range("N64"), _ 'Gross Profit
MaxMinVal:=1, _
ByChange:=Range("E59,I59,M59"), _
Engine:=1
'Lower limits (H29=50). One constraint per cell.
SolverAdd CellRef:=Range("E59"), _
Relation:=3, _
FormulaText:=Range("H29")
SolverAdd CellRef:=Range("I59"), _
Relation:=3, _
FormulaText:=Range("H29")
SolverAdd CellRef:=Range("M59"), _
Relation:=3, _
FormulaText:=Range("H29")
'Upper Limits (H30=80). Again, one constraint per cell.
SolverAdd CellRef:=Range("E59"), _
Relation:=1, _
FormulaText:=Range("H30")
SolverAdd CellRef:=Range("I59"), _
Relation:=1, _
FormulaText:=Range("H30")
SolverAdd CellRef:=Range("M59"), _
Relation:=1, _
FormulaText:=Range("H30") '
SolverSolve userFinish:=True
Application.ScreenUpdating = True
End Sub