VBA:求解器添加忽略约束
我试图为宏的每次运行设置求解器,但它似乎忽略了一些约束,但由于某种原因并非全部。
SolverReset
SolverOk SetCell:="$N$15", MaxMinVal:=3, ValueOf:=0, ByChange:=Range("i3", Range("i3").End(xlDown)), _
Engine:=3, EngineDesc:="Evolutionary"
SolverAdd CellRef:=Range("k3", Range("k3").End(xlDown)), Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$P$12:$P$13", Relation:=1, FormulaText:="$R$12:$R$13"
SolverAdd CellRef:="$P$12:$P$13", Relation:=3, FormulaText:="$N$12:$N$13"
SolverAdd CellRef:=Range("n3", Range("n3").End(xlDown)), Relation:=2, FormulaText:="1"
SolverAdd CellRef:=Range("i3", Range("i3").End(xlDown)), Relation:=5, FormulaText:="binary"
SolverSolve
它只是忽略范围 Range("n3", Range("n3").End(xlDown)) 和 Range("k3", Range("k3").End(xlDown)) 的约束
任何帮助都是赞赏。
谢谢!
I'm trying to set up solver for each run of a macro but it seems to be ignoring some of the constraints but not all of them for some reason.
SolverReset
SolverOk SetCell:="$N$15", MaxMinVal:=3, ValueOf:=0, ByChange:=Range("i3", Range("i3").End(xlDown)), _
Engine:=3, EngineDesc:="Evolutionary"
SolverAdd CellRef:=Range("k3", Range("k3").End(xlDown)), Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$P$12:$P$13", Relation:=1, FormulaText:="$R$12:$R$13"
SolverAdd CellRef:="$P$12:$P$13", Relation:=3, FormulaText:="$N$12:$N$13"
SolverAdd CellRef:=Range("n3", Range("n3").End(xlDown)), Relation:=2, FormulaText:="1"
SolverAdd CellRef:=Range("i3", Range("i3").End(xlDown)), Relation:=5, FormulaText:="binary"
SolverSolve
It is only ignoring the constraints for the ranges Range("n3", Range("n3").End(xlDown)) and Range("k3", Range("k3").End(xlDown))
Any help would be appreciated.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 XL 帮助:
“关系必需的整数。约束左侧和右侧之间的算术关系。如果选择 4 或 5,CellRef 必须引用可调整(更改)的单元格,并且 不应指定 FormulaText。”
您的最后一个范围的关系 = 5,因此您不能包含 FormulaText。
From XL Help:
"Relation Required Integer. The arithmetic relationship between the left and right sides of the constraint. If you choose 4 or 5, CellRef must refer to adjustable (changing) cells, and FormulaText should not be specified."
Your last range has relation=5, so you can't include FormulaText.
我遇到了同样的问题,关系 = 5 的范围的 FormulaText 不是问题,但问题是 FormulaText =“1”。每个具有 FormulaText="1" 的 SolverAdd 在求解器中都会被忽略。
对我来说,解决方案只是在一个单元格中添加“1”(例如 E9)并设置 FormulaText="$E$9"。
可能有更好的解决方案,但这对我来说很好......
I had same problem and FormulaText for range with relation=5 was not a problem, but problem was FormulaText="1". Every SolverAdd which had FormulaText="1" was ignored in solver.
Solution for me was just to add "1" in one cell (e.g E9) and set FormulaText="$E$9".
There is probably better solution, but this was fine for me...
可能有更好的解决方案,但这对我来说效果很好:
只需将“1”替换为“SUM(1)”即可。
There is probably better solution, but this Works fine for me :
just replace "1" with "SUM(1)" .