简体   繁体   English

使用VBA时Excel求解器错误

[英]Excel Solver Error while using VBA

I have the following code which is basically a macro recorded. 我有以下代码,基本上是录制的宏。 after recording the macro, I cleaned away some lines of code that were found repetitive. 记录宏后,我清除了一些重复的代码行。 When I try to run this code again I get a Sub or Function not defined error. 当我再次尝试运行此代码时,出现Sub或Function not defined错误。 I checked out other Solver VBA questions on Stackoverflow and I dont seemt to be doing anything wrong. 我检查了有关Stackoverflow的其他Solver VBA问题,但似乎没有做错任何事情。 Can anyone please help? 谁能帮忙吗? Thanks in advance. 提前致谢。

Sub Macro2()

SolverOk SetCell:="$BF$7", MaxMinVal:=2, ValueOf:=0, ByChange:="$BA$7:$BC$7", Engine:=2, EngineDesc:="Simplex LP"

SolverAdd CellRef:="$BA$7", Relation:=4, FormulaText:="integer"
SolverAdd CellRef:="$BA$7", Relation:=3, FormulaText:="0"

SolverAdd CellRef:="$BB$7", Relation:=4, FormulaText:="integer"
SolverAdd CellRef:="$BB$7", Relation:=3, FormulaText:="0"

SolverAdd CellRef:="$BC$7", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$BC$7", Relation:=4, FormulaText:="integer"

SolverAdd CellRef:="$BE$7", Relation:=3, FormulaText:="0"

SolverOk SetCell:="$BF$7", MaxMinVal:=2, ValueOf:=0, ByChange:="$BA$7:$BC$7", Engine:=2, EngineDesc:="Simplex LP"

SolverSolve
End Sub

The Solver is an add in, so it won't work in your vba code until you enable it. Solver是一个插件,因此在启用它之前,它不会在您的vba代码中起作用。 I use 07, so you click office button -> options -> add-ins -> manage add ins to enable it. 我使用07,所以您单击Office按钮->选项->加载项->管理加载项以启用它。 Not sure about other versions. 不确定其他版本。

Once you have enabled the add-in, you must then connect the add-in to the vba editor. 启用外接程序后,必须将外接程序连接到vba编辑器。 Open the VBA Editor (Alt+F11), then go to Tools -> References, then check off "SOLVER" and hit OK. 打开VBA编辑器(Alt + F11),然后转到“工具”->“引用”,然后选中“求解器”并单击“确定”。

see this MSDN 看到这个MSDN

http://msdn.microsoft.com/en-us/library/office/ff196600%28v=office.15%29.aspx http://msdn.microsoft.com/en-us/library/office/ff196600%28v=office.15%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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