Sub solve_it()
Dim Result As Long
Dim y As Long
Dim str1 As String
y = Sheets("Sheet1").[A50]
str1 = "$D$6:$D$" & y
'reset Solver
Application.Run "Solver.xla!Auto_Open"
Application.Run "Solver.xla!SolverReset"
'set up new analysis
Application.Run "Solver.xla!SolverOk", "$D$3", 1, "0", str1
'add constraints
Application.Run "Solver.xla!SolverAdd", str1, 3, "0"
Application.Run "Solver.xla!SolverAdd", str1, 1, "0.035"
Application.Run "Solver.xla!SolverAdd", "$D$50", 2, "0.2835"
'run the analysis
Result = Application.Run("Solver.xla!SolverSolve", True)
'finish the analysis
Application.Run "Solver.xla!SolverFinish"
'report on success of analysis
Select Case Result
Case Is = 0
Str = "Solution found, optimality and constraints satisfied"
Case Is = 1
Str = "Converged, constraints satisfied"
Case Is = 2
Str = "Cannot improve, constraints satisfied"
Case Is = 3
Str = "Stopped at maximum iteration"
Case Is = 4
Str = "Solver did not converge"
Beep
Case Is = 5
Str = "No feasible solution"
Beep
End Select
'report result of running Solver
MsgBox Str
End Sub
|