Excel Solver Ignoring Constraint in VBA

前端 未结 3 968
我寻月下人不归
我寻月下人不归 2021-01-21 01:29

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

相关标签:
3条回答
  • 2021-01-21 02:24

    I think there is a bug here whenever the value is exactly 1. Other postings state that the above solution (putting the value into a cell) is unreliable. I have found it does not work a my code always refers to a cell which holds the constraint limit. My (crude) solution is to shift the limit value by 1 part in 10^12 or lower in the appropriate direction which sort of makes the constraint into a < or > rather than a <= or >=. So rather than:

    SolverAdd CellRef:=Range("SolverParam").Address, Relation:=3, _ FormulaText:=Range("SolverConstraint").value

    Use:

    SolverAdd CellRef:=Range("SolverParam").Address, Relation:=3, _ FormulaText:=Range("SolverConstraint").value + Abs(Range("SolverConstraint").value) * 1e-12

    And use the opposite sign for Relation:=1

    In this trivial example SolverParam is a single cell parameter to be adjusted and SolverConstraint is a single cell lower bound.

    This is the only consistent approach I can foresee to have a uniform handling of all values

    On further looking I found another solution from the web

    FormulaText:="=" & Range("SolverConstraint").value

    Seems to work reliably

    0 讨论(0)
  • 2021-01-21 02:33

    I had the exact same issue. I solved it in the following way: Just type FormulaText:=1 without the quotes for 1.

    0 讨论(0)
  • 2021-01-21 02:34

    Found a work around for the bug. For the flag "FormulaText:=1". instead of using 1, use a reference to any cell with the value 1 instead.

    I.e., Change "FormulaText:=1" to "FormulaText:=$H$5" where $H$5's value is 1

    0 讨论(0)
提交回复
热议问题