You are on page 1of 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.

68394955.xls

Excel Solver Demo: Forecast for 4 quarters


Changing Cells Constraints Product Price: Product Cost: $40.00 $25.00

Solve fo Product

Target Cell

Seasonality Units Sold Sales Revenue Cost of Sales Gross Margin Salesforce Advertising Overhead Total Costs Product Profit Profit Margin

Q1 0.9 3,592 $143,662 89,789 53,873 8,000 10,000 21,549 39,549 $14,324 10%

Q2 1.1 4,390 $175,587 109,742 65,845 8,000 10,000 26,338 44,338 $21,507 12%

Q3 0.8 3,192 $127,700 79,812 47,887 9,000 10,000 19,155 38,155 $9,732 8%

Q4 1.2 4,789 $191,549 119,718 71,831 9,000 10,000 28,732 47,732 $24,099 13%

Total

Return origin

15,962 $638,498 399,061 239,437 34,000 40,000 95,775 169,775 $69,662 11%

To return to the original worksheet: The changing cells values were 10,000 each quarter.

Page 2

68394955.xls

Solve for maximum Product Profit in Q1

Return model to original values.

Page 3

68394955.xls

Excel Solver Demo: with Solver's Solution


Changing Cells Constraints Product Price: Product Cost: $40.00 $25.00

Target Cell

Seasonality Units Sold Sales Revenue Cost of Sales Gross Margin Salesforce Advertising Overhead Total Costs Product Profit Profit Margin

Q1 0.9 4,465 $178,605 111,628 66,977 8,000 $17,093 26,791 51,884 $15,093 8%

Q2 1.1 3,970 $158,819 99,262 59,557 8,000 $7,636 23,823 39,459 $20,099 13%

Q3 0.8 2,888 $115,505 72,190 43,314 9,000 $7,636 17,326 33,961 $9,353 8%

Q4 1.2 4,331 $173,257 108,286 64,971 9,000 $7,636 25,989 42,624 $22,347 13%

Total

15,655 $626,186 391,366 234,820 34,000 40,000 93,928 167,928 $66,892 11%

The completed "Solver Parameters" dialog for this problem.

The constraint d

Page 4

68394955.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 to open the "References - VBAProject" dialog shown below.

The 12 Solver-related funct 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:

You might also like