Skip to main content

SolverAdd Function

Table of contents
  1. Syntax
  2. Remarks
  3. Example

Adds a constraint to the current problem. Equivalent to clicking Solver in the Data | Analyze group and then clicking Add in the Solver Parameters dialog box.

Syntax

SolverAdd( CellRefRelationFormulaText)

CellRef Required Variant. A reference to a cell or a range of cells that forms the left side of a constraint. Relation Required Integer. The arithmetic relationship between the left and right sides of the constraint. If you choose 4, 5, or 6, CellRef must refer to decision variable cells, and FormulaText should not be specified.

Relation Arithmetic relationship
1 <=
2 =
3 >=
4 Cells referenced by CellRef must have final values that are integers.
5 Cells referenced by CellRef must have final values of either 0 (zero) or 1.
6 Cells referenced by CellRef must have final values that are all different and integers.

FormulaText Optional Variant. The right side of the constraint.

Remarks

After constraints are added, you can manipulate them with the SolverChange and SolverDelete functions.

Example

The SolverAdd function is used to add three constraints to the current problem.

Sub SolverDemo()
    Worksheets("Sheet1").Activate
    Range("F4").Formula = "=SUM(C4:E4)"
    Range("F5").Formula = "=SUM(C5:E5)"
    Range("F6").Formula = "=SUM(C6:E6)"
    Range("F7").Formula = "=SUM(F4:F6)"
    SolverReset
    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
    SolverSave saveArea:=Range("A33")
End Sub

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>