【发布时间】:2016-11-18 11:18:42
【问题描述】:
我正在尝试控制 vba 中的 excel 求解器。我使用它通过调整输入值 (i,8) 来最小化目标单元格 (i,10) 的值。我还有一个用户定义的函数,它使用 cell(i,8) 作为输入调用外部应用程序进行一些计算。目标单元格是UDF输出与另一个固定值的差。
我发现求解器会定期设置问题然后跳过它。我可以手动调整这些值以获得更好的解决方案,因此我认为求解器跳到新迭代的速度比 UDF 返回值的速度快。
有没有办法减慢求解器的迭代速度?如果有帮助,下面的代码...
Sub Main()
Dim Msg As String, MyString As String
Dim Style As Variant, Response As Variant
Application.ScreenUpdating = False
Application.EnableEvents = False
'Define a confirmation message due to long duration of calculations
Msg = "This calculation takes long. Do you want to proceed?"
'Define message box style
Style = vbOKCancel + vbCritical + vbDefaultButton2
'Record the user response
Response = MsgBox(Msg, Style)
If Response = vbOK Then
Call RateTool.RateSolver
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
还有求解器脚本
Sub RateSolver()
Dim First As Integer, Last As Integer
Dim i As Integer
First = Cells(2, 4).Value
Last = Cells(3, 4).Value
For i = First To Last
SolverReset
'Define parameters for the solver: Minimise target cell (i,8) by changing input cell (i,6)
SolverOk SetCell:=Cells(i, 10), MaxMinVal:=2, ByChange:=Cells(i, 8), Engine:=1
SolverSolve UserFinish:=True
Next i
End Sub
【问题讨论】:
-
对不起,我在 Main() 中取出了一些冗长的无聊代码,设置了一个按钮来启动 RateTool....因此 if response = vbOK 语句。