gpt4 book ai didi

excel - 在 VBA 中使用 Excel Solver 时捕获最大时间/迭代对话框

转载 作者:行者123 更新时间:2023-12-04 20:20:29 28 4
gpt4 key购买 nike

我在 VBA 循环中使用 Excel 2003 中的内置求解器来求解许多不同的问题。有时,求解器会达到最大时间或迭代限制,这会导致出现一个弹出对话框,询问用户是要继续、停止还是结束。在所有情况下,我都希望它结束​​,然后继续循环的下一行。这将防止用户每次都必须坐在那里并做出响应。

似乎有人在这里尝试了一下,但失败了:
http://www.excelforum.com/excel-programming/483175-catching-max-iterations-stop-of-solver-in-vba.html

最佳答案

这是一个示例解决方案:

它使用 SolverSolve PassThru 方法调用一个函数来处理每次迭代的求解器结果。

Option Explicit

Sub SolverExample()
Dim results

' Set up your solver here...


' Execute solve
SolverOptions StepThru:=True

results = SolverSolve(True, "SolverIteration")

Select Case results
Case 0, 1, 2
' solution found, keep final values
SolverFinish KeepFinal:=1
Case 4
'Target does not converge
'Your code here
Case 5
'Solver could not find a feasible solution
'Your code here
Case Else
'Your code
End Select
End Sub

Function SolverIteration(Reason As Integer)
' Called on each solver iteration

Const SolverContinue As Boolean = False
Const SolverStop As Boolean = True
'
Select Case Reason
Case 1
SolverIteration = False ' Continue

Case 2
' Max Time reached
SolverIteration = True ' Stop

Case 3
' Max Iterations reached
SolverIteration = True ' Stop

End Select
End Function

关于excel - 在 VBA 中使用 Excel Solver 时捕获最大时间/迭代对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3984584/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com