A Blending Application
20.1 Introduction
This application illustrates how a typical linear programming model, in this case an.
oil-blending model, can be transformed into an impressive decision support system
with very little VBA coding, ‘The key to the application is that it is a fixed-size
model. Specifically, it works (in its present form) only if there are three types of
crude oils blended into three gasoline products. This is certainly a limiting feature
of the application. However, the fixed-size property allows the entire application to
be set up at design time, without any VBA. The linear programming model can be
developed, a report can be created, and several charts can be created. The only
VBA tasks are to get the uscr’s inputs for the model and to run Solver. ‘There are
many Excel details to take care of at design time, but the finished application is very
straightforward, with a minimal amount of VBA code.
New Learning Objective: VBA
* To sce how VBA can be used to develop a complete decision support system
around a fixed-size optimization model by supplying input dialog boxes and
charts and reports for the results.
New Learning Objective: Non-VBA
* To develop an understanding of linear programming blending models
20.2 Functionality of the Application
The application provides the following functionality:
1, The application is based on a typical oil-blending model with three crude oils
blended into three gasoline products. There are many inputs to the model,
including crude oil availabilities, gasoline demands, minimum octane and
maximum sulfur percentage requirements on the gasoline products, and
others, all of which can change each time the model is run, The user has a
chance to view all of these inputs and make any desired changes,
437438 Chapter 20
2. The model is developed in a Model worksheet and is optimized by Solver.
‘The key outputs are reported in a Report worksheet. Various aspects of the
solution are also displayed on several chart sheets. The user can view these
chart sheets by clicking navigational buttons on the Report worksheet.
20.3 Running the Application
‘The application is stored in the file Blending Oil.xlsm. When this file is opened,
the Explanation worksheet in Figure 20.1 is displayed.’ When the user clicks the
Figure 20.1. Explanation Worksheet
Oil Blending Application
Run the application
‘This application finds the optimal blending plan for a problem of a fixed size. The “fixed size"
means that the problem always has three crude oils being blended into three gasoline types.
Because of this fixed size, the optimization model and all reports, including charts, can beset up
with Excel —no VBA required— at design time. Then the application uses VBA to show the
‘current inputs to the user and allows the user to change any of these. It then runs Solvar with
these Inputs and displays the results in a report, with several optional charts that can be
viewed.
“The inputs come in three groups:
(1) Monetary inputs: the cost per gallon of purchasing each type of crude, the selling price per
gallon for each type of gascline, and the cost of transforming a gallon of any crude into any
type of gasoline,
(2) Data related to actanes and sulfur percentages, Each crude has certain octane rating and
sulfur percentage. Each gasoline has a minimum required octane rating and a maximum
allowable sulfur percentage.
(3) Data related to demands, availabilities, and production capacity. As for demand, this is the
maximum amount of each type of gesoline that can be sold, (There Is no incentive, therefore, 10
produce more than this.)
* Actually, assuming that a pre-2010 version of Excel i being used, the fist thing the utr ses upon
‘opening the ile isa Solver warning. ‘This waming appears in all fata applications that invoke Soler
Teaddesses the “missing Solver reference” problem ducussed in Chapter 17. 1cindiates the necessary 6x
Solver problem. (It appears that this problem was fixed in Excel 2010, x0 the warning doesn't
sppearin Excel 2010 or later versions)A Blending Application 439
button on this sheet, the dialog box in Figure 20.2 appears, which indicates that
the inputs to the model are grouped into three categories. The user can view (and
then change, if desired) the inputs in any of these categories by checking the
appropriate options. If they are all checked, the dialog boxes in Figures 20.3,
204, and 20.5 appear sequentially. The inputs that appear initially in the boxes
are those from the previous run of the model (if any). Of course, any of these
inputs can be changed
When the user has finished viewing/changing inputs, these inputs are substi-
tuted into the model in the (hidden) Model worksheet, and the model is opti-
mized with Solver. The important inputs and outputs are displayed in a Report
worksheet, as shown in Figure 20.6, ‘This worksheet contains several buttons for
navigating to the various chart sheets and back to the Explanation worksheet
The available charts appear in Figures 20.7-20.13. Each of these charts con-
tains a button that navigates back to the Report worksheet.
Figure 20.2. Initial Dialog Box
Input options
‘You can vien change any of the route
for the modelin the folowing grows.
‘Checthe graup() you want to view. aa
EE
ones vinous
IM Data on octane ratngs, sulfur contents
data on demands, avaible, capadty
Figure 20.3 Dialog Box for Monetary Inputs
eas a7
Ten dle fant pre
tuple, esr paper eek on of
gasoline, andthe cost oF transforming ene gallon of rude ints
tne gallon af gazaine
cancel
Unit ude costs
crude: [
rude 3
crude B
CCostofprodichaa salon of cascline fon a gallon of rude: [5440° Chapter 20
Figure 20.4 Dialog Box for Octane, Sulfur Inputs
dane ad surinauts ee
ht ag fn ed
Se
Ce ee ee nee
ne ee
10%, for example.
ce ee sree
cues [a cde [oor
ouex [ ce [305
> Minimum octane ratngs ———— j- Maximum sulfur percentages
= Eb Ss
Figure 20.5 Dialog Box for Remaining Inputs
or
These nputs, al expressedin galons, indude the demands for
csalines (the maximum amounts that canbe sad), the avalabiites
cf cuces, and production capacity,
Crue avaicbies Demands for gas
cadet: [5000 Gest: 3000)
| | cude2: — [sn00 cas2: 7000
cadess [S000 cae 008
Production cpacty: [15000
et me nig ie el ai ce me pn ng act ft egg ty ns eckA Blending Application 441
Figure 20.6 Report Worksheet
Report of optimal blending solution
ae a cy S230] ‘Siso00 — ston]
ce Tas a 0) Binns sos]
toning pn gions ef crue wean erent gsc
‘ined — Wire Ges Marion]
aA
‘soon
Figure 20.7 Chart of Crude Oils Purchased
Gallons of Crudes Purchased
5000
4500,
4000
3500
(Purchased
Available
3000
2500
2000
1500
1000
500
Crude 1 Crude 2 Crude 3
et me nig Aiea ee oat ce «pn ng nic fined ty es eck442. Chapter 20
Figure 20.8 Chart of Crude Oil Purchase Costs
Crude Purchase Costs
$160,000
$140,000
120,000
$100,000
$80,000
$60,000
$40,000
$20,000
so
Crude 1 Crude 2 crude 3
Figure 20.9 Chart of Gasolines Produced and Demands
Gallons of Gasolines Produced
3000
2500
2000 ——
1D Produced
1B Demand
1500
Gast Gas 2 Gas 3
et me nig Aiea el oi ce me pn nga ct ined ty is eckA Blending Application 443
Figure 20.10 Chart of Production Costs and Gasoline Revenues
$250,000
'$200,000
$150,000
$100,000
$50,000
$0
Production Costs, Revenues from Gasolines
1D Production’
cost
Revenue
Gas 1
Gas 2 Gas 3
Figure 20.11. Chart of Blending Plan
2000.00
1800.00
1600.00
1400.00
1200.00
1000.00
800.00
600.00
400.00
200.00
0.00
Gast
Blending Plan: Gallons of Crudes Used in Gasolines
Grids t
Crude 2
wCrude 3
Crude 3
Crude 2
Crude 1
Gas 2 Gas 3
et me nig Aig el oi ce me «pn ng ct fined ty ns eck444 Chapter 20
Figure 20.12 Chart of Octane Requirements
Octane Requirements
12.00
10.00
8.00
1D Obtained
Min required
6.00
4,00
2.00
0.00
Gast Gas? Gas 3
Figure 20.13 Chart of Sulfur Percentage Requirements
Sulfur Requirements
3.00%
2.50%
2.00%
1D Obtained
Max allowed
1.50%
1.00%
0.50%
0.00%
Gast Gas 2 Gas 3
te cme nig ipa el oa ce «ni ng ac inept ns eckA Blending Application 445
20.4 Setting Up the Excel Sheets
The Blending Oil.xlsm file contains three worksheets and seven chart sheets. All
of these can be developed at design time, without any VBA. The three worksheets
are the Explanation shect in Figure 20.1, the Model sheet in Figure 20.14, and
the Report sheet in Figure 20.6. Because of the fixed size of the problem (always
three crude oils and three gasoline products), the structure of the model never
changes; the only changes are new input values. Therefore, this model can be
developed completely, including the Solver dialog box, at design time. Any inputs
can be used for testing the model. (The model itself is a straightforward applica-
tion of linear programming, You can open the Blending Oil.xlsm file, unhide
the Model worksheet, and examine its formulas if you like.) Similarly, the Report
worksheet can be developed, with links to appropriate cells in the Model work-
sheet, once and for all at design time. Finally, the charts in Figures 20.7 to 20.13
can be developed and linked to appropriate ranges in the Report worksheet at
design time. After these worksheets and chart sheets are developed, they are
ready and waiting for user inputs.
20.5 Get
g Started with the VBA
The application includes four user forms, named frminputTypes, frminputs1, frmin-
puts2, and frminputs3, a single module, and, because this application will be invok-
ing Solver VBA functions, a reference to Solver. (Remember that you set a
reference with the Tools-*References menu item in the VBE.) Once these items
are added, the Project Explorer window will appear as in Figure 20.15. (As
always, I have supplied meaningful code names for all of the shects.)
Workbook_Open Code
To guarantee that the Explanation worksheet appears when the file is opened, the
following event handler is placed in the ThisWorkbook code window. Note that it
uses For Each loops to hide all sheets except the Explanation worksheet
Private Sub Workbook_Open()
im ws As Worksheet, cht As Chart
For Each ws In ThisWorkbook Worksheets
I ws.CodeName <> "wsExplanation" Then ws.Visible = False
Next
2p also contains the one other user form, femSover, that simply dips « message about possible
Solver problems when the workbook is opened. This is inchided in all of the Solver applications in
the remainder ofthe book, althougls wers with Excel 2010 of ater versions won't se it.446 Chapter 20
Figure 20.14 Model Worksheet
x [se
7 [oir blending model
2z
3 [Buichase prices per gaon of crude Sai ric per barrel of gasoline
z $55 Gasi Gas Gavi
5 $35, $70 $60 $75
§ $25
7
1 |cost to transform one barrel of crude into one bart! of gasoline
3 $5
10
11 [Requirements for gesolines
2 Gas Gas2 Gas
33 [Minimum setae u 10 9
28 [Maximum sulfur 1% % 1%
35
36 Octane ratings Saulfurcontent
a7 crude 2 2 Crude 2 10%
a8 ude 2 20 crude 2 05%
39 exude 3 8 Crude 3 30%
20
21 Jpurchase/production plan
2 Gaei Gas ae 3” Yall purchased ian Ava
23 [Guide i 350000 0.00 0.00 1500, 000]
28 Crude 2 150000 200000 800.00 ‘300 5000]
25 Crude 3 0.900 000 200.00 200 000]
36
27 Demand for gasolines
Gast Gas2 Gas
JAmount produced 3000 2000 1000
3000 1000”
33 [Constraint on total production
% Total produces Max Capacity
35 6000 15000
36
37 octane constraints Gas Gas2 Gas
35 [Actual total octane 33000 20000 9600
42 [Sulfur constraints Geet Gs Gast
43 [Actual total sulfur 228 6 10,
45 [Required 30 6 10
46
47 purchase cost $238,000
248 [Production costs 30,000
29 sales revenue $405,000
50
51 [Profit $137,000A Blending Application 447
For Each cht In ThisWorkbook.Charts
cntVisible = False
Next
IF Not (Application Version = "15.0" Or Application Version = *14.0") Then
fmSolver.Show
End Sub
Figure 20.15 Project Explorer Window
G9 Mieroxoft Excel Objects
i) disendPan Gerda)
i chiCrudeCosts (CruceCosts)
ADD ctCrudePurch (CrucePurch)
i) chicasPredkced (GasProd.ced)
Al] crtcctane (Octane)
chiprodCostsRievs (ProdCostsRevs)
chesufwr Gus)
3B) Thswerkbock
8) wsExplanation (Explanation)
FB) wstodel eds)
£} wsRepart Report)
S+9 Foms
EB Frntnputst
FB Fmtnputs2
Fal frninputs3
EB frntnoutynes
FH finsoher
-E¥ Modules
“988, Module
2-63 References
18g Reference to SOLIER.ILAM 3
20.6 The User Forms
frminputTypes
The design of fminputTypes is shown in Figure 20.16. It contains the usual OK
and Cancel buttons, an explanation label, and three check boxes named ehklnputst,
chkinputs2, and chkinputs3. The design of the user forms for this application is
completely straightforward, The text boxes must be positioned and named, the
labels must be positioned and captioned, and so on—straightforward operations.
The event handlers for this user form are listed below. The Initialize sub
checks cach of the check boxes by default, The ShowInputTypesDialog function
captures the user’s entries in the check boxes in the Boolean variables binInputst,
blninputs2, and bininputs3.app 08 Compe ning ADs eel Mase pid se peel nesta Dehn ih ie pty ae ay peed mh eke a
‘sve ea tk yep nl nt a setae. gL asia aan nbn te ue nant see eeA Blending Application 449
frminputs1
The design of frminputs1 is shown in Figure 20.17. It contains OK and Cancel
buttons, an explanation label, two frames for grouping inputs, and seven text
boxes and corresponding labels. The three text boxes in the Unit crude costs
group are named txtCrudet, txtCrude2, and txtCrudeS. The three text boxes in the
Unit gas prices group are named ttGast, ttGas2, and ttGas3. Finally, the
production cost text box is named tetProdCost.
The Initialize sub captures the values in the Model worksheet (from a previous ran,
if any) and enters them in the text boxes, and then the Showinputs1Dialog fiunction
takes the user’s choices and places them back in the Model worksheet. Note that it
uses the Val function to convert a string (which is always the result from a text box) to
a numeric value (which is required in the worksheet). Otherwise, arithmetic operations
couldn’t be performed on these cells in the worksheet. The Valid fiunetion performs
some error checking to ensure that the user’s inputs are numeric and positive.
Figure 20.17 Design of frmInputs1
sentence
Monetary inputs
‘he monetary inputs include the purchase cost per gallon for
each type of crude, the seling price per gallon For each type of
gasoline, andthe cest of traneferming ane gall of crude into
tne galon of gasolne.
pr Utierude costs Unit ges prices
a ao
_— :
cule ma
oS aes
:
Cost tres alr sine em again of oe
Private cancel As Boolean
Public Function Showinputs1Dialog() As Boolean
Call Initialize
‘Me,Show
IY Not cancel Then
" Enter the user inputs in the (hidden) Model sheet.
With wsModel.Range('PurchCosts")
is(1}Value = ValtGrude1 Text)
ls(2)Value = ValxtGrude2 Tex)
[Cels(2) Value = Val(xtCrudes.Text)
End With
With wsModel Range(SellPrices")
{Cel} Vake = Val(xtGast-Text)
s(2} Value = Val(oxtGas2-Text)
i(3).Value = Val(tGas3 Text)opp 08 Cn ning Aig eel Mae pid se pel nhs hn i pty ate ay be peed mh ede
‘sea eth ype nl nt a ean Cg ats is aan bn ee eae ste feA Blending Application 451
frminputs2 and frminputs3
The other two input forms, frminputs2 and fmminputs3, shown in Figures 20.4 and
20.5, are very similar. For example, frminputs2 contains OK and Cancel buttons,
an explanation label, four frames for grouping the inputs, and 12 text boxes and
corresponding labels. The code behind these two forms is very similar to the
frminputst code, so it is not listed here
20.7 The Module
In most applications, the VBA code in the module does the majority of the work.
However, this is not the case here. There is a MainBlending module that “shows”
the appropriate forms, which capture the user’s inputs. Therefore, all the Main-
Blending sub needs to do is show the forms and then nan Solver. (Note that it
must first unhide the Model worksheet. Solver cannot be run on a model in a
hidden sheet.) Other than this, the module contains only navigational subs (not
shown here). ‘These are attached to the buttons on the Report worksheet and
the various chart sheets.
MainBlending Code
Sub MainBtending()
"This sub runs when the user clicks on the button on the Explanation sheet.
Dim inputTypet As Boolean, inputType2 As Boolean, inputType3 As Boolean
Dim solverStatus As Integer
" Find which types of inputs the user wants to view/change.
If frminputTypes-ShowinputTypesDialog(inputTypet, inputType?, inputTypes) Then
* Show the input forms the user has requested,
* Exit if the user cancels from any of them.
|W inputTypet Then
If Not frminputs1.Showinputs1Dialog Then Exit Sub
End It
1 inputType2 Then
If Not fminputs2.Showinputs2Dialog Then Exit Sub
End It
1 inputType3 Then
If Not fminputs3.ShowinputsDialog Then Exit Sub
End It
Application ScreenUpdating = False
* Unhide and activate the Model sheet, and run the Solver
‘With weMode!
Msible = True
‘Activate
End With
* Call Solver and check for no feasible solutions
solverStatus = SolverSolve(UserFinish:=True)
If solvorStatus = 5 Then452 Chapter 20
" There aro no feasible solutions, so report this and quit
MsgBox “There is no feasible solution to the problem with these
‘& “inputs. Try again with different inputs.”, _
vblnformation, "Not feasible"
wsModel Visible = False
Call ViewExplanation
Else
"There is a solution, so report it in the Report sheet.
wsModel Visible = False
With wsReport
Visible = True
‘Activate
‘Range(’A") Select
End With
End it
‘Application ScreenUpdating = True
End If
End Sub
20.8 Summary
‘This application is a great example of the functionality you can achieve with very
liale VBA code. Although I don’t necessarily encourage you to create all of the
applications, starting from scratch, in the remaining chapters, I urge you to try
developing this blending application on your own. ‘There are at least two ways you
can proceed. First, you can open a new, blank workbook and create the entire
application—model, user forms, and code. Alternatively, you can make a copy of
the Blending Oil.xlsm file, ‘Then you can delete the user forms and the module
from your copy and recreate them on your own, using the explanations in this
chapter as a guide. In either case, you will find that there are no difficult steps in
this application; there are just a lot of relatively simple steps. In fact, you will find
that many of these steps are quite repetitive. Therefore, you should look for any
possible shorteuts, such as copying and pasting, to reduce the development time.
EXERCISES
1. All of the charts in this application are types of three-dimensional column charts.
Change the application so that they are different chart types. (You can decide
which you prefer.) Do you need to rewrite any of the VBA code?
2. Continuing Exercise 1, suppose you want to give the user the choice of chart
types. For example, suppose you want to give the user two choices: the current
chart types or some other chart type. (You can choose the other type.) Change
the application to allow this choice. Now you will have to add some new VBA
code, along with another user form. However, you should never need to create
any charts from scratch with VBA. You only need to modify existing charts