Skip to main content

SolverSolve Function

Begins a Solver solution run. Equivalent to clicking Solve in the Solver Parameters dialog box.


SolverSolve( UserFinishShowRef)

UserFinish Optional VariantTrue to return the results without displaying the Solver Results dialog box. False or omitted to return the results and display the Solver Results dialog box. ShowRef Optional Variant. You can pass the name of a macro (as a string) as the ShowRef argument. This macro is then called, in lieu of displaying the Show Trial Solution dialog box, whenever Solver pauses for any of the reasons listed below. The ShowRef macro must have the signature Function name (Reason As Integer). The argument Reason is an integer value from 1 to 5:

  1. Function called (on every iteration) because the Show Iteration Results box in the Solver Options dialog box was checked, or function called because the user pressed ESC to interrupt the Solver.
  2. Function called because the Max Time limit in the Solver Options dialog box was exceeded.
  3. Function called because the Iterations limit in the Solver Options dialog box was exceeded.
  4. Function called because the Maximum Subproblems limit in the Solver Options dialog box was exceeded.
  5. Function called because the Maximum Feasible Solutions limit in the Solver Options dialog box was exceeded.

The macro function must return 1 if Solver should stop (same as the Stop button in the Show Trial Solution dialog box), or 0 if Solver should continue running (same as the Continue button).The ShowRef macro can inspect the current solution values on the worksheet, or take other actions such as saving or charting the intermediate values. However, it should not alter the values in the variable cells, or alter the formulas in the objective and constraint cells, as this could adversely affect the solution process.

SolverSolve Return Value

If a Solver problem has not been completely defined, SolverSolve returns the #N/A error value. Otherwise the Solver runs, and SolverSolve returns an integer value corresponding to the message that appears in the Solver Results dialog box:

Return Value Message
0 Solver found a solution. All constraints and optimality conditions are satisfied.
1 Solver has converged to the current solution. All constraints are satisfied.
2 Solver cannot improve the current solution. All constraints are satisfied.
3 Stop chosen when the maximum iteration limit was reached.
4 The Objective Cell values don't converge.
5 Solver could not find a feasible solution.
6 Solver stopped at user's request.
7 The linearity conditions required by this LP Solver are not satisfied.
8 The problem is too large for Solver to handle.
9 Solver encountered an error value in a target or constraint cell.
10 Stop chosen when the maximum time limit was reached.
11 There is not enough memory available to solve the problem.
13 Error in model. Please verify that all cells and constraints are valid.
14 Solver found an integer solution within tolerance. All constraints are satisfied.
15 Stop chosen when the maximum number of feasible [integer] solutions was reached.
16 Stop chosen when the maximum number of feasible [integer] subproblems was reached.
17 Solver converged in probability to a global solution.
18 All variables must have both upper and lower bounds.
19 Variable bounds conflict in binary or all different constraint.
20 Lower and upper bounds on variables allow no feasible solution.


The SolverSolve function begins the Solver solution run. Solver calls the function ShowTrial when any of the five conditions described above occurs; the function simply displays a message with the integer value 1 through 5.

Sub SolverDemo()
    Range("F4").Formula = "=SUM(C4:E4)"
    Range("F5").Formula = "=SUM(C5:E5)"
    Range("F6").Formula = "=SUM(C6:E6)"
    Range("F7").Formula = "=SUM(F4:F6)"
    SolverOptions Precision:=0.001
    SolverOK SetCell:=Range("F7"), MaxMinVal:=3, ValueOf:=50, ByChange:=Range("C4:E6")
    SolverAdd CellRef:=Range("F4:F6"), Relation:=1, FormulaText:=100
    SolverAdd CellRef:=Range("C4:E6"), Relation:=3, FormulaText:=0
    SolverAdd CellRef:=Range("C4:E6"), Relation:=4
    SolverSolve UserFinish:=False, ShowRef:="ShowTrial"
    SolverSave SaveArea:=Range("A33")
End Sub
Function ShowTrial(Reason As Integer)
    MsgBox Reason
    ShowTrial = 0
End Function

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>