I am trying to use solver in Excel VBA to set a cell by changing 3 other cells. However, the three cells don't change by the values that I choose (I put a solver add in for every colomn to be bigger and lesser than specific value), it seemes like the solver can't read what I have given to it.
I need to know how to make it read the three constant and make the changes based on them.
Sub solver()
For i = 5 To 48
SolverReset
objstring = "$I$" & i
SolverOk SetCell:=Range(objstring), MaxMinVal:=2, ValueOf:=0, ByChange:=Range("C5:E7")
SolverAdd CellRef:="$C$5:$C$48", Relation:=1, FormulaText:=934000
SolverAdd CellRef:="$C$5:$C$48", Relation:=3, FormulaText:=953000
SolverAdd CellRef:=Range("$D$" & i), Relation:=1, FormulaText:="$B$13"
SolverAdd CellRef:=Range("$D$" & i), Relation:=3, FormulaText:="$B$14"
SolverAdd CellRef:=Range("$E$" & i), Relation:=1, FormulaText:="$B$15"
SolverAdd CellRef:=Range("$E$" & i), Relation:=3, FormulaText:="$B$16"
SolverSolve userfinish:=True
Next i
End Sub
I think your set up has other problems, but it's hard to tell without seeing the rows/columns in your screen cap, or knowing the formula - specifically for column I.
However, for the problem you outline:
SetCell
and CellRef
. Use strings. SolverOK
statement, you should explicitly specify the Engine
. SolverOK
statement, MaxMinVal:=2
is the specification to minimize. Therefore, ValueOf
is ignored and can be omitted. SolverOK
, where you have ByChange:="C5:E7"
, I suspect you want ByChange:="C5:E5"
, and that you want the row to increment with the counter i
. $C$5
in a similar way that you constrained $D$5
and $E$5
. $B$13
contains the value 5000 which is your minimum value for Pi. Cell $B$14
contains the value 8000 which is your maximum value. In SolverAdd
, Relation:=1
is equivalent to "less than or equal to" or "<=". Relation:=3
is equivalent to ">=". You want to be "<=" your maximum value, and ">=" your minimum value ... you have your constraints specified backwards. All, as illustrated below ...
Sub solver()
For i = 5 To 48
SolverReset
SolverOk SetCell:="$I$" & i, MaxMinVal:=2, ByChange:="$C$" & i & ":$E$" & i, Engine:=1
SolverAdd CellRef:="$C$" & i, Relation:=1, FormulaText:="$B$18"
SolverAdd CellRef:="$C$" & i, Relation:=3, FormulaText:="$B$17"
SolverAdd CellRef:="$D$" & i, Relation:=1, FormulaText:="$B$14"
SolverAdd CellRef:="$D$" & i, Relation:=3, FormulaText:="$B$13"
SolverAdd CellRef:="$E$" & i, Relation:=1, FormulaText:="$B$16"
SolverAdd CellRef:="$E$" & i, Relation:=3, FormulaText:="$B$15"
SolverSolve userfinish:=True
Next i
End Sub
Other potential problems ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.