Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 de 8
http://peltiertech.com/Excel/SolverVBA.html
Peltier Technical
Services, Inc.
- Custom Excel
Solutions
PTS Blog: Charts
and Things
Jon's Excel and
Charting Pages
- Jon's Charts Tutorials - Excel
- Site Index and
Search - RSS
Solver Links
Excel Books
Books that I own and
use while developing
in Excel
Goods and Services
Excel or charting
related products and
services which I use or
feel are worthwhile
additions
Click the Solve button, and another dialog indicates whether a solution is found and offers some options.
2 de 8
http://peltiertech.com/Excel/SolverVBA.html
If you record a macro while you use Solver, you will get something like the following:
Sub SolverMacro1()
'
' SolverMacro1 Macro
' Macro recorded by Jon Peltier
'
SolverOk SetCell:="$B$8", MaxMinVal:=1, ValueOf:="0", ByChange:="$B$5:$B$6"
SolverAdd CellRef:="$B$5:$B$6", Relation:=1, FormulaText:="4"
SolverOk SetCell:="$B$8", MaxMinVal:=1, ValueOf:="0", ByChange:="$B$5:$B$6"
SolverSolve
End Sub
SolverAdd adds constraints to the Solver model. SolverOK defines the cell to optimize, how to optimize it, and
what cells to change during the Solver optimization. The macro recorder wrote this line twice, so the first
occurrence can be removed. To prevent parameters from a different Solver optimization interfering with the
macro's optimization, Solver should be reset prior to running, using SolverReset. SolverSolve has an optional
UserFinish argument; if UserFinish is False or omitted, the second dialog shown above will ask the user
whether to save the optimization, but if UserFinish is True, Solver will end without the dialog. A modified
Solver macro is shown below:
Sub SolverMacro2()
'
' SolverMacro2 Macro
' Macro fixed up by Jon Peltier
'
SolverReset
SolverAdd CellRef:="$B$5:$B$6", Relation:=1, FormulaText:="4"
SolverOk SetCell:="$B$8", MaxMinVal:=1, ValueOf:="0", ByChange:="$B$5:$B$6"
SolverSolve True
End Sub
When you try to run this macro, you get a compile error. The command SolverReset is highlighted, and the
following error message appears.
In order to use a macro based on an installed add-in, you must first make sure that the add-in is installed,
then you must set a reference to the add-in in the workbook containing the code that calls the add-in's
procedures.
To install an add-in, on Excel's Tools menu, choose Add-Ins. If the add-in is shown on the list, check the box
in front of its name. If the add-in is not found, click Browse, navigate to the add-in file*, then when it appears
3 de 8
http://peltiertech.com/Excel/SolverVBA.html
on the add-in list, check its checkbox. Solver was already installed, or we would not have been able to record
a macro using it.
*Depending on your Office and Windows versions, the default Excel add-ins library is "C:\Program
Files\Microsoft Office\OFFICE11\Library" or "C:\Documents and Settings\{username}\Application
Data\Microsoft\AddIns". By default in Excel 2003, Solver is located in "C:\Program Files\Microsoft
Office\OFFICE11\Library\SOLVER".
To set a reference to an add-in, it must first be installed. Then on the VB Editor's Tools menu, select
References. This lists all open workbooks and installed add-ins, as well as a huge list of resources installed on
the host computer. Find the add-in in the list, and check the box in front of its name.
4 de 8
http://peltiertech.com/Excel/SolverVBA.html
5 de 8
http://peltiertech.com/Excel/SolverVBA.html
If Application.Run is used to return the calculated result of a function, the syntax is slightly different, with a
variable set equal to Application.Run, with the procedure and arguments enclosed within parentheses:
MyVariable = Application.Run("Function", Argument1, Argument2,...)
A more general version of a Solver procedure is shown below. This includes more informative comments, and
it provides a notice to the user about the success of the Solver optimization.
Sub RunSolver()
'' Adjusted for Application.Run() to avoid Reference problems with Solver
'' Peltier Technical Services, Inc., Copyright 2007. All rights reserved.
6 de 8
http://peltiertech.com/Excel/SolverVBA.html
' reset
Application.Run "Solver.xla!SolverReset"
' set up new analysis
Application.Run "Solver.xla!SolverOk", "Blah1", 1, , "BlahBlah1"
' add constraints
Application.Run "Solver.xla!SolverAdd", "Blah2", 3, 0
Application.Run "Solver.xla!SolverAdd", "Blah3", 2, "BlahBlah3"
' run the analysis
Result = Application.Run("Solver.xla!SolverSolve", True)
' finish the analysis
Application.Run "Solver.xla!SolverFinish"
' report on success of analysis
If Result <= 3 Then
' Result = 0, Solution found, optimality and constraints satisfied
' Result = 1, Converged, constraints satisfied
' Result = 2, Cannot improve, constraints satisfied
' Result = 3, Stopped at maximum iterations
MsgBox "Solver found a solution", vbInformation, "SOLUTION FOUND"
Else
' Result = 4, Solver did not converge
' Result = 5, No feasible solution
Beep
MsgBox "Solver was unable to find a solution.", vbExclamation, "SOLUTION NOT FOUND"
End If
End Sub
Solver found a solution. All constraints and optimality conditions are satisfied.
Solver has converged to the current solution. All constraints are satisfied.
Solver cannot improve the current solution. All constraints are satisfied.
Stop chosen when the maximum iteration limit was reached.
The Set Cell values do not converge.
Solver could not find a feasible solution.
Solver stopped at user's request.
The conditions for Assume Linear Model are not satisfied.
The problem is too large for Solver to handle.
Solver encountered an error value in a target or constraint cell.
Stop chosen when maximum time limit was reached.
There is not enough memory available to solve the problem.
Another Excel instance is using SOLVER.DLL. Try again later.
Error in model. Please verify that all cells and constraints are valid.
This command should be run before the first Solver optimization procedure is executed. I have developed a
Solver initialization routine that first makes sure the computer even has Solver, then it installs it and runs its
Auto_Open procedure. The procedure is written as a function, which returns True if Solver is available and
ready to use. I usually call this procedure from the parent workbook's Workbook_Open event procedure. If
CheckSolver is False, I usually have the workbook close itself after a brief warning to the user.
Function CheckSolver() As Boolean
'' Adjusted for Application.Run() to avoid Reference problems with Solver
'' Peltier Technical Services, Inc., Copyright 2007. All rights reserved.
'' Returns True if Solver can be used, False if not.
Dim bSolverInstalled As Boolean
'' Assume true unless otherwise
CheckSolver = True
On Error Resume Next
' check whether Solver is installed
bSolverInstalled = Application.AddIns("Solver Add-In").Installed
Err.Clear
If bSolverInstalled Then
' uninstall temporarily
Application.AddIns("Solver Add-In").Installed = False
' check whether Solver is installed (should be false)
bSolverInstalled = Application.AddIns("Solver Add-In").Installed
End If
7 de 8
http://peltiertech.com/Excel/SolverVBA.html
Solver Links
Frontline Systems
Frontline Systems has developed Solver add-ins for Excel and other applications. The standard Excel Solver
add-in can be upgraded to a premium Solver version or to other specialized Solvers, and there are versions
for use with other programming platforms. While the capabilities of Solver are very extensive, the online
documentation is somewhat sparse.
Solver Tutorial
Standard Excel Solver
Optimization Solutions with the Microsoft Excel Solver (examples)
Controlling the Solver with VBA
Microsoft
Solver (list of articles about Solver and Excel 2003)
Introduction to optimization with the Excel (2003) Solver tool
How to create Visual Basic macros by using Excel Solver in Excel 97
Perform What-If Analysis with the Excel 2007 Solver Tool
Around the Web
Microsoft Excel Solver add-in Examples (Vertex42)
Teaching Linear Programming using Microsoft Excel Solver
Using Solver for LP Problems
Solving a Linear Program Using the Excel Solver
www.bonavistasystems.com
PTS Blog
Radar-XY Combination Chart
19 Aug 2008
In Plot your data around the clock, Chandoo proposed a complicated bubble chart to show website
traffic vs. time of day. I thought it was daring of Chandoo to try such a unique treatment of the
data, but I found the bubbles difficult to interpret, and it was distracting
Time is on My Side
18 Aug 2008
Chandoo saw a post at Smashing Magazine showing Top 10 creative ways to display time, and he
took a crack at duplicating a polar clock using an Excel donut chart. Chandoo did a fine job with his
copy of Pixel Breakers Polar Clock, though the style itself is tricky
Rolling Wheel Animation
8 de 8
http://peltiertech.com/Excel/SolverVBA.html
17 Aug 2008
A college science professor named Roger Blickensderfer wrote to me about an animation he was
working on. He wanted to trace the path followed by a point on the rim of a rolling wheel. Roger had
cobbled something together, and I fiddled with it, and it kind of worked.