我试图在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
Solver脚本
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
答案 0 :(得分:2)
Application.Wait Now + TimeValue("00:00:02")
以上将暂停2秒