【问题标题】:vba solver - adding a delay to the iterationsvba 求解器 - 为迭代添加延迟
【发布时间】: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 语句。

标签: excel solver vba


【解决方案1】:
Application.Wait Now + TimeValue("00:00:02")

上面会暂停2秒

【讨论】:

  • 谢谢。这有助于引入延迟。不幸的是,这仍然没有解决问题,所以似乎时间延迟可能不是我的 UDF 的问题。这适用于一个简单的方程式,但是当我介绍控制外部应用程序的 UDF 时,它的运行并不顺畅。回到开头,我猜。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2015-01-14
  • 1970-01-01
  • 2017-04-18
  • 2020-05-27
  • 1970-01-01
  • 2011-07-16
相关资源
最近更新 更多