Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA命令处理“解算器”对话框_Vba_Excel_Solver - Fatal编程技术网

使用VBA命令处理“解算器”对话框

使用VBA命令处理“解算器”对话框,vba,excel,solver,Vba,Excel,Solver,目前我正在excel中创建一个运行solver的程序。我已对“解算器”命令设置了最大时间限制。如果程序超过时间限制,将出现一个求解器对话框,询问是继续还是停止。我想知道是否有一种方法可以编码到VBA中,以自动选择停止,而不必让用户单击该选项 提前谢谢 是的,你可以。需要在解算器对象上设置一些选项。你可以阅读更多关于它的内容 UserFinish可选变量。如果为True,则返回结果而不显示“解算器结果”对话框。False或省略返回结果并显示“解算器结果”对话框 ShowRef可选变量。可以将宏的名

目前我正在excel中创建一个运行solver的程序。我已对“解算器”命令设置了最大时间限制。如果程序超过时间限制,将出现一个求解器对话框,询问是继续还是停止。我想知道是否有一种方法可以编码到VBA中,以自动选择停止,而不必让用户单击该选项


提前谢谢

是的,你可以。需要在解算器对象上设置一些选项。你可以阅读更多关于它的内容

UserFinish可选变量。如果为True,则返回结果而不显示“解算器结果”对话框。False或省略返回结果并显示“解算器结果”对话框

ShowRef可选变量。可以将宏的名称(作为字符串)作为ShowRef参数传递。然后,每当解算器因列出的任何原因暂停时,都会调用此宏,而不是显示“显示试用解决方案”对话框

您需要定义一个运行的宏,该宏必须采用整数参数。下面是一个代码示例,直接来自链接页面(在此处发布,以防将来链接断开)

调用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
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
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