Anda di halaman 1dari 9

On Calling Solver From a Macro

Solver is a wonderful tool but one that


can't be called from an Excel macro
without some setup work in advance.

The worksheets in this workbook are:


Solver Demo - Exposition
See Solver work under macro control.
Solver's Solution
An archive workbook with Solver's
solution to the problem recorded.
Call Solver from VBA
A description of how to set up Solver
as an "available reference" to VBA.
413397763.xls

Excel Solver Demo: Forecast for 4 quarters


Solve fo
Product Price: $40.00 Product
Changing Cells Constraints Product Cost: $25.00
Target Cell

Q1 Q2 Q3 Q4 Total
Return
Seasonality 0.9 1.1 0.8 1.2 origin

Units Sold 3,592 4,390 3,192 4,789 15,962


Sales Revenue $143,662 $175,587 $127,700 $191,549 $638,498
Cost of Sales 89,789 109,742 79,812 119,718 399,061
Gross Margin 53,873 65,845 47,887 71,831 239,437

Salesforce 8,000 8,000 9,000 9,000 34,000


Advertising 10,000 10,000 10,000 10,000 40,000
Overhead 21,549 26,338 19,155 28,732 95,775
Total Costs 39,549 44,338 38,155 47,732 169,775

Product Profit $14,324 $21,507 $9,732 $24,099 $69,662


Profit Margin 10% 12% 8% 13% 11%

To return to the original worksheet: The


changing cells values were 10,000 each quarter.

Page 2
413397763.xls

Solve for maximum


Product Profit in Q1

Return model to
original values.

Page 3
413397763.xls

Excel Solver Demo: with Solver's Solution

Product Price: $40.00


Changing Cells Constraints Product Cost: $25.00
Target Cell

Q1 Q2 Q3 Q4 Total
Seasonality 0.9 1.1 0.8 1.2

Units Sold 4,465 3,970 2,888 4,331 15,655


Sales Revenue $178,605 $158,819 $115,505 $173,257 $626,186
Cost of Sales 111,628 99,262 72,190 108,286 391,366
Gross Margin 66,977 59,557 43,314 64,971 234,820

Salesforce 8,000 8,000 9,000 9,000 34,000


Advertising $17,093 $7,636 $7,636 $7,636 40,000
Overhead 26,791 23,823 17,326 25,989 93,928
Total Costs 51,884 39,459 33,961 42,624 167,928

Product Profit $15,093 $20,099 $9,353 $22,347 $66,892


Profit Margin 8% 13% 8% 13% 11%

The completed "Solver Parameters" dialog for this problem.

The constraint d

Page 4
413397763.xls

The constraint definition dialog for this problem.

Page 5
Setup so you can Call Solver from a Macro
How to:
Create your model with Solver as usual. Save it.
Open the VBA Editor (ALT+F11) and choose Tools, References The 12 Solver-related func
to open the "References - VBAProject" dialog shown below. use in VBA code:

Find "Solver" in the list of "Available References" and check it 'on'. Click OK.
Save your workbook.
Look in the VBA Project window for your project. There should now be a folder
with the name "References". If you open this folder, you should see an item
that reads "Reference to SOLVER.XLA". This reference is stored with your
workbook and macros so you don't need to re-establish it every time you
want to run the macro that calls Solver that's in your workbook.
Now in this workbook you can write a macro that invokes Solver
and the invocation can occur under macro control.

Without this setup step, you can record a macro that invokes Solver
but the macro won't run. You'll get an error message like the one below.
The 12 Solver-related functions available for
use in VBA code:

Anda mungkin juga menyukai