VBA:求解器添加忽略约束

发布于 2024-12-01 10:10:10 字数 778 浏览 5 评论 0原文

我试图为宏的每次运行设置求解器,但它似乎忽略了一些约束,但由于某种原因并非全部。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少钕鈤記 2024-12-08 10:10:10

来自 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.

穿透光 2024-12-08 10:10:10

我遇到了同样的问题,关系 = 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...

回忆躺在深渊里 2024-12-08 10:10:10

可能有更好的解决方案,但这对我来说效果很好:

SolverAdd CellRef:=Range("k3", Range("k3").End(xlDown)), Relation:=1, FormulaText:="SUM(1)"

只需将“1”替换为“SUM(1)”即可。

There is probably better solution, but this Works fine for me :

SolverAdd CellRef:=Range("k3", Range("k3").End(xlDown)), Relation:=1, FormulaText:="SUM(1)"

just replace "1" with "SUM(1)" .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文