vba 求解器 - 为迭代添加延迟

vba solver - adding a delay to the iterations

我正在尝试控制 vba 中的 excel 解算器。我使用它通过调整输入值 (i,8) 来最小化目标单元格 (i,10) 的值。我还有一个用户定义的函数,它调用外部应用程序进行某些计算,使用 cell(i,8) 作为输入。目标单元格是 UDF 输出与另一个固定值的差值。

我发现求解器会定期设置问题然后跳过它。我可以手动调整值以获得更好的解决方案,因此我认为求解器比 UDF returns 更快地跳到新的迭代。

有没有办法减慢求解器的迭代速度?下面的代码如果有帮助...

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
Application.Wait Now + TimeValue("00:00:02")

以上会暂停2秒