Home » excel » excel vba – VBA Solver disabling the dialog box that pops after each iteration

excel vba – VBA Solver disabling the dialog box that pops after each iteration

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using the built-in solver in Excel 2007 within a VBA loop to solve a number of different problems. Occasionally, the solver hits the maximum time, which causes a pop-up dialog box to appear asking whether the user wants to Continue, Stop, or End. In all cases I want it to end, and proceed to the next line of the loop. This will prevent a user from having to sit there and respond each time.

I ran a macro with the solver pass thru method (Catch max time/iteration dialog box when using Excel Solver in VBA) but then it gives me another dialog box saying “The formula you typed contains an error”

I also tried http://msdn.microsoft.com/en-us/library/office/ff197237(v=office.15).aspx .
Which is a less complicated version of ‘The solver pass thru method’ but after each iteration it gives me the same message as “The formula you typed contains an error”
This is my code

Sub Optimize()
'
' OptimizeShortfall Macro
'

'
Set MyFirstObj = Range("I124")
Set MyFirstRange = Range("H600:H698")
Dim i As Integer
For i = 0 To 1
    MyObj = MyFirstObj.Offset(0, i).Address
    MyTestRange = MyFirstRange.Offset(0, i).Address
    SolverReset
    SolverOk SetCell:=MyObj, MaxMinVal:=1, ValueOf:="0", ByChange:= _
        MyTestRange
    SolverAdd CellRef:=MyTestRange, Relation:=1, FormulaText:="100%"
    SolverOptions MaxTime:=20, Iterations:=100, Precision:=0.000001, AssumeLinear _
        :=False, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _
        IntTolerance:=5, Scaling:=False, Convergence:=0.0001, AssumeNonNeg:=True
     SolverSolve UserFinish:=True, ShowRef:="SolverIteration"
    Next i
End Sub

Function SolverIteration(Reason As Integer)
    MsgBox Reason
    SolverIteration = 1
End Function
How to&Answers:

http://msdn.microsoft.com/en-us/library/office/ff197237(v=office.15).aspx , will explain to you clearly when a dialog box appears, what it means and how to avoid it if you are running solver in a loop.

One force Solution to go to the next loop in case Max Time/Iterations were reached was to keep the Alt+T button pressed for the duration of the whole macro.

Another wiser method is to use The ShowRef argument in the SolverSolve function.
What ShowRef does is instead of poping a dialog box, it will run the macro given to it as an argument, in this case “SolverIteration”. If you want the solver to continue for the given Iteration set SolverIteration to 0. If you want to stop the solver for the given Iteration and move to the next Iteration, Set SolverIteration to 1.

Do take a note that the ShowRef Argument has some issues in digesting workbooks with very long name and/or names with space in them, So set the name of the workbook as short as possible.

Sorry if my explanation wasn’t sufficient enough for you. Here is the exhaustive list of three links that i needed to solve my problem:
http://msdn.microsoft.com/en-us/library/office/ff197237(v=office.15).aspx

Catch max time/iteration dialog box when using Excel Solver in VBA

http://www.excelforum.com/excel-programming-vba-macros/555406-solved-solver-solversolve-showref.html

P.S. In my code mentioned in the above question, just remove the line ‘MsgBox Reason’ from the function “SolverIteration”.(It just causes another dialog box to pop up with an Integer of value Reason on it :P)