Deleting Solver Constraints in VBA
Posted by Dan Henry on June 07, 2001 2:45 PM
I wrote the following macro to automate the Solver:
SolverOk SetCell:=ActiveCell.Offset(0, 0), MaxMinVal:=3, ValueOf:="0.95", ByChange:=ActiveCell.Offset(0, -1)
SolverAdd CellRef:=ActiveCell.Offset(0, -1), Relation:=1, FormulaText:=ActiveCell.Offset(-1, -1)
SolverAdd CellRef:=ActiveCell.Offset(0, -1), Relation:=3, FormulaText:=ActiveCell.Offset(0, -3)
SolverSolve
SolverDelete CellRef:=ActiveCell.Offset(0, -1), Relation:=1, FormulaText:=ActiveCell.Offset(-1, -1)
SolverDelete CellRef:=ActiveCell.Offset(0, -1), Relation:=3, FormulaText:=ActiveCell.Offset(0, -3)
When run, the macro adds the two constraints called for by the SolverAdd lines (except that it replaces the cell address with the cell value in the right-hand side of the constraint -- for example, instead of "$B$4<=$B$3", I get "$B$4<=0.2512"; it works fine, but it's kind of unexpected).
My problem is that the SolverDelete lines don't delete the constraints. If I run the macro over and over, I end up with a long list of constraints, which I have to delete by hand. Anyone figured out how to delete solver constraints using VBA?
Thanks