

One group of functions can return a variety of numeric, logical, string or array values, depending on the arguments you supply. The return values can range from 0 to 20, and correspond one-for-one with Solver Result Messages that appear when Solver stops and displays the Solver Results dialog box. Of particular interest is the return value of the SolverSolve function, which describes the result of the solution process. Check the box next to this entry, and click OK to close the dialog. Scroll down until you find Analytic Solver Platform 2018 Type Library. If the arguments you supply are invalid, an error condition can be raised, which you would have to handle via an On Error VBA statement. With Excel open, press Alt-F11 to open the VBA Editor. Other possible return values are given in the descriptions of the individual functions. The normal return value is 0, indicating that the function succeeded. Solver VBA functions generally return integer values, which you should check in your VBA code. and make sure that the box next to Solver is checked. Then (after closing the Solver Parameters dialog) press Alt-F11 to open the Visual Basic Editor, choose Tools References. In Microsoft Excel, click Solver in the Analysis group on the Data tab to ensure that the Solver add-in is loaded. To use the Solver VBA functions, your Visual Basic module must include a reference to the Solver add-in (Solver.xlam). Open the Visual Basic Editor (Alt-F11), select Help - Microsoft Visual Basic Help, and type ‘Solver’ in the Search box to display a list of function names. The Solver VBA functions are documented in Help for Excel VBA. You can then edit and customize this macro, and incorporate it into your application. Microsoft Excel will record a macro in VBA that calls the Solver functions to mimic the actions you perform.

#Excel solver function in vba 2013 how to
If you want to set up a Solver model “from scratch” programmatically, one easy way to see how to use the Solver VBA functions is to turn on the Excel Macro Recorder (click Record Macro on the Developer tab), and then set up a Solver model interactively. If you distribute such a workbook, with a worksheet containing a Solver model and a VBA module, you can simply add a reference to the Solver add-in (see below), activate the worksheet, and add one line to call the function SolverSolve in VBA. You can create this Solver model interactively if you wish. Using Solver VBA functions, you can display or completely hide the Solver dialog boxes, create or modify the choices of objective cell, constraints and decision variable cells, check whether an optimal solution was found, and produce reports.Ĭontrolling Solver can be as simple as adding one line to your VBA code! Each worksheet in a workbook may have a Solver problem defined, which is saved automatically with the workbook. For more help on using the Solver functions in VBA, search for "solver" using the VBA help system.You can control Solver from VBA, defining and solving problems just as you do interactively. To keep the Solver Results dialog box from showing up, the userFinish:=True option has been added to the SolverSolve function.
#Excel solver function in vba 2013 code
The VBA code for the Solver macro that was recorded for Example 2 is shown You can also press Alt+ F11 to open up VBA.Īdd the Solver reference in visual basic (Tools > References., then make (Right-click on the button andīefore the macro will work, a reference to the Solver VBA add-in functionsĮdit the macro you just created (Tools > Macro > Macros. Toolbar is not displayed, right-click on any toolbar and click on "Forms".)Īssign the macro you created to the button.

The solver you are actually using is Simplex LP which has the value of 1. ).Īdd a button to the worksheet, using a button from the Forms toolbar. Probably, your objective function is nonlinear and you thought you are using the GRG Nonlinear solver since you mention it under the EngineDesc parameter. Stop the macro recorder (Tools > Macro > Stop Recording. ).Ĭlear any existing solver settings (Press the Reset All button).Ĭhoose the target cell, design variables, and constraints and press the Open the solver dialog box (Tools > Solver. Start the macro recorder (Tools > Macro > Record New Macro. Let's use the problem from Example 2 above. Is to first record all the steps used to set up and run the solver. Macro that can be run by pressing a single button. Routine a number of times using the same model, it is convenient to create a If you need to solve the same system of equations or run an optimization For optimization problems, this means that the solution may be only a local optimum. The solution may depend upon the starting values. This example has demonstrated a very important point having to do with the Excel solver and optimization in general. Figure 4: Mechanism showing two possible configurations.
