VBA 求解器循环
VBA Solver loop
我正在使用具有以下代码的规划求解:
Sub Test()
SolverReset
SolverOk SetCell:="$K", MaxMinVal:=1, ValueOf:=0, ByChange:="$I:$J", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$G", Relation:=2, FormulaText:="$H"
SolverAdd CellRef:="$K", Relation:=2, FormulaText:="$B"
SolverSolve UserFinish:=False
SolverFinish KeepFinal:=1
End Sub
我现在需要将它放入一个循环中,以便 运行 从第 7 行求解到第 17 行。我按照以下代码对其进行了编码,但它不起作用:
Dim i As Long
For i = 7 To 17
SolverReset
SolverOk SetCell:="$K$" & i, MaxMinVal:=1, ValueOf:=0, ByChange:="$I$ & i:$J$ & i", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$G$" & i, Relation:=2, FormulaText:="$H$" & i
SolverAdd CellRef:="$K$" & i, Relation:=2, FormulaText:="$B$" & i
SolverSolve UserFinish:=False
SolverFinish KeepFinal:=1
Next i
End sub
也许...
Dim i As Long
For i = 7 To 17
SolverReset
With Rows(i)
SolverOk SetCell:=.Range("K1").Address, _
MaxMinVal:=1, _
ByChange:=.Range("I1:J1").Address, _
Engine:=1
SolverAdd CellRef:=.Range("G1").Address, _
Relation:=2, _
FormulaText:=.Range("H1").Address
SolverAdd CellRef:=.Range("K1").Address, _
Relation:=2, _
FormulaText:=.Range("B1").Address
SolverSolve UserFinish:=True
End With
Next i
我正在使用具有以下代码的规划求解:
Sub Test()
SolverReset
SolverOk SetCell:="$K", MaxMinVal:=1, ValueOf:=0, ByChange:="$I:$J", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$G", Relation:=2, FormulaText:="$H"
SolverAdd CellRef:="$K", Relation:=2, FormulaText:="$B"
SolverSolve UserFinish:=False
SolverFinish KeepFinal:=1
End Sub
我现在需要将它放入一个循环中,以便 运行 从第 7 行求解到第 17 行。我按照以下代码对其进行了编码,但它不起作用:
Dim i As Long
For i = 7 To 17
SolverReset
SolverOk SetCell:="$K$" & i, MaxMinVal:=1, ValueOf:=0, ByChange:="$I$ & i:$J$ & i", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$G$" & i, Relation:=2, FormulaText:="$H$" & i
SolverAdd CellRef:="$K$" & i, Relation:=2, FormulaText:="$B$" & i
SolverSolve UserFinish:=False
SolverFinish KeepFinal:=1
Next i
End sub
也许...
Dim i As Long
For i = 7 To 17
SolverReset
With Rows(i)
SolverOk SetCell:=.Range("K1").Address, _
MaxMinVal:=1, _
ByChange:=.Range("I1:J1").Address, _
Engine:=1
SolverAdd CellRef:=.Range("G1").Address, _
Relation:=2, _
FormulaText:=.Range("H1").Address
SolverAdd CellRef:=.Range("K1").Address, _
Relation:=2, _
FormulaText:=.Range("B1").Address
SolverSolve UserFinish:=True
End With
Next i