未为求解器定义 VBA Sub
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41643134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Sub not defined for Solver
提问by Val.F
The following line of code is giving me Sub or Function not defined whenever I try to use it. I've tried double checking my spelling, renaming it, looking up other questions online (most other mistakes were forgetting to add an s after sheets or worksheets), but I have no idea what's wrong with my code. I originally got this code from the macro recorder.
每当我尝试使用它时,以下代码行都会给我 Sub 或 Function not defined 。我试过仔细检查我的拼写,重命名它,在线查找其他问题(大多数其他错误是忘记在工作表或工作表后添加 s),但我不知道我的代码有什么问题。我最初是从宏记录器中获得此代码的。
I am using this sub multiple times on different sheets, but that hasn't affected my other subs that are similar.
我在不同的工作表上多次使用这个子,但这并没有影响我的其他类似的子。
Sub SolverSolver()
SolverOk SetCell:="$G", MaxMinVal:=2, ValueOf:=0, ByChange:="$G,$G", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
End Sub
Maybe the way in which it's being used is what's wrong, so I've added the way in which its called below. This line is in a large sub of simple commands like giving a cell a name, and this large simple sub also repeats multiple times. However even when attempting to run the sub by directly running it though the developer tab, it still has the same error so I don't think its the way the sub is called.
也许它的使用方式有问题,所以我在下面添加了它的调用方式。这一行在简单命令的一个大子中,比如给一个单元一个名字,这个大的简单子也重复多次。但是,即使尝试通过直接通过开发人员选项卡运行子程序来运行子程序,它仍然存在相同的错误,因此我认为这不是子程序的调用方式。
ActiveSheet.Buttons.Add(630.75, 52.5, 74.25, 26.25).OnAction = "SolverSolver"
Any help would be greatly appreciated!
任何帮助将不胜感激!
回答by cyboashu
You dont have reference set to Solver
. To use Solver functions that'
s manadatory. Set it like this: in VBA editor, Go to Tools --> References --> and select Solver. Click OK.
您没有将参考设置为Solver
. 使用强制的求解器函数。像这样设置:在 VBA 编辑器中,转到工具 --> 参考 --> 并选择求解器。单击确定。
Also make sure that you have enabled the Solver Add-In, In Excel you can enable it by File--> Options --> Add-ins , here selecet Excel Add-Ins under manage dropdown and click go. On The next screen check Sover Add-In and click OK.
还要确保您已启用 Solver Add-In,在 Excel 中,您可以通过 File--> Options --> Add-ins 启用它,这里在 manage 下拉列表下选择 Excel Add-Ins 并单击 go。在下一个屏幕上,选中 Sover Add-In,然后单击 OK。
As GSerg mentioned, if your SolverSolver sub is in a sheet module , then you need to include the module name in action param value. So if sheet1 contains the sub then
正如 GSerg 所提到的,如果您的 SolverSolver sub 位于工作表模块中,那么您需要在操作参数值中包含模块名称。所以如果 sheet1 包含 sub 那么
ActiveSheet.Buttons.Add(630.75, 52.5, 74.25, 26.25).OnAction = "Sheet1.SolverSolver"