我想控制vba中的excel解算器。我用它通過調整輸入值(i,8)來最小化目標單元格(i,10)的值。我還有一個用戶定義的函數,它使用單元格(i,8)作爲輸入,調用外部應用程序進行一些計算。目標單元格是UDF輸出和另一個固定值的差值。vba求解 - 爲迭代添加延遲
我發現求解器定期設置問題然後跳過它。我可以手動調整這些值以獲得更好的解決方案,所以我認爲求解器跳過一個新的迭代比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語句。 – bwindi