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:
SolverAdd CellRef:="$T$10", Relation:=2, FormulaText:="100"
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:

And my code is:
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
Any suggestions gratefully welcomed!
CellRef's before theSolverOK?FormulaText:=1? (without the double quotes) I mention it because the code on MSDN doesn't use quotes.