VBA:使用求解器时出现运行时错误 1004

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21968981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:05:25  来源:igfitidea点击:

VBA: Runtime error 1004 using Solver

excelvba

提问by blah_crusader

I'm new to VBA, and have some trouble with using the Solver in a macro. I'm assigning a macro to a command button in Excel, and they calculate everything perfectly, but an ugly error shows up as well at the end ("Runtime error 1004, method calculation of object _application failed").

我是 VBA 新手,在宏中使用求解器时遇到了一些麻烦。我正在为 Excel 中的命令按钮分配一个宏,他们完美地计算了所有内容,但最后也出现了一个丑陋的错误(“运行时错误 1004,对象 _application 的方法计算失败”)。

Sub serieus1()
'
' serieus1 Macro
'  

'
    SolverReset
    SolverAdd CellRef:="$R", Relation:=2, FormulaText:="1"
    SolverAdd CellRef:="$L", Relation:=2, FormulaText:="$B"
    SolverOk SetCell:="$L", MaxMinVal:=2, ValueOf:=0, ByChange:="$L:$Q", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve

End Sub

回答by Ram Narasimhan

I was getting the same error in my Solver macro.

我在我的求解器宏中遇到了同样的错误。

I tried some of the things mentioned in several Excel forums. As the comment suggests, I did add SolverFinish, and also wrapped all my SetCellsto of Rangetype. I also saved the file and exited out of Excel.

我尝试了几个 Excel 论坛中提到的一些内容。正如评论所暗示的那样,我确实添加了SolverFinish,并且还包装了我所有的SetCellstoRange类型。我还保存了文件并退出了 Excel。

That did the trick and the pop-up error vanished for me when I got back in.

这样做就成功了,当我重新进入时,弹出错误对我来说消失了。

回答by Will Ediger

I was getting this runtime error using Solver through the GUI, after I had been running some SolverOk's in VBA. I came across a post on MSDN, "Excel 2010 and Solver Issue,"and it recommended adding a SolverResetbefore you run your SolverOk. I ran SolverResetin the Immediate Window, and that got rid of the runtime error.

SolverOk在 VBA 中运行 some 's之后,我通过 GUI 使用 Solver 收到此运行时错误。我在 MSDN 上看到了一篇文章“Excel 2010 和求解器问题”,它建议SolverReset在运行SolverOk. 我SolverReset在立即窗口中运行,并消除了运行时错误。

Previously I didn't have SolverFinishas Ram Narasimhan recommended, and if I had, that might have prevented the error in the first place, but that's just conjecture.

以前我没有SolverFinish像 Ram Narasimhan 推荐的那样,如果我有,那可能首先可以防止错误,但这只是猜测。