title: Excel Solver Ignoring Constraint in VBA
body: <p>I am trying to find a maximum return for a simple portfolio using Solver. Using Solver in the worksheet directly works sensibly, however it does not when the commands are set in VBA. Instead (as you can see from the screengrab)it ignores one of the constraints (that the sum of weights calculated in T10 should =1). Interestingly it works fine if I change the third line to say:</p>
  
  <pre><code>SolverAdd CellRef:="$T$10", Relation:=2, FormulaText:="100"
  </code></pre>
  
  <p>Or any other integer other than "1".
    (It may also be ignoring the other constraint but I cannot check this).
  The table looks like this:
  <img src="https://i.stack.imgur.com/m593V.jpg" alt="enter image description here"></p>
  
  <p>And my code is:</p>
  
  <pre><code>Sub FindRange()
  
                  SolverReset
                  SolverOk SetCell:="$T$7", MaxMinVal:=1, ValueOf:="0", ByChange:="$O$10:$R$10"
                  SolverAdd CellRef:="$T$10", Relation:=2, FormulaText:="1"
                  SolverAdd CellRef:="$O$10:$R$10", Relation:=3, FormulaText:="0"
                  SolverSolve UserFinish:=True
                  SolverFinish KeepFinal:=1
                  Range("T9").Value = Range("T7").Value
             End Sub
  </code></pre>
  
  <p>Any suggestions gratefully welcomed!</p>