简体   繁体   中英

Set cell in solver VBA by changing more than one cell?

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

[1]:https://i.stack.imgur.com/dS7x3.jpg

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:

  • Although not strictly necessary ,it is simpler to not use ranges for SetCell and CellRef . Use strings.
  • In the SolverOK statement, you should explicitly specify the Engine .
  • In the SolverOK statement, MaxMinVal:=2 is the specification to minimize. Therefore, ValueOf is ignored and can be omitted.
  • This is a guess. You say "I am trying to ... set a cell by changing 3 other cells", but your code is changing 9 cells. In 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 .
  • Another guess, you probably want to constrain $C$5 in a similar way that you constrained $D$5 and $E$5 .
  • Another guess - Cell $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 ...

  • SolverReset can leave your Excel in a Manual Calculation state, if SolverSolve is never run.
  • I would guess that your error calculation is Column I, but the results you illustrate do not make any sense (eg qttemp - q(t)?). The value you are minimizing should be ABS(error) or error^2 - since you provided no formula, I can't tell what you have.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM