使用 VBA 命令来处理求解器对话框
Using VBA commands to work with Solver Dialogue box
目前我正在 excel 中创建一个运行求解器的程序。我在求解器命令上设置了最大时间限制。如果程序超过时间限制,将出现一个求解器对话框,询问是继续还是停止。我想知道是否有一种方法可以将代码写入 VBA 以自动 select 停止,而不必让用户单击该选项。
提前致谢!
是的,你可以。您需要在 Solver 对象上设置一些选项。您可以阅读更多相关信息 In the MSDN documentation
SolverSolve(UserFinish, ShowRef)
UserFinish Optional Variant. True to return the results without displaying the Solver Results dialog box. False or omitted to return the results and display the Solver Results dialog box.
ShowRef Optional Variant. You can pass the name of a macro (as a string) as the ShowRef argument. This macro is then called, in lieu of displaying the Show Trial Solution dialog box, whenever Solver pauses for any of the reasons listed
您需要定义一个运行的宏,它必须接受一个整数参数。这是一个代码示例,直接来自 linked 页面(张贴在这里以防 link 将来被破坏)
您调用 SolverSolve,将上面的参数传递给它:
SolverSolve UserFinish:=True, ShowRef:= "ShowTrial"
然后您需要定义运行的 ShowTrail
宏,并且它必须具有正确的签名:
Function ShowTrial(Reason As Integer)
'Msgbox Reason <= commented out, as you just want it to end, with no user input
ShowTrial = 0 'See comment below on the return value.
End Function
return 值很重要。 Return 0 如果你想让求解器不管怎样都继续,或者 1 如果你想让它停止。
然后你可以让它因为不同的原因而有不同的行为。例如,如果达到最大时间限制(您的情况),则停止。否则,继续:
Function ShowTrial(Reason As Integer)
Select Case Reason
Case 1 '//Show iterations option set
ShowTrial = 0 '//Carry on
Exit Function
Case 2 '//Max time limit reached
ShowTrial = 1 '//Stop
Exit Function
Case 3 '//Max Iterations limit reached
ShowTrial = 0 '//Keep going
Exit Function
Case 4 '//Max subproblems limit reached
ShowTrial = 0 '//Keep Going
Exit Function
Case 5 '//Max feasible solutions limit reached
ShowTrial = 0 '//Keep going
Exit Function
End Select
End Function
目前我正在 excel 中创建一个运行求解器的程序。我在求解器命令上设置了最大时间限制。如果程序超过时间限制,将出现一个求解器对话框,询问是继续还是停止。我想知道是否有一种方法可以将代码写入 VBA 以自动 select 停止,而不必让用户单击该选项。
提前致谢!
是的,你可以。您需要在 Solver 对象上设置一些选项。您可以阅读更多相关信息 In the MSDN documentation
SolverSolve(UserFinish, ShowRef)
UserFinish Optional Variant. True to return the results without displaying the Solver Results dialog box. False or omitted to return the results and display the Solver Results dialog box.
ShowRef Optional Variant. You can pass the name of a macro (as a string) as the ShowRef argument. This macro is then called, in lieu of displaying the Show Trial Solution dialog box, whenever Solver pauses for any of the reasons listed
您需要定义一个运行的宏,它必须接受一个整数参数。这是一个代码示例,直接来自 linked 页面(张贴在这里以防 link 将来被破坏)
您调用 SolverSolve,将上面的参数传递给它:
SolverSolve UserFinish:=True, ShowRef:= "ShowTrial"
然后您需要定义运行的 ShowTrail
宏,并且它必须具有正确的签名:
Function ShowTrial(Reason As Integer)
'Msgbox Reason <= commented out, as you just want it to end, with no user input
ShowTrial = 0 'See comment below on the return value.
End Function
return 值很重要。 Return 0 如果你想让求解器不管怎样都继续,或者 1 如果你想让它停止。
然后你可以让它因为不同的原因而有不同的行为。例如,如果达到最大时间限制(您的情况),则停止。否则,继续:
Function ShowTrial(Reason As Integer)
Select Case Reason
Case 1 '//Show iterations option set
ShowTrial = 0 '//Carry on
Exit Function
Case 2 '//Max time limit reached
ShowTrial = 1 '//Stop
Exit Function
Case 3 '//Max Iterations limit reached
ShowTrial = 0 '//Keep going
Exit Function
Case 4 '//Max subproblems limit reached
ShowTrial = 0 '//Keep Going
Exit Function
Case 5 '//Max feasible solutions limit reached
ShowTrial = 0 '//Keep going
Exit Function
End Select
End Function