I am having trouble writing code to use solver to get the correct asset allocation percentages. The problem I am having occurs when I enter my constraints. It seems that excel is only picking up 2 of my three constraints. Specifically, it is not picking up my constraint that says that the total of all assets must equal 1 (100%).
'Solve 2 - 10 Barbell
'Clears all solver constraints
SolverReset
'Sets the target cell equal to the the 5yr duration
SolverOk SetCell:="$K$44", MaxMinVal:=3, ValueOf:=Range("$K$41").Value, ByChange:= _
"$F$42,$I$42"
'Adds constraints that sum of 2 weightings must = 1 and percentages must be (>=) greater than or equal to 0.
SolverAdd CellRef:="$k$42", Relation:=2, FormulaText:="1"
SolverAdd CellRef:="$f$42", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$i$42", Relation:=3, FormulaText:="0"
'Tells the computer to solve with the information given.
SolverSolve UserFinish:=True
'Accepts the computer's solution without displaying the dialogue box
SolverFinish KeepFinal:=1
When I look in excel after I run the macro I only see the constraints that the weightings (F42, I42) must be >=0, I do not see that the sum must be equal to 1 (100%). Because I am not running the SolverReset function between when the macro completes and when I look at Solver interactively in excel I think that my constraints should still be there.
Please let me know if theres any flaw to my logic or my code. Thanks!
'Solve 2 - 10 Barbell
'Clears all solver constraints
SolverReset
'Sets the target cell equal to the the 5yr duration
SolverOk SetCell:="$K$44", MaxMinVal:=3, ValueOf:=Range("$K$41").Value, ByChange:= _
"$F$42,$I$42"
'Adds constraints that sum of 2 weightings must = 1 and percentages must be (>=) greater than or equal to 0.
SolverAdd CellRef:="$k$42", Relation:=2, FormulaText:="1"
SolverAdd CellRef:="$f$42", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$i$42", Relation:=3, FormulaText:="0"
'Tells the computer to solve with the information given.
SolverSolve UserFinish:=True
'Accepts the computer's solution without displaying the dialogue box
SolverFinish KeepFinal:=1
When I look in excel after I run the macro I only see the constraints that the weightings (F42, I42) must be >=0, I do not see that the sum must be equal to 1 (100%). Because I am not running the SolverReset function between when the macro completes and when I look at Solver interactively in excel I think that my constraints should still be there.
Please let me know if theres any flaw to my logic or my code. Thanks!