简体   繁体   English

VBA:使用Solver的运行时错误1004

[英]VBA: Runtime error 1004 using Solver

I'm new to VBA, and have some trouble with using the Solver in a macro. 我是VBA的新手,在宏中使用Solver时遇到一些麻烦。 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"). 我正在为Excel中的命令按钮分配一个宏,它们完美地计算所有内容,但最后也出现了一个丑陋的错误(“运行时错误1004,对象_应用程序的方法计算失败”)。

Sub serieus1()
'
' serieus1 Macro
'  

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

End Sub

I was getting the same error in my Solver macro. 我在Solver宏中遇到了同样的错误。

I tried some of the things mentioned in several Excel forums. 我尝试了几个Excel论坛中提到的一些东西。 As the comment suggests, I did add SolverFinish , and also wrapped all my SetCells to of Range type. 正如评论所暗示的那样,我确实添加了SolverFinish ,并且还将我的所有SetCells包装为Range类型。 I also saved the file and exited out of Excel. 我还保存了文件并退出了Excel。

That did the trick and the pop-up error vanished for me when I got back in. 这就是诀窍,当我回来时,弹出错误消失了。

I was getting this runtime error using Solver through the GUI, after I had been running some SolverOk 's in VBA. 在我在VBA中运行一些SolverOk之后,我通过GUI使用Solver得到了这个运行时错误。 I came across a post on MSDN, "Excel 2010 and Solver Issue," and it recommended adding a SolverReset before you run your SolverOk . 我在MSDN上发现了一篇文章“Excel 2010和Solver Issue” ,它建议在运行SolverReset之前添加一个SolverOk I ran SolverReset in the Immediate Window, and that got rid of the runtime error. 我在立即窗口中运行了SolverReset ,这消除了运行时错误。

Previously I didn't have SolverFinish as Ram Narasimhan recommended, and if I had, that might have prevented the error in the first place, but that's just conjecture. 以前我没有像Ram Narasimhan推荐的SolverFinish ,如果我有,那可能首先防止了错误,但这只是猜想。

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

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