Hello, I am new to VBA and I am trying to create a Solver macro that loops and solves for each row until the end. My Solver code is below.
Sub ASPs()
SolverReset
SolverOK MaxMinVal:=0, ValueOf:=0, ByChange:="$AR$5:$AS$5", Engine:=1, _
EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AW$5", Relation:=2, FormulaText:="$AT$5"
SolverOK MaxMinVal:=0, ValueOf:=0, ByChange:="$AR$5:$AS$5", Engine:=1, _
EngineDesc:="GRG Nonlinear"
SolverOK MaxMinVal:=0, ValueOf:=0, ByChange:="$AR$5:$AS$5", Engine:=1, _
EngineDesc:="GRG Nonlinear"
SolverSolve UserFinish:=True
End Sub
One of the solutions I have seen is the one below but I can't get the FormulaText to continue in the Loop.
Sub ASPLoop()
Dim cellChange As Range
Dim cellGoal As Range
Dim cellConstraint As Range
Set cellChange = ActiveSheet.Range("AR5:AS5")
Set cellGoal = ActiveSheet.Range("AW5")
Set cellConstraint = ActiveSheet.Range("AT5")
Do '********* LOOP & SOLVE ***************
SolverReset
SolverOptions Precision:=0.001
SolverOK SetCell:=cellGoal.Address(True, True), _
MaxMinVal:=1, ByChange:=cellChange.Address(True, True)
SolverAdd CellRef:=cellConstraint.Address(True, True), _
Relation:=2, FormulaText:="AT5"
Solver.SolverSolve UserFinish:=True
Set cellChange = cellChange.Offset(1, 0)
Set cellGoal = cellGoal.Offset(1, 0)
Set cellConstraint = cellConstraint.Offset(1, 0)
Loop While Trim(cellGoal.Text) <> "" 'until goal cell is empty
End Sub
Bookmarks