Excel VBA 求解器循环 - 如何标记求解器找不到可行解的行

Excel VBA solver loop - How can I flag rows where solver could not find a feasible solution

我正在使用 VBA 遍历数百行,其中 Objective 个单元格将使用 Solver 最小化。

求解器通常无法找到解决方案,但没有提供任何信息来表明 Objective 单元格是否已合理最小化,或者求解器是否因为找不到可行的解决方案而过早退出。

我正在使用 SolverSolve UserFinish:=TrueSolverFinish KeepFinal:=1 来避免点击数百个对话框。

理想情况下,我希望能够 return Objective 单元格旁边的某种标志,以指示求解器是否能够找到解决方案。

下面是我的代码示例:

For i = iStart To iStop

'clear solver
    SolverReset

'set up formula
    SolverOk SetCell:=Range("$EN").Offset(i, 0).Address, MaxMinVal:=2, ValueOf:=0, ByChange:= _
        Range("$U:$AE,$BX:$CI,$CJ:$CU").Offset(i, 0).Address, _
        Engine:=2, EngineDesc:="GRG Nonlinear"

'set parameters

    'set variables as integers

        'number of each model
        SolverAdd CellRef:=Range("$U:$AE").Offset(i, 0).Address, _
            Relation:=4, FormulaText:="integer"
        'mono allocations
        SolverAdd CellRef:=Range("$BX:$CI").Offset(i, 0).Address, _
            Relation:=4, FormulaText:="integer"
        'colour allocations
        SolverAdd CellRef:=Range("$CJ:$CU").Offset(i, 0).Address, _
            Relation:=4, FormulaText:="integer"

    'set mono/colour max >= Optimal mono/colour click allocation
        SolverAdd CellRef:=Range("$EI").Offset(i, 0).Address, _
            Relation:=3, FormulaText:=Range("$EK").Offset(i, 0).Address

    'set colour max >= Optimal total colour click allocation
        SolverAdd CellRef:=Range("$EJ").Offset(i, 0).Address, _
            Relation:=3, FormulaText:=Range("$EM").Offset(i, 0).Address

'solve and avoid popups
    SolverSolve UserFinish:=True
    SolverFinish KeepFinal:=1

Next i

函数SolverSolvereturns一个结果可以用来决定结果

来自 MSDN:

If a Solver problem has not been completely defined, SolverSolve returns the #N/A error value. Otherwise the Solver runs, and SolverSolve returns an integer value corresponding to the message that appears in the Solver Results dialog box:

0 = Solver found a solution. All constraints and optimality conditions are satisfied.
1 = Solver has converged to the current solution. All constraints are satisfied.
2 = Solver cannot improve the current solution. All constraints are satisfied.
3 = Stop chosen when the maximum iteration limit was reached.
4 = The Objective Cell values do not converge.
5 = Solver could not find a feasible solution.
6 = Solver stopped at user's request.
7 = The linearity conditions required by this LP Solver are not satisfied.
8 = The problem is too large for Solver to handle.
9 = Solver encountered an error value in a target or constraint cell.
10 = Stop chosen when the maximum time limit was reached.
11 = There is not enough memory available to solve the problem.
13 = Error in model. Please verify that all cells and constraints are valid.
14 = Solver found an integer solution within tolerance. All constraints are satisfied.
15 = Stop chosen when the maximum number of feasible [integer] solutions was reached.
16 = Stop chosen when the maximum number of feasible [integer] subproblems was reached.
17 = Solver converged in probability to a global solution.
18 = All variables must have both upper and lower bounds.
19 = Variable bounds conflict in binary or alldifferent constraint.
20 = Lower and upper bounds on variables allow no feasible solution.

你可以用它来判断Solve的成功与否,然后根据需要向sheet写一个flag

参考MSDN article