我有以下问题!
我想执行线性编程,以使用Solver对所有7到16行(总共10次)最大化目标值(Y)。但是,我不想使用Solver 10次。有没有自动化的方法来做到这一点? 例如,对于第7行,问题可以这样表达:
Max A7
subject to B7>=B2, C7>=B2, D7>=B2,
B7<=B3, C7<=B3, D7<=B3,
B7 + C7 + D7 = 100%
同样,对于第8行,问题应该像这样表达:
Max A8
subject to B8>=B2, C8>=B2, D8>=B2,
B8<=B3, C8<=B3, D8<=B3,
B8 + C8 + D8 = 100%
如您所见,每一行都具有相同的配方结构但不同的细胞。有没有办法在不使用Solver 10次的情况下实现这一目标? 我非常感谢!!!
非常感谢!
答案 0 :(得分:1)
尝试这样的事情。确保在VBA中添加解算器引用。它每次仍然会运行求解器,但您不必每次都要点击求解器并更改约束。它将在后台完成所有工作
Sub FindMaxYs()
Dim i As Integer
ActiveSheet.Cells(7, 1).Select
For i = 7 To 16
ActiveSheet.Cells(i, 1).Select
SolverReset
SolverOk SetCell:=ActiveCell, MaxMinVal:=1, ValueOf:=0, ByChange:=Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 3)), _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:=ActiveCell.Offset(0, 1), Relation:=1, FormulaText:="$B$3"
SolverAdd CellRef:=ActiveCell.Offset(0, 2), Relation:=1, FormulaText:="$B$3"
SolverAdd CellRef:=ActiveCell.Offset(0, 3), Relation:=1, FormulaText:="$B$3"
SolverAdd CellRef:=ActiveCell.Offset(0, 1), Relation:=3, FormulaText:="$B$2"
SolverAdd CellRef:=ActiveCell.Offset(0, 2), Relation:=3, FormulaText:="$B$2"
SolverAdd CellRef:=ActiveCell.Offset(0, 3), Relation:=3, FormulaText:="$B$2"
SolverAdd CellRef:=ActiveCell.Offset(0, 4), Relation:=2, FormulaText:="$B$3"
SolverSolve True
Next i
End Sub