0

我在求解器上有困难。我试图开发一个可以多次调用求解器的 for 循环,但我遇到了问题。我已经从工具/参考中将求解器的参考添加到工作簿中。我已经成功地让循环与求解器一起工作;不幸的是,求解器不会自动更改我的输入单元格。我觉得这个问题可以通过 SolverReset 函数解决,但是每次使用它都会失败,并且如果不手动重新解决系统,循环就无法找到解决方案。我的代码如下。

 Sub Go_Click() 

    'Variable Types
    Dim x As Double 
    Dim First_Variable As Range 
    Dim Second_Variable As Range 
    Dim Number_of_Calculations As Double 
    Dim Input_Cells As Range 
    Dim First_Dimension_Lower As Double 
    Dim y As Double 

    Number_of_Calculations = 6 

    x = (First_Dimension_Upper_Bound - First_Dimension_Lower_Bound) /  Number_of_Calculations 

    For y = 1 To Number_of_Calculations 

         ' x is the integer that the first dimension of the solve will increase by.
         ' by finding the difference between the upper and lower bound and dividing by the number of calculations
         ' then the rest is just a for loop, adding the value "x" to each loop

        SolverOk SetCell:="First_Dimension", _ 
        MaxMinVal:=3, _ 
        ValueOf:=First_Dimension_Lower_Bound, _ 
        ByChange:="Input_Cells", _ 
        Engine:=1, EngineDesc:="GRG Nonlinear" 
        SolverSolve 

         'Just to test the results
         MsgBox (Range("b4")) 

         'increase in the lower first dimension bound
         First_Dimension_Lower_Bound = First_Dimension_Lower_Bound + x 

     Next y 

 End Sub 
4

1 回答 1

0

查看SolverSolve的返回结果是否成功完成:

如果 Solver 问题尚未完全定义,SolverSolve 返回 #N/A 错误值。否则,求解器将运行,并且 SolverSolve 返回一个整数值,该整数值对应于求解器结果对话框中出现的消息。

链接页面列出了可能返回的整数值。例如,5 表示“求解器找不到可行的解决方案”。

您还可以提供函数名称作为ShowRef参数SolverSolve

SolverSolve(UserFinish, ShowRef)

如果 Solver 由于链接页面中列出的原因之一暂停,将调用此函数。例如,2 表示:

由于超出了“求解器选项”对话框中的“最大时间”限制而调用的函数。

于 2013-07-26T00:15:21.513 回答