简体   繁体   中英

VBA ignoring constraints of reference cell in Excel Solver

I am running a macro in Excel 2013 that uses Solver for each row (one account per row) for 30 rows.

  • Column B is my Account Name.
  • Column C is "Total Cost".
  • Column E is # of Employees for Low Cost.
  • Column F is # of Employees for Medium Cost.
  • Column G is # of Employees for High Cost.
  • Column H is "Low Cost per Employee".
  • Column I is "Medium Cost per Employee".
  • Column J is "High Cost per Employee".
  • Column K is sumproduct of (E:G, H:J).
  • Column L is differential between Column C and Column K. In Solver, this is the target cell that is set to 0 by changing what columns I, and J need to be.

I have two constraints:

  1. I must be greater or equal to J.
  2. H must be greater or equal to I.

*Note that H = C-sum(I,J) so it's not an independent variable.

Question 1. for the first constraint: I have in my code:

SolverAdd CellRef:=Range("I" & RowCount), _
        Relation:=3, _
        FormulaText:=Range("J" & RowCount)

*Note that I tried googling formulaText but it seems that the ones I found only equate it to a value. I don't want a value. I want it to reference a cell. but the code I have kept getting ignored. the results would show J being greater than I. How do I solve this?

Question 2. I keep getting error when running

   SolverAdd CellRef:=Range("H" & RowCount), _
            Relation:=3, _
            FormulaText:=Range("I" & RowCount)

I'm not sure why it would fail. Is it because H is referring an equation instead of a blank cell? If so, is there any way around it?

Thank you.

have you got any further with this?

this seemed to work ok for me, though I had a hardcoded value in H

SolverReset

SolverOk SetCell:="$L$3", MaxMinVal:=3, ValueOf:=0, ByChange:="$I$3:$J$3", _
    Engine:=1, EngineDesc:="GRG Nonlinear"

SolverAdd CellRef:="$H$3", Relation:=3, FormulaText:="$I$3"

SolverAdd CellRef:="$I$3", Relation:=3, FormulaText:="$J$3"

SolverSolve

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