1DevelopingDSS SpreadSheet Ebook PDF
1DevelopingDSS SpreadSheet Ebook PDF
Developing Spreadsheet-Based
Decision Support Systems
Using Excel and VBA for Excel
Michelle M. Hanna
Industrial and Systems Engineering
University of Florida
Ravindra K. Ahuja
Industrial and Systems Engineering
University of Florida
Wayne L. Winston
Kelly School of Business
Indiana University
Book Outline
Preface
Chapter 1: Introduction
Part I – Excel Essentials [Chapters 2 – 10]
Part II – VBA for Excel [Chapters 11 – 22]
Part III – Case Studies [Case Studies 1 - 25]
Appendix
Preface
Chapter 1: Introduction to Decision Support Systems
1.1 Introduction to DSS
1.2 Defining DSS
1.3 DSS Applications
1.4 Overview of Excel
1.5 Overview of VBA for Excel
1.6 Overview of Case Studies
1.7 Summary
13.1.5 Application
13.2 The With construct
13.3 Referencing in VBA
13.3.1 Referencing ranges and cells
13.3.2 Naming ranges
13.3.3 Naming other Excel objects
13.4 Formulas in VBA
13.4.1 Using ranges and cells
13.4.2 Using the Application object
13.5 Summary
18.9 Applications
18.9.1 Real Estate Search Spreadsheet Application
18.9.2 Product Search Form
18.10 Summary
Chapter 19: The Solver Re-Visited
19.1 Review from Chapter 7 in Part I
19.1.1 Understanding the problem
19.1.2 Preparing the worksheet
19.2 Solver commands in VBA
19.2.1 Identifying Solver Input
19.2.2 Setting Solver options
19.2.3 Running the solver
19.2.4 Generating Reports
19.3 Application
19.3.1 Dynamic Production Problem
19.4 Summary
Chapter 20: Simulation Re-Visited
20.1 Review from Chapter 7 in Part I
20.1.1 Defining Simulation
20.1.2 Working with Distributions
20.2 Simulation with VBA
20.2.1 Random Numbers and Distributions
20.2.2 Making Runs and Collecting Data
20.2.3 Animation
20.2.4 Analysis
20.3 Application
20.3.1 Game of Craps Revisited
20.4 Summary
Chapter 21: Working With Large Data in VBA
21.1 Creating Pivot Tables with VBA
21.2 Using External Databases
21.2.1 Importing Data
21.2.2 Performing Queries with SQL
21.2.3 Exporting Data
21.3 Applications
21.3.1 Transcript Query
21.4 Summary
Chapter 22: Debugging and Error Checking
22.1 Types of Errors
22.2 The Debug Toolbar
22.2.1 Step-Into
22.2.2 Step-Over
22.2.3 Step-Out
22.2.4 Run-To-Cursor
22.2.5 Breakpoints
22.3 The Debug Windows
22.3.1 Watch Window
22.3.2 Immediate Window
22.4 Debugging tips
22.5 Error Checking
22.5.1 Review of some techniques
22.5.2 Is Functions
22.5.3 On Error, Resume Next
22.5.4 On Error, Go To
22.5.5 Err Object
22.6 Summary
Appendix
Overview
Motivation
Book Contents
Intended Audience and Required Background
Suggestions for Instruction
CD Contents
Acknowledgements
Preface
Motivation
Developing Spreadsheet-Based Decision Support Systems is intended to be a textbook
which explains spreadsheet functionality and modeling in Microsoft Excel, illustrates the
programming basics and advanced topics in Visual Basic Applications (VBA) for Excel,
and demonstrates case studies which combine these two topics to create complete
decision support systems.
The field of IT is vast and the area where industrial engineering or operations research
(IE/OR) graduates can contribute effectively, considering their background, is in
developing Intelligent Information Systems, alternatively called Decision Support
Systems (DSS). A DSS uses data, models it, processes or analyzes it using problem-
specific methodologies, and assists the user in the decision-making process through a
graphical user interface (GUI).
For the context of this text, we define a spreadsheet-based decision support system to
be a decision support system developed in a spreadsheet environment, namely
Microsoft Excel, with the enhancement of a programming language and user interface
developer, namely VBA for Excel. In reviewing available literature, we have found that
there are many books on Excel and many books on VBA, but very few books which
illustrate how to combine these tools to develop decision support systems. As there is a
need for teaching IE/OR and business students how to develop decision support
systems, there is also a need for a comprehensive textbook in this subject. Thus, this
book is intended to give an overview of Excel’s functionality, instruction on how to
program in VBA, and finally an explanation and illustration of how to develop a complete
decision support system application.
This book explains new topics through numerous examples selected from Industrial
Engineering courses, business concepts, and other various engineering fields. In
addition to several examples, the book contains many exercises at the end of each
chapter. Each example in the text introduces a new concept, and the exercises which
follow reinforce these concepts. Through the material in this book, students will learn the
basics of decision support systems development skills so that they can play a valuable
role in the design, development and deployment of decision support systems.
Book Contents
There are three parts of this textbook: learning Excel functionality, learning how to
program with VBA for Excel, and learning how to develop DSS applications through
several case studies.
2
Preface
Part II – VBA for Excel An overview of programming in VBA and manipulating Excel
objects is presented. An introduction to the visual basic environment is given and
macros are explained. The programming topics discussed include variables,
procedures, programming structures, and arrays. User interface development is then
discussed through the explanation of user forms, form controls, and navigational
buttons. In the last few chapters of this part of the book, extended Excel functionality
topics are revisited to illustrate the enhancement that VBA offers: the Solver is
revisited, simulation is revisited, and working with large data in VBA is covered. The
programming topics discussed are essential to the development of DSS applications.
Part III – Case Studies Several fully developed DSS applications are presented in
the areas of IE/OR, business, and general engineering. The case studies are
preceded by two chapters which explain the DSS development process and provide
more detailed instructions on designing a good user interface and using
programming principles in VBA. It is important for the reader to understand how to
plan the DSS application, prepare the spreadsheet, and implement the code so that
the user’s objectives are met and the decision maker will truly be aided by the DSS
system they will use.
We assume the reader has minimal Excel experience; that is, we hope the reader is
already familiar with how to insert data into cells, make tables, and other basic
spreadsheet actions. However, this book will still be useful to the experienced Excel
user as Part I of the book will provide a comprehensive review of Excel’s features and
may even present new functionality to the reader. Even if an experienced reader wishes
to skip some of the chapters in Part I of the book, we believe that the rest of the text will
be informative for them. For the sake of completeness we have chosen to cover enough
material on Excel’s functionality so that a reader with minimal Excel experience can be
comfortable working in a spreadsheet environment after reading Part I of the text.
For the chapter on the Excel Solver (Chapter 8), we recommend that readers are
somewhat familiar with OR modeling. If a DSS course is offered to junior/senior level
IE/OR students or MBA students, then OR/modeling courses will already have been
taken. However, we do provide several examples in the chapter so that even a reader
with minimal modeling experience should be able to understand how to formulate an
optimization problem in the spreadsheet and use the Solver to find the solution.
3
Preface
No programming experience is required to use this book. We instruct the reader on how
to create variables, define functions, use basic programming structures, and work with
arrays in VBA. The reader will also learn how to create good graphical user interfaces in
Excel through user forms and various controls. It is useful if the reader has some
programming experience, but not required. As with Part I of the book, some experienced
programmers may skip some chapters in Part II of the book. However, it is good to
review the examples of programming in VBA. These chapters also include several
applications which may be considered as “mini” DSS applications. Therefore, even for
an experienced programmer, it is useful to review these chapters to strengthen the
reader’s skills in using VBA in the context of developing a DSS.
We hope that after learning the Excel background and VBA material in Parts I and II of
the book, the reader will be able to develop any of the case studies found in Part III. The
case studies construct prototype decision support systems that are simple enough to be
understood by undergraduates and complex enough to be reasonably accurate
representatives of the real-world problems.
We provide the following table to aid the instructor in selecting chapters to teach. We
have listed groups of topics in the text from Part I (Excel), Part II (VBA) and Part III
(Case Studies). Here, we categorize the background level of the students/readers as
“beginner” or “intermediate”. For each background level, we suggest chapters for the
students to read. The notation of parenthesis around a chapter number, e.g., (Ch 2),
implies that this chapter may be reviewed quickly by the instructor or left for the reader to
review on their own. We also list the hours needed to teach the suggested chapters for
each background level and each topic.
4
Preface
Case studies may be selected to fit the audience. Case studies may be 5 - 10
grouped for undergraduate and graduate students or engineering and
business students.
A few applications may be selected and developed from scratch with
the students or certain aspects of several applications may be
demonstrated to the students.
From this table, several possible modules may be formed. We present four example
modules in the table below.
5
Preface
In our experience, it seems most productive to hold this course in a computer laboratory
or require students to bring laptops to the class. There are several examples in the text
which are intended to be conducted in class as hands-on learning examples. We
suggest that after the instructor provides some general information of the course
material at the beginning of each class, they end the class by conducting one of these
hands-on examples as an illustration of the concepts taught. For this reason, we also
recommend that there be a teaching assistant available to help students as they are
performing the examples on their personal computers so that the instructor may illustrate
the example simultaneously at the front of the class. We have found that students learn
much more in this manner as they experiment with Excel and VBA themselves with the
instructor, or teaching assistant, readily available for questions.
In this suggested setting of a computer laboratory or class with laptops, we also suggest
that the course be taught in two-hour sessions. This allows students enough time to set
up their computers and instructors enough time to illustrate a full example at the end of
each class. Teaching two-hour sessions twice a week implies that the course be offered
as a 4-credit course in a semester format. In IE/OR curriculums, it is common to offer
courses with programming for more credit, so this does not seem to be a problem to
offer this course as such. However, this course may also be taught as a 3-credit course
which meets three times a week for one hour each or twice a week for one two-hour
session and one one-hour session. We also feel that the course material can be
adjusted to teach a module or quarterly format as a two-credit or three-credit course.
If a computer laboratory or student laptops are not an option, then there are still several
exercises at the end of each chapter which may be assigned to the student for extra
practice. (We of course recommend that these exercises be given to students regardless
of the classroom format.) The main idea is that students have a chance to sit at a
computer, either in class or on their own, and work in Excel and VBA. It is important that
they become very comfortable working with these software as they are the main tools
they will use for developing spreadsheet-based decision support systems.
We also suggest that students be assigned a course project in which they will develop a
complete DSS application. As our case studies are the heart of our text, these student
projects are the heart of the course. It is vital that students not only learn the tools
needed to develop a DSS, but that they actually to develop a complete DSS application.
Course projects can be assigned on an individual basis or in groups depending on the
course size and course format. Students may create their own ideas for a DSS, which
may test their understanding of the definition of a DSS, or they may be assigned a DSS
to develop. We have a list of 100 team projects that may be used in such an assignment.
In our experience, students have had much positive feedback for these student projects.
Some of them use the DSS applications they develop for the course as part of an honors
thesis or masters project. Some of them include them in their resumes and interviews for
job applications. Some of them extend them for actual industry work once they move into
industry positions. Almost all of them have come back to tell us how useful decision
support systems are in their jobs or graduate work as they realize the true need to
combine OR and IT skills.
6
Preface
CD Contents
We have included a CD with this text book: one for students and one for instructors. This
CD is intended to aid students by providing them with Excel files and case study files to
help them in their practice of learning how to develop a DSS. We have also developed
several course materials to aid instructors. The contents of these CDs are listed below.
Students:
• Chapter example files
• Course example files (these are incomplete so that students may complete
them in class with the instructor or on their own)
• All case study files from Part III of the book
• License for the educational version of the Evolutionary Solver
Instructors:
• Chapter example files
• Course example files (complete and incomplete versions are both included)
• Course PPT lectures
• All case study files from Part III of the book
• License for the educational version of the Evolutionary Solver
• List of student team project ideas
• (Solution manual available in hard copy upon request)
Acknowledgements
There are many people we would like to thank who have made contributions to the
development of this book. First and foremost, we would like to thank Dr. Donald Hearn,
Chairperson of the Industrial and Systems Engineering Department at the University of
Florida. This project was motivated by Dr. Hearn’s foresight on the importance of
teaching IT and decision making tools to both undergraduate and graduate students
preparing to work with industries. Without his support, the importance of the topics
covered in this book and related courses would not have been recognized.
We would also like to give great thanks to Sandra Duni Eksioglu. Sandra received her
doctorate in Industrial and Systems Engineering from the University of Florida. She has
been working with us since the project began. She has developed several team projects
and organized them as a manual for students and instructors. She also prepared the
solutions manual for this textbook. Her hard work and dedication are much appreciated.
There have also been several students who have helped in the development of the
book. These student include both undergraduates and graduates from the Industrial and
Systems Engineering and English Departments at the University of Florida. We would
like to thank Richard Barrow and Melissa Sullivan for the contributions they made to
developing exercises and some case study programming; Carolyn Houston, Krystal
Harriot, and Sarah Schiff for the initial review and editing of the text; and Ali Abbas,
Krishna Jha, and Burak Eksioglu for the development of several team projects. We
would also like to thank the students of the DSS courses offered at the University of
Florida who used our preliminary chapters as text for the course. Their feedback is much
appreciated.
7
Preface
We would like to thank our publisher Curt Hinrich and Duxbury Press for their support.
Their energy for this project was most inspiring. We also thank our reviewers for their
comments and suggestions.
Last, but definitely not least, we thank our colleagues and families for their constant
support and encouragement.
8
Chapter 1
Introduction
Chapter Overview
Equipped with the modeling and algorithmic skills taught in the standard Operations
Research (OR) curriculum, Susan is ready to solve real-world problems. With a
knowledge and understanding of theory and applications of mathematical programming,
simulation techniques, and supply-chain management, she is ready to help her company
solve distribution problems by linear programming, inventory problems by applying the
EOQ model, and manpower planning problems by integer programming. But then, as
she interacts more with her colleagues and team members, it slowly dawns on her that
the models she learned in her education are not taking her very far. The Vice President
(VP) of her division is facing a decision problem which she wants Susan to solve, but
she has no interest in understanding the mathematical model for her decision problem.
She wants Susan to develop a software solution for her decision problem. However,
Susan is clueless about how to develop such a system. She knows the right model but
she does not know how to package her model and how to present it with friendly
graphical user interface (GUI). Her VP needs to be able to easily use it, see the results
with graphs or charts, and modify inputs to analyze different business scenarios. Susan
also realizes that the data needed by her model is available somewhere in the company
database and needs to be pulled from there. But databases are strangers to her. She
concludes that her education did not impart to her the skills she needs to meet her job
requirements.
Susan is not the only one facing problems in her job as an OR practitioner. This is a
widely prevalent problem which is not addressed in the current OR curriculum. Models
need data which is mostly available in spreadsheets or databases. Hence, our students
need to know how to extract data from these data sources. They need to know how to
check data integrity and perform data analysis and data manipulation. As OR
practitioners, our students are support staff members and are required to build systems
for non-OR users. They must know how to package OR models so that they can be
comfortably used by top managers and other co-workers. Real-life decision making often
requires building interactive systems, which our students must know how to design and
implement. To summarize, our students must learn sufficient information technology
skills to that they can build intelligent information systems, alternatively, called decision
support systems, which can run sophisticated models at the back-end, but are friendly
enough at the front end to be used comfortably by any user.
A decision support system (DSS) gives its users access to a variety of data sources,
modeling techniques, and stored domain knowledge via an easy to use GUI. For
example, a DSS can use the data residing in spreadsheets, prepare a mathematical
model using this data, solve it or analyze it using problem-specific methodologies, and
assists the user in the decision-making process through a graphical user interface. Our
students are frequently being employed in positions that require developing DSS which
are gaining widespread popularity. As more and more companies install enterprise
resource planning (ERP) packages and invest in building data warehouses, those who
are able to create decision technologies driven applications that interface with these
systems and analyze the data they provide will become increasingly valuable. Indeed,
imparting DSS development skills, which combine OR skills with IT skills, will make
students highly sought after in the modern workplace.
2
Chapter 1: Introduction
Developing courses that teach our students how to build DSS has been a challenging
task so far since it requires the availability of platforms which allowed the integration of
various technologies (data, models, codes, etc.). However, in the past few years, several
platforms have become available which allows such integration. One such platform is
Microsoft Excel. Excel, which is the most widely used spreadsheet package among
managers and engineers, allows data storage and model building. Excel also has many
built-in program as well as many add-on programs available that wallow optimization and
simulation of various models built in Excel. Excel also has a macro programming
language, Visual Basic for Applications (VBA), which allows building GUIs and
manipulating Excel objects. Thus, Excel provides a platform using which fairly
sophisticated DSS applications can be built. This book imparts the skills needed to build
such systems.
3. A DSS usually uses models for analyzing decision-making situations and may
also include a knowledge component.
4. A DSS attempts to improve the effectiveness of decision making rather than its
efficiency.
5. A DSS pro vide support for various managerial levels from line mangers to top
executives. It provides support to individuals as well as groups. It can be PC-
based or web-based.
A DSS application contains five components: database, model base, knowledge base,
GUI, and user (see Figure 1.1). The database stores the data, model and knowledge
bases store the collections of models and knowledge, respectively, and the GUI allows
the user to interact with the database, model base and knowledge base. The database
and knowledge base can be found in a basic information system. The knowledge base
may contain simple search results for analyzing the data in the database. For example,
the knowledge base may contain how many employees in a company database have
worked at the company for over ten years. A decision support system is an intelligent
information system because of the addition of the model base. The model base has the
models used to perform optimization, simulation, or other algorithms for advanced
3
Chapter 1: Introduction
calculations and analysis. These models allow the decision support system to not only
supply information to the user but aid the user in making a decision. We now present a
more detailed look at each of these components.
Database
GUI
User
Database: The database provides the data with which decisions are made. The data
may reside in spreadsheets or a data warehouse, a repository for corporate relevant
decision-making data. The database allows a user to access, manipulate, and query
data. Some examples of databases would include a spreadsheet containing personal
banking account information or a data warehouse containing shipment records of
various products.
Knowledge Base: Many managerial decision making problems are so complex that
they require special expertise for their solution. The knowledge base part of a DSS
allows this expertise to be stored and accessed to enhance the operation of other DSS
components. For example, credit card companies use a DSS to identify credit card
thefts. They store in their knowledge base the spending patterns that usually follow
credit card thefts; any abnormal activity in an account would trigger checking for the
presence of those patters and a possible suspension of the account.
GUI: The graphical user interface (GUI) covers all aspects of communication between a
user and a DSS application. The user interface interacts with the database, model base,
and knowledge base. It allows the user to enter data or update data, run the chosen
model, view the results of the model, and possible rerun the application with different
4
Chapter 1: Introduction
data and/or model combination. The user interface is perhaps the most important
component of a DSS because much of the poser, flexibility, and ease of use of a DSS
are derived from this component.
User: The person which use the DSS to support the decision making process is called
the user, or decision maker. A DSS has two broad classes of users: managers and staff
specialists, or engineers. When designing a DSS, it is important to know for which class
of users the DSS is being designed. In general, managers expect a DSS to be more
user-friendly than do staff specialists.
Given the above definition of a decision support system, we have developed several
spreadsheet-based DSS applications in Excel. Using the spreadsheet functionality in
Excel and the VBA programming capabilities, a complete decision support system can
be developed. The applications we develop illustrate decisions which are made in IE/OR
and business industries. In this section, we provide two examples of decision support
systems. These examples have been selected from the DSS applications developed in
Part III of this book.
Facility Layout In this application, we study a facility location problem which consists of
placing n facilities at n locations to minimize the total handling cost. This problem is also
known as the Quadratic Assignment Problem (QAP). The QAP arises in many other
applications, such as the allocation of plants to candidate locations, backboard wiring
problem, design of control panels and typewriter keyboards, turbine balancing, etc. The
user begins the application by providing the size of their facility. From the dimensions
provided, a layout is displayed and a distance matrix is created. Random flow matrix
values are generated which the user may overwrite if desired. From these two matrices,
the cost matrix is derived; the total of the costs from this matrix is minimized by
5
Chapter 1: Introduction
performing a pair-wise local search on the user’s facility. The user may also opt to fix
some facilities so that they cannot be moved when the local search is performed. The
user may run this local search algorithm automatically or participate in the decision taken
at each iteration. The final layout is then displayed to the user. The model base for this
application uses an algorithm developed in the VBA code. This DSS application aids a
facility designer in creating a facility layout which minimized total handling cost. It can be
used to solve typical facility layout problems which may be encountered by plant
managers, school administrators, or in other applications. This DSS can also be used as
a pedagogical tool in facility planning courses to illustrate the pair-wise local search
technique.
Microsoft Excel spreadsheets have become one of the most popular software packages
in the business world, so much so that business schools have developed several
popular Excel based courses. A spreadsheet application has functionality for storing and
organizing data, performing various calculations, and using additional packages, called
Add-Ins, for more advanced problem solving and analysis. Part I of this book discusses
two aspects of Excel: basic functionality and extended functionality.
The topics we discuss on Excel basic functionality include referencing and names,
functions and formulas, charts, and pivot tables. These are standard tools that may be
common to most spreadsheet users. The topics we discuss on Excel extended
functionality include statistical analysis, the Solver and modeling, simulation, and
querying large data. These tools are especially important for building a decision support
system. The ability to model a problem and solve it or simulate it adds the model base
component of the DSS we are building. There are several examples given in the
chapters in this part of the book which illustrate how these tools can be applied. It is
important that the reader become familiar with the capabilities of Excel so that they know
what they can offer the user when developing a decision support system.
VBA for Excel is a programming language that allows for further manipulation of the
Excel functionalities. VBA for Excel also allows the developer to create dynamic
applications which can receive user input for the model base component of the DSS.
VBA allows users without knowledge of Excel to be able to use spreadsheet-based DSS
applications. Part II of this book teaches important features of VBA for Excel.
We begin the VBA part of the book by illustrating the idea of macros and the visual basic
environment. Then, we discuss how to work with variables, procedures, programming
structures, and arrays in VBA. VBA for Excel is an easy to understand programming
language. Even if the reader has not programmed before, they should be able to
program several types of applications after reading these chapters. We then show the
reader how to create a user interface in VBA. This discussion includes building user
forms, working with several different form controls, using navigational functions, and
designing a clear and professional application. VBA is beneficial as it places all of the
complicated spreadsheet calculations and any other analysis in the background of a
user-friendly system.
6
Chapter 1: Introduction
We then revisit some of the extended Excel functionality topics from Part I of the book.
We show how VBA can enhance the modeling, simulation, and query features of Excel.
Each of these chapters includes an application of a small DSS which combines the tools
taught in VBA with the functionality taught in Excel. These techniques are especially
important to understand in order to build complete DSS applications.
Part III of the book illustrates the relevance and importance of decision support systems
in the fields of industrial and systems engineering, business, and some general
engineering. We strive to accomplish this by showing how to develop DSS applications
which integrate databases, models, methodologies, and user interfaces.
We have developed 27 case studies which we describe in detail in this part of the book.
Most of the case studies consists of developing a complete decision support system and
is based on an important application of IE/OR or business. We have also included some
simpler case studies which apply to general engineering concepts. Through case
studies, students will learn how IE/OR and business techniques apply to real-life
decision problems and how those techniques can be effectively used to build DSS
applications.
Some of our case studies include portfolio management and optimization, facility
location, queuing systems, critical path method, and a student information system. There
are also case studies on forecasting, inventory scheduling, supply chain management,
and capital budgeting. These case studies are just some of the numerous case studies
we develop in order to illustrate how DSS applications can be developed by combining
information technology tools with operations research and business tools to solve
important decision problems. Extensions are listed for each case study for students to
attempt or to use as ideas for other projects.
In this part of the book we also discuss a general proposed DSS development process
(Chapter 23). After the reader has learned how to work in the Excel spreadsheet
environment (Part I) and is able to program in VBA to manipulate Excel objects and
perform advanced calculations and analysis (Part II), they must then be able to combine
these tools to develop a complete DSS application. Chapter 23 describes to the reader
how to plan the DSS application, prepare the spreadsheet, and implement the code so
that the user’s objectives are met and the decision maker will truly be aided by the DSS
system they will use. In Chapter 24, we spend much time on designing good graphical
user interfaces and using good programming principles when developing the DSS. We
give several examples of GUI designs to illustrate the importance of clarity and
consistency when interacting with the user. The programming principles discussed will
help the reader avoid errors in the DSS coding. These are both important chapters for
the reader to understand before developing complete DSS applications.
1.7 Summary
7
Chapter 1: Introduction
¾ There are five components to a DSS: database, model base, knowledge base, GUI,
and user.
¾ Excel is a spreadsheet application with functionality for storing and organizing data,
performing various calculations, and using additional packages for more advanced
problem solving and analysis.
¾ VBA is a programming language that allows for further manipulation of the Excel
functionalities and creation of dynamic applications which can receive user input for
the model base component of the DSS.
¾ The case studies are intended to show the reader how to develop DSS applications
which integrate databases, models, methodologies, and user interfaces.
1.8 Exercises
1.8.1 Review Questions
8
Part I: Excel Essentials
EXCEL BASICS
Excel has many spreadsheet functions that can be considered as basic functionality.
However, there are many features of Excel that allow the user to perform advanced
analysis and modeling. These features can be considered as the extended functionality
of Excel. The chapters in this part of the book cover both topics.
Basic Functionality
Chapter 2: Excel Basics and Formatting
Chapter 3: Referencing and Names
Chapter 4: Functions and Formulas
Chapter 5: Charts
Chapter 6: Pivot Tables
Extended Functionality
Chapter 7: Statistical Analysis with Excel
Chapter 8: Solver and Modeling
Chapter 9: Simulation Using Excel
Chapter 10: Working with Large Data
Chapter 2
Chapter Overview
2.1 Introduction
2.2 Defining the Set of Excel Objects
2.3 Understanding Excel Menus and Toolbars
2.3.1 Menus and Shortcut Keys
2.3.2 Toolbars
2.3.3 Customizing Toolbars and Menus
2.4 Formatting
2.4.1 Formatting Cells
2.4.2 General Formatting
2.4.3 Conditional Formatting
2.5 Entering Data into Cells
2.6 Summary
2.7 Exercises
Chapter 2: Excel Basics and Formatting 2
2.1 Introduction
This chapter provides a review of basic Excel functionality. It is intended for those
readers who have a very brief experience with using Excel or no experience at all. It is
important to be familiar with Excel’s spreadsheet environment in order to increase the
efficiency of developing DSS applications. The formatting techniques will be important
for presenting information and results to the user as well as in developing a clear user
interface.
To clarify this fundamental concept of database organization, let’s use the simple
example of a book. The objects of a book are parts, chapters, pages, and words. A part
contains many chapters, a chapter contains many pages, and each page contains many
words. Understanding the object hierarchy in Excel helps us to quickly locate objects.
Objects will become very important when using Visual Basic for Applications. Each
application—Excel, Word, PowerPoint and Access—has its own object model that is
associated with VBA code. Therefore, in order to manipulate an object in an application,
the programmer must understand the object hierarchy. For a listing of the complete
Excel object hierarchy, refer to Microsoft Excel Online Help from the Help menu option.
The Excel menu consists of the standard Windows options of File, Edit, View, Insert,
Format, Tools, Window, and Help. Excel also includes an additional menu option, Data,
which other Microsoft Applications do not offer (refer to Figure 2.1). Each of these
standard Windows options provides a drop-down list of further options. See Figure 2.2
for an example of the Edit drop-down menu. As you can see, this list features some
options that are specific to Excel; for example, in addition to Delete, we find Delete
Sheet and Move or Copy Sheet.
Figure 2.1 Microsoft Excel menu items include standard Windows options plus a
Data option.
Figure 2.2 Each menu option has a drop-down list of further options. These lists
include shortcut keys and icons referencing toolbars.
Another example of a menu option specific to Excel is the Paste Special option. Paste
Special can be used to paste Values alone instead of Formulas or Formats, Formats
alone instead of Values, and other isolated options. Paste Special can also be used to
Transpose data as it is pasted; in other words, we can transfer a column of data into a
row of data. In Figure 2.3, we have copied the column of red numbers and pasted them
using the Paste Special options of Transpose and Values so that the numbers are in a
row instead of a column and are no longer red.
Figure 2.3 Choosing Transpose and Values in the Paste Special option.
Excel additionally offers several Operations that can be used to transform data. Say we
have a value in a cell that is separate from the range of data we wish to modify. In Figure
2.4, the value 100 is in cell D2. If we want to modify the original column of data in cells
B2:B6 by multiplying each value by 100, we copy cell D2, highlight cells B2:B6, and then
choose Paste Special. In the Paste Special dialog box, we then select Multiply from the
Operation options and press OK. The resulting values, which have replaced the original
column of numbers, appear in Figure 2.5.
Figure 2.4 Selecing the Multiply operation using the value in cell D2.
Figure 2.5 The resulting values after the Paste Special options have been selected.
The drop-down menus also offer cascading features; for example, clicking Fill in the Edit
list displays another shorter drop-down list of options. Note that some shortcut keys are
provided in the drop-down list, such as CTRL+Z for undoing an operation. These keys
simplify standard operations such as copy (CTRL+C), paste (CTRL+V), and cut
(CTRL+X). Icons in each drop-down list refer to toolbars that offer some of the same
options in the menu.
2.2.2 Toolbars
Several toolbars are available in Excel. The two main toolbars that feature the most
common operations are the Standard and Formatting toolbars. The Standard toolbar in
Figure 2.6 contains operations in the File menu option (New, Open, Save, Send, Print,
Print Preview), the Edit menu option (Check Spelling, Cut, Copy, Paste, Copy Format
Painter, Undo, and Redo), and some shortcut icons for functions (which we will discuss
in detail in Chapter 4).
Figure 2.6 The Standard toolbar contains shortcuts to the File and Edit menu
options as well as some shortcuts for functions.
The Formatting toolbar in Figure 2.7 provides modifications for font, including font size,
bold, italics, underline, alignment, color, and indent size. It also includes some shortcuts
to format cells that we will discuss later in this chapter.
Figure 2.7 The Formatting toolbar provides shortcuts to manipulate font as well as
some other modifications.
We can leave these toolbars floating on the screen (as shown in Figures 2.6 and 2.7) or
docked on the menu bar (the default setting). To change a toolbar’s location, we click on
it, hold down on the mouse, and drag it to a new location (i.e. drag-and-drop). We can
determine which toolbars should be displayed on the screen by checking or un-checking
from the list of toolbars that is located at View > Toolbars in the menu (refer to Figure
2.8). For example, if we go to this list of toolbars and check Drawing, the Drawing toolbar
appears on the screen (see Figure 2.9).
Figure 2.8 Selecting which toolbars to view with the View > Toolbars option in the
menu. This option can also be used to customize toolbars.
The Drawing toolbar allows us to place figures such as rectangles, circles, arrows, and
lines on the screen. These are helpful when drawing a diagram or a flow line. We can
also add text boxes to a worksheet which can be formatted differently than text in cells.
We can select many figures by using the white arrow icon in the toolbar and group them
together by right-clicking on the selection. Grouping figures is helpful when constructing
a large diagram made up of several figures that we may want to resize or format as one
group. When working with several drawing images, you can use the Draw drop down
menu on the Drawing toolbar to align the shapes.
To place a figure on the screen, we click on the item we want; the cursor then changes
to a thin cross and we can click and drag to change the size of the figure. To locate more
figure options, we click on Auto Shapes to see a list of available figures. After clicking
and dragging the figure to the screen, we can format it by right-clicking on the figure and
choosing Format Auto Shape or by double-clicking on the figure. A dialog box then
appears.
Figure 2.9 The Drawing toolbar allows a user to place figures and text boxes on the
screen; they can be formatted as needed.
The dialog box is a separate window that has several modifiable options on different
tabs, or categories, within the box. For example, we have changed the fill color, line
color, dash, and weight of the rectangle in Figure 2.10.
Excel offers many other dialog boxes (such as Tools > Options) that allow us to
configure many settings at one time.
Figure 2.10 The Format Auto Shape dialog box allows a user to change the color,
border, and size of a figure, and to specify other specific characteristics.
To add text to a drawing object, we can either insert a text box or right-click on the
drawing object and choose Add Text from the list of drop-down options. This option
creates a text box that automatically matches the shape we have created. Drawing
objects can be useful visual aids in Excel.
Menu contains the standard Windows options of File, Edit, View, Insert, Format,
Tools, Window, and Help, as well as a Data option.
Two main toolbars are Standard and Formatting.
A dialog box is a separate window that provides several modifiable options on
Summary
different tabs, or categories, within the box.
To drag-and-drop, click, hold down on the mouse, and drag.
Excel allows us to customize any toolbar. Customization can simplify the process of
accessing repeated items. To customize a toolbar, we access the list of toolbars from
View > Toolbars in the menu and select Customize from the list. We can also go to Tools
> Customize from the menu. A dialog box then appears that allows us to edit toolbars by
modifying their commands and display options (see Figure 2.11).
Figure 2.11 The Customize dialog box allows a user to create and modify toolbars.
In the Toolbars tab, we select the toolbar that we wish to modify. We can also create a
new toolbar with this option. To do so, we click New and name the toolbar. The Attach
key attaches specific toolbars to a particular workbook. In the Commands tab, we can
modify commands by choosing a command from a specific menu category and using
drag-and-drop to add the command to the toolbar. The Options tab lists some display
settings for toolbars.
For example, if we select the Drawing toolbar from the Toolbars tab, it appears on the
screen. We can select any command from any category on the Commands tab to add to
the Drawing toolbar. Suppose that we frequently open new files while working on
drawing figures; we would then want to add the Open icon to the Drawing toolbar. To do
so, we choose the File category and drag-and-drop the Open icon from the list of
commands to the Drawing toolbar. The Drawing toolbar in Figure 2.12 now includes the
Open icon.
Note that we can also add or remove buttons from the Standard and Formatting toolbars
by clicking on the small arrow at the end of the toolbar. This will display a limited list of
icons available for addition to either toolbar. For example, the Format Painter icon was
already added to the Standard toolbar.
We will discuss customizing toolbars and menu items in more detail in Chapter 18. At
that time, we will combine VBA code functionality with toolbar icons and menu options.
Figure 2.12 The Commands tab allows a user to add commands to a selected toolbar.
2.4 Formatting
Excel has the incredibly helpful ability to format cells, ranges, and entire worksheets, as
well as other objects. We have already learned how to format figures from the Drawing
toolbar by using a dialog box. We also use a dialog box to format cells, or groups of
cells, called ranges.
To format a cell, we either click on a cell and choose Format > Cells from the menu, or
we right-click on the cell and choose Format Cells from the drop-down list. A dialog box
with six tabs then appears (see Figure 2.13). The first tab, Numbers, specifies different
types of numbers for different purposes. For example, if we wanted to create a
spreadsheet for financing calculations, we would have to format the entries in our
spreadsheet as Currency or Accounting, depending on the features desired. We may
either wish to create a table of Dates or Times with a particular uniform format, or a table
of Numbers with one decimal place each. The Numbers tab (shown in Figure 2.14)
describes and lists the options available for each Number type. This tab also includes a
Custom type option. By using symbols such as (), -, and #, we can define the number
formatting that we want to apply to a spreadsheet. The standard format for telephone
numbers, (###) ### - ####, serves as a good example of Custom type.
Figure 2.13 The Format Cells dialog box provides six categories of formatting options.
Figure 2.14 The Numbers tab lists several options for formatting numerical entries.
The Alignment tab sets the horizontal and vertical placement of a cell or range data and
also determines indention and text orientation. The Format Cells dialog box in Figure
2.15 presents a format that places an entry at the bottom of a cell. The entry has no
particular horizontal placement or indentation, and appears with a 45-degree orientation.
The Text control options reveal that the cell has been merged with one or more cells.
Figure 2.15 The Alignment tab offers formatting options for text or other entries.
The next tab, Font, does not differ from the Font options found in other Microsoft
applications. We can set the font type and size as well as color and other highlighting
features. In Figure 2.16, we have set the font to 10-point Arial in red with bold italics.
The Border tab allows us to add a variety of border styles to a cell range. First, we pick
the style and color; then, we use the Outline and Inside presets to quickly format the
cells. We select one line at a time by clicking within the border box. For instance, to
make the top line of a table blue, we select the color blue and then click on the top line of
the box. We can also click within the border box to eliminate one line of a border at a
time. In Figure 2.17, the entire cell range has a black outline, while a dashed red line
separates the individual cells.
Figure 2.16 The Font tab provides standard font manipulation options.
Figure 2.17 The Border tab enables a user to add a variety of border styles to a cell
range.
The Patterns tab, useful for highlighting a data range, allows us to fill a cell or range with
a color and/or pattern. In Figure 2.18, we have given our cell a color and pattern.
Figure 2.18 The Pattern tab gives a user the option of adding colors and patterns to
cells.
The last tab, the Protection tab, enables us to lock or hide our cells. To lock cells, we
protect our sheet using the Tools > Protection > Protect Sheet option in the menu. The
Protect Sheet option prevents anyone from entering or changing data in locked cells.
This option is beneficial when creating a form where some cells should be free for users
to input information, but other informational cells should not be modified by the user. The
hidden option hides any cell formulas or functions if the worksheet is protected. This is
only applicable when viewing the formulas in a worksheet. (We will discuss this in more
detail in Chapter 4.)
Figure 2.19 The Protection tab allows a user to lock or hide cells.
After we have selected one or more cells and formatted them, we may wish to format
other cells in the same fashion. To do this without repeating all of the previous formatting
modifications, we highlight the cells that we have just formatted and click the icon in
the Standard toolbar. Keep in mind that this icon may be in another toolbar depending
on how we have customized them. If we select any new cell or range of cells, Excel
automatically formats them to match the highlighted cells. The data in the cells does not
change, only the formatting.
The Format menu option features formatting applications for rows and columns as well.
Auto Fit can alter the dimensions of rows and columns, making them as wide as
necessary to show all values entered. Rows and columns can also be hid or revealed
and, with Standard Width, evenly distributed through the spreadsheet (see Figure 2.20).
We can also format sheets by renaming them, hiding and unhiding them, and adding a
background image to the entire sheet.
We can apply the Auto Format option, shown in Figure 2.21, to any cell range. It offers
several preset table designs that we can apply to a spreadsheet, allowing us to present
data formally or organize personal information more effectively.
Figure 2.21 Auto Format applies preset formatting options to a range of cells.
We can also choose Style from the Format menu option list to apply general formatting
to the entire worksheet. A dialog box appears that displays the current formatting for
each of the six formatting categories. Uncheck any aspects that should not be included
in the format. To change some categories, we choose Modify (see Figure 2.22).
For example, if we want to have a bold and italicized typeface for a worksheet, we
choose Modify to open the Format Cells dialog box, go to the Font tab, and choose the
bold-italics option. We click OK to return to the Style window. Pressing OK again makes
everything in the worksheet in bold-italics. We can continue to format specific cells from
now on, but the default format for the worksheet will have bold-italic font.
Figure 2.22 Formatting the Style of a worksheet creates a new default format.
Conditional Formatting, another Excel tool, is the last option on the Format menu; it
only formats cells that meet a predetermined condition or a set of conditions. For
example, suppose we want to change the format of all numbers over 35 in a set of
numbers. First, we highlight the set of numbers and then choose Format > Conditional
Formatting from the menu. At this point, the menu shown in Figure 2.23 appears.
Figure 2.23 The first step in Conditional Formatting is specifying the condition.
Next, we specify the first condition for the cells. The option box on the far left allows us
to choose between cell value with Cell Value Is, and formula value with Formula Is.
After that, we select from the list of conditional statements in the middle box. In Figure
2.24, we choose Cell Value Is, and greater than. Then, we enter the value 35 in the next
box. Instead of entering a numerical value, we can also choose to apply the conditional
statement to a range of cells.
Then, we use the Format button to select the desired formatting style for the cells that
meet this condition. The formatting options we choose can be previewed in the space
next to the Format button. We select bold-italic, red font. We can then select Add to
create another condition or OK to finish. In Figure 2.25, our initial list of numbers has
been formatted so that the numbers greater than 35 are bold-italic and red, while the
values up to 35 maintain the default font setting.
To make comparisons outside of the selected cell or cells, we use the Formula Is option
instead of Cell Value Is. For example, we may want to compare values of cells in
columns A and C of the above example to set the formatting condition of the cells in
column B. (See Chapter 4 for a more detailed discussion of formulas.) To delete a
conditional format applied to a range of cells, we just select the range of cells and
choose Edit > Clear Formats from the menu options.
Conditional Formatting is beneficial when we are working with large sets of data and
want to distinguish certain values from others. We may be looking for particular results
or patterns of data that we can specify by creating conditions and formatting them
differently than the other data.
Figure 2.26 These simple values have been entered by typing directly in the cells.
To copy a cell or range of cells to surrounding cells, we click on the cell or range that we
want to copy and place the cursor in the lower right-hand corner. The cursor then
transforms into a small cross. Then, we click-and-drag the cursor so that a box
encloses the entire area to which we would like to transfer data. In Figure 2.27, we have
highlighted the set of numbers in column B and then moved the cursor to the right to
copy the set into column C.
Excel automatically enters data if it detects a pattern. For example, if we are creating a
list and numbering down a column of cells, we type “1, 2, 3” in different cells and then
use the click-and-drag method described above to copy the cells to the end of the
column. This time, however, Excel recognizes the numerical pattern and, instead of
copying “1 2 3” again, continues the count for us (see Figure 2.28). If we are numbering
by even or odd numbers only, Excel creates the same pattern.
Figure 2.28 Excel automatically numbers sequentially when copying from a pattern it
recognizes.
Two useful shortcuts simplify these functions. The first, a shortcut for the click-and-drag
option, allows us to copy a formula to the end of a list of data. To do so, we use one of
two useful shortcuts. The first uses the click-and-drag option. If we want to copy a
formula to the end of a list of data, we can double-click on the small cross cursor on the
cell/s that we want to copy. That is, in the above figure, if we have a set of data in
column C for which we are assigning numbers as shown in column B, instead of clicking-
and-dragging the small cross to enumerate these cells, we can highlight the cells with “1,
2, 3” and then double-click the small cross cursor. Excel then continues the
enumeration, based on the pattern in column B, until the adjacent data in column C
comes to an end.
One last note on moving data in Excel: To move an entire table or even one cell, we
select it and put the cursor somewhere on the border of the selection. We then see an
arrow or another change in the cursor. Then, we drag-and-drop the entire section at the
desired location in the worksheet. However, if we drop the selection on an area that
already has data entered in the cells, Excel provides a warning that it will replace this
data with the new selection.
Simple values: Numbers and text that are entered into cells.
Click-and-drag: Used to copy a highlighted cell or range of cells to an
adjacent cell or range of cells.
Summary
2.6 Summary
¾ Excel objects are elements that you can manipulate in spreadsheet applications
and VBA programming. They follow an object hierarchy, which is an ordered
listing of all the Excel objects. The object model is the entire system of objects.
¾ Menus and toolbars create Excel’s user interface; they provide options for
working in the spreadsheet environment.
¾ The Excel menu consists of the standard Windows options of File, Edit, View,
Insert, Format, Tools, Window, and Help, as well as Data, an extra menu option
that other Microsoft Applications do not have.
¾ Excel features extra options such as Delete Sheet and Paste Special in the Edit
menu.
¾ The two main toolbars on which you can find the most common operations are
the Standard and Formatting toolbars.
¾ You can determine which toolbars should be shown on the screen by checking or
un-checking from the list of toolbars. Select View > Toolbars from the menu.
¾ To customize a toolbar, go to the list of toolbars from View > Toolbars on the
menu and select Customize from the list. (You can also go to Tools > Customize
from the menu). A dialog box will appear that allows you to edit the toolbars by
modifying commands and display options.
¾ To format a cell, click on it and choose Format > Cells from the menu or right-
click on the cell and choose Format Cells from the list. A dialog box with six
different tabs will appear: Numbers, Alignment, Font, Border, Pattern, and
Protection.
¾ Rows and columns can be formatted by setting a particular height or width, using
Auto Fit to make them as wide as necessary to show all values entered, hiding or
unhiding them, or using Standard Width to evenly distribute them.
¾ The Auto Format option in the Format list offers several preset table designs that
you can apply to a spreadsheet.
¾ Style from the Format menu option list applies general formatting rules to the
entire worksheet.
¾ Conditional Formatting formats cells only if they meet a specified condition. To
specify the condition for the cells, choose between Cell Value Is and Formula Is,
and select from a list of conditional statements.
¾ Data entry in Excel pertains to four different types of data: numbers, text,
formulas, and Boolean values (true or false). Numbers and text are simple
values. To enter this type of data into a table, type a number or text directly in
each cell.
2.7 Exercises
2.6.1 Review Questions
1. Use the Drawing toolbar to create the following AutoShape. (The fill color should be
yellow and the border should be blue.)
2. Use commands from various categories to custom create the toolbar shown below:
3. A professor uses the table shown in Figure A to take roll in his class. He writes a “Y”
in the Roll Call column if a student is present, an “N” if a student is absent without an
excuse, and “excused” if the student has an excused absence. Enter this table into
an Excel spreadsheet. Then, following Figure B, use the Conditional Formatting
option to highlight all students who were not present in class.
Figure A Figure B
4. The table in Figure A displays the amount of sales dollars earned and the
percentage of returns made for three new products recently introduced onto the
market. Using the options on the Formatting toolbar, enhance this table to make it
resemble the table in Figure B.
Figure B
5. A financial analyst is comparing the following list of stock prices: 1.25, 0.35, 2.49,
3.15, 5.87, 4.63, 1.02, 0.33, 2.71, 0.99. Enter these prices into an Excel worksheet
and display them as fractions rounded to the nearest sixteenth.
Since all of the substances dissolved, the engineer decides to create solutions that
are more concentrated. Her new observations include the following:
Create a table to compare the results of the two experiments by entering this data
into Excel. Use the AutoFormat option to format your table.
7. Enter the data below into a spreadsheet by using the click-and-drag method:
1 10 1 a hello
2 9 3 b hello
3 8 5 c hello
4 7 7 a hello
5 6 9 b hello
6 5 11 c hello
7 4 13 a hello
8 3 15 b hello
9 2 17 c hello
10 1 19 a hello
8. A student keeps a list of his bill payment due dates, shown in Figure A. He wants to
identify all the dates in October. Enter his list into a spreadsheet. Using conditional
formatting, highlight all the dates in October and cross out all remaining dates so that
the list resembles Figure B.
Figure A Figure B
9. A bank uses spreadsheets to record the transactions made by each of its account
holders. This information is then used to prepare bank statements for the account
holders as shown below:
Transa
ction Transacti Account Descripti Withdrawal Deposit Interest Service
No. on Date No. on Amount Amount Earned Charge
April 11, withdraw
00178 2002 111223333 al 30.25
May 5,
00189 2002 111223333 deposit 52.48
August 7,
00211 2002 444556666 Interest 15.133
November
00245 23, 2002 111223333 charge 2.50
December
00277 13, 2002 444556666 interest 21.245
Create this table in Excel. Make the statement easier for an account holder to read
by using the options under the Numbers tab in the Format Cells dialog box to change
the number types of the cells in the following ways:
a. Remove the leading zeroes from the transaction numbers.
b. Display the transaction dates in the format mm/dd/yy.
c. The account numbers are based on the account holders’ social security
numbers. Display these numbers as social security numbers in the format
XXX-XX-XXXX.
d. Deposits and earnings result in an increase in the account balance, but
withdrawals and charges result in a decrease. Show currency symbols on all
monetary transactions. Display all negative transactions within parentheses.
10. A department store must predict the demand for various items of women’s apparel
for the upcoming months before production begins. The following table displays the
forecasted demand in thousands for various items for January through June. Create
this table, paying particular attention to the alignment and border formatting
properties.
12. The table below lists the names, games, total points, and points per game of five
basketball players (cells A2:D7). The column D3:D7 uses the data from cells B3:C7
to compute each player's points per game. Each cell of column D3:D7 contains a
formula that divides the number of points by the number of games of each player to
calculate their points per game.
a. Copy the entire table, including the calculated points per game, and paste
only the values to the cells A13:D18.
b. Copy the names from the original table and paste them in a row in cells
A20:E20.
c. Copy the entire table and transpose it to cells A22:F25.
13. Below is a table with the annual rate of interest paid by 3-month Treasury bills for
each month in 1989. To find the annual interest earned on $1.00 invested at the
current T-bill rate, we use the calculation (1 + (annual rate)/100). Therefore, it would
be easier to compute earned interest if the column of annual interest rates were
divided by 100. Use Paste Special Operations to transform the column of annual
interest rates. (Hint: You will need to use a cell outside of the table.)
14. The table below stores quarterly sales for four products. Copy this data to a range
below so that the sales values are read as rows and not columns.
15. Copy the table below to an Excel spreadsheet. This table contains monthly prices of
the M and N stocks. Changes in stock price are also calculated.
a. Highlight each value at which M and N went up more than 3% in purple.
b. Highlight each value at which M and N went down more than 3% in red.
16. A professor is reviewing her students’ grades for the semester (see the table below).
Using conditional formatting, help her find the A’s by highlighting them in red, the B’s
by highlighting them in yellow, and the F’s by highlighting them in blue. [CONSIDER
ADDING A GRADE SCALE: VALUES FOR EACH GRADE]
Student Grade
1 54
2 79
3 82
4 97
5 90
6 90
7 59
8 61
9 63
10 91
11 93
12 87
13 55
14 65
15 69
16 55
17 55
18 54
19 68
20 83
21 70
22 73
23 70
24 59
25 87
26 68
27 57
28 65
29 74
30 51
17. John is trying to review his bank balance for the last several weeks. Format the data
he has (see below) by changing the dates into the “mm/dd/yy” format. Then format
the balance values as currency so that the negative values are red and in
parentheses.
Date Balance
1 37660 238
2 39084 -321
3 38354 -456
4 38650 -9
5 38270 1114
6 39003 -115
7 38986 286
8 38985 495
9 38657 960
10 39050 620
11 38399 1313
12 39290 -441
13 38070 9
14 39727 -45
15 39183 601
16 38020 541
17 39835 -348
18 39019 619
19 38512 -202
20 39676 850
18. Review the quarterly sales for an office supplies company below:
19. Create your own accounting table. Number 20 entries using the click-and-drag
method. Enter dates in “Month dd” format. Enter positive values with a “+” sign
before the number. Enter negative values with a “-” sign before the number. Highlight
any values greater than $1000 in yellow.
20. Create a new format style. The following are the features of the new style:
a. Numbers are formatted as percentages with 2 decimal places.
b. The text and numbers are centrally aligned.
c. The font size is 12pt and the font is set to Time New Roman.
d. The table has left, right, top and bottom borders.
e. The table is shaded using the blue color.
Use the new style to format the data on the table that corresponds to Hands-On
Exercise 13.
Chapter Overview
3.1 Introduction
3.2 Referencing Cells
3.2.1 Relative Referencing and Absolute Referencing
3.2.2 R1C1 Notation
3.2.3 Referencing Other Worksheets and Workbooks
3.2.4 Circular Referencing
3.3 Names for Cells, Ranges, and Worksheets
3.3.1 The Name Window
3.3.2 Define
3.3.3 Apply
3.3.4 Create
3.3.5 Formulas and Constants
3.3.6 Natural Range Names
3.4 Summary
3.5 Exercises
Chapter 3: Referencing and Names 2
3.1 Introduction
This chapter discusses how to create references and names in the spreadsheet
environment. These are basic but very important concepts. This chapter may be most
useful for the reader who is inexperienced in Excel, but we also recommend it be
reviewed by experienced users. Referencing and names will be used often in creating a
DSS and these concepts should be clear to the user in Excel before they learn
referencing and names in VBA.
When typing a formula or a simple operation in a cell, we use the names of other cells to
refer to the values that they contain. That is, if we want to refer to a value in cell B3, we
type:
=B3
We can also just type an equal sign (=) in the cell and then click on cell B3 with our
cursor. This method of referencing cells is convenient when using a large range of cells
in a formula. For example, we can type “=SUM” and then highlight an entire range of
cells to complete the formula. We use this method when constructing formulas to
prevent making errors when referencing appropriate cells.
Referencing is important for using cells in formulas and in VBA code. In order to take
advantage of the capabilities of organizing large data in a spreadsheet application, we
must be able to refer to it properly. We can reference cells, ranges, worksheets, and
workbooks in a formula or in VBA code.
There are four basic ways to refer to a cell: relative, absolute, row absolute, and column
absolute referencing. To express the importance of the differences between these
referencing types, let’s consider the following example. Figure 3.1 features two columns
of data; the sums of each column are located below the “Data” table. Let’s assume that
we created the small “Sum” table by entering a SUM function in cell B16. If we want to
copy the SUM function from B16 into the adjacent cell, C16, to calculate the sum of the
second column of data, we copy and paste the SUM function in that cell. We can also
place the cursor at the bottom right-hand of cell B16 until it transforms into a small cross,
and then click-and-drag to the adjacent cell, using the method we discussed in Chapter
2. Excel automatically transforms the values and names of the cells in the function
according to the direction of the operation. That is, if we copy to one cell below the
original function, Excel automatically advances the row number by one unit. If we copy
one cell to the right, as in this case, Excel automatically advances the column letter of
each cell name by one unit. The functions in cell B16 and C16 are shown in the code
below; we can see that Excel copied the function correctly and automatically changed
the column letter by one unit.
Note in the figure that the format for both functions remains the same. This is an
example of relative referencing. Excel automatically shifts the row and column names in
the function relative to the displacement of the copy and paste action.
Figure 3.1 Relative referencing copies the SUM function from B16 to C16. Both the
row and column values change.
column letter row number: column letter row number such as B2:B8
The only difference between the format of relative referencing and absolute referencing
is the addition of a dollar sign. The $ implies that the value that follows cannot be
changed. For absolute referencing, the format is:
$column letter $row number: $column letter $row number such as $B$2:$B$8
That is, neither the column nor the row values can change. So, B2:B8 would be
expressed as $B$2:$B$8 in absolute referencing.
For example, using the same data columns and sums from Figure 3.1, we want to
calculate a product (see Figure 3.2). We multiply both sums (from B16 and C16) by the
number in cell E4 and place the result in the Product cells. We define the product
operation in E12 as follows:
=E4*B16
Copying this formula to E13 automatically uses relative referencing to create the
formula:
=F4*C16
=E4*C16
Excel detects that we are copying a function one cell to the right; as a result, it
automatically increases all column letters by one unit. In this case, we need absolute
referencing because we want to keep the Multiplier value, E4, the same in both formulas
(see Figure 3.2a).
To enforce this, we change our formula in E12 to:
=$E$4*B16
This way, when we copy the formula to the next product cell, F16, Excel still
automatically transfers the reference to C16; however, the E4 reference will not be
updated (see Figure 3.2b).
(a)
(b)
Figure 3.2 Absolute referencing ensures that neither the row nor the column names
changes when copying a formula. In (a), E4 automatically changes to F4 when the
default of relative referencing is assumed. Absolute referencing, (b), maintains $E$4 in
both formulas.
Row absolute and column absolute referencing are similar to absolute referencing
because the placement of the $ prevents the following value from changing when a
formula is copied. Row absolute referencing prevents the row value from changing when
a formula is copied to another cell and column absolute referencing prevents the column
value from changing. (Note that the shortcut key F4 changes the reference type;
repeatedly clicking F4 switches between relative, absolute, row absolute, and column
absolute referencing.)
To make sure all of the first set of numbers comes from row 2 only we use row absolute
referencing. We type the first number as B$2 in the first cell, B3, so that the $ implies
this number will always come from row 2. We yype the second number as $A4 so the $
implies this number will always come from column A (refer to Figure 3.3a). As we can
see in Figure 3.3b, when the formula in B3 is copied to the other cells of the table, the
row value is 2 for the first number and the column value is A for the second number.
Excel automatically updates the column values for the first number and the row values
for the second number; for example, B$2 becomes C$2.
(a)
(b)
Figure 3.3 Row absolute referencing prevents the row value from changing when
copying a formula to another cell. Here B$2 is transferred to C$2 in the copied formula;
row 2 is constant. Column absolute referencing keeps the column value from changing
when a formula is copied. The multipliers are referred to as $A3 and $A4, keeping the
column name the same.
There is one other type of cell referencing known as R1C1 notation. Instead of referring
to a cell by its row and column value, R1C1 notation describes a cell’s position relative to
the cell in which the formula appears. That is, if we are inserting a formula into cell A1, it
would be the origin cell and referred to as R1C1. (The name of a cell is written without
brackets.) A2 would then be R[1]C[] (or R[1]C) to reflect that its position is one row
below A1, and B3 would be R[2]C[1] to convey that it is two rows below and one column
to the right of A1. It is possible to switch row and column titles to R1C1 notation by
choosing Tools > Options from the menu and clicking on the General tab. We check
R1C1 reference style from the list of options on that tab. Our rows and columns are now
both numbered. If we enter a formula into a cell, it uses R1C1 notation as the default
referencing.
For example, if we have already transferred to R1C1 notation and wish to perform a
simple calculation by typing a formula in cell A1, we note the R1C1 notation of the cells
we reference. In Figure 3.4a, we have entered the simple formula in A1 (which is now
R1C1):
=R[1]C[2]
This is the first value in the “Data” column, which implies that the value 95 is one row
below and two columns to the right of our original cell. As another example, in Figure
3.4b, we calculate the sum of the first two values in the “Data” column. Our origin cell is
now A2 (which is R2C1); to obtain the sum of 95 and 32 from the “Data” column, we use
the following function:
=RC[2] + R[1]C[2]
The value 95 is in the same row, but one column to the right of A2 (R2C1), and 32 is one
row below and two columns to the right of R2C1. Note the notation R1C1 is used to
denote the name of a cell, which is based on the numbered rows and columns in the
worksheet, and the notation R[1]C[1] is used to denote the location of a cell relative to
the origin cell. In Figure 3.4a, we can see that the name of the origin cell appears in the
upper left-hand window as R1C1, and the formula appears in the center top window as
=R[1]C[2].
(a) (b)
Figure 3.4 R1C1 notation displays the displaced position of the referenced cell to the
origin cell where the function is entered. (a) R1C1 is the name of the origin cell and
R[1]C[2] is the position of the referenced cell.
Even though this type of referencing may seem tedious, it can be very useful. For
example, when copying a formula using R1C1 notation, it appears identical in each cell
to which we copy it. That is, if we copy the formula from Figure 3.4b to the three rows
below it, each time we are calculating the sum of the data value in the same row of the
“Data” column and one row below it in the “Data” column. Therefore, the formula value is
always the following:
=RC[2] + R[1]C[2]
This is relative to each new origin cell we copy to (see Figure 3.5). R1C1 notation is
therefore a helpful check method to ensure that we copied the formulas correctly. We
can even work in the default-referencing mode (A1, A2, etc.) and then switch to R1C1
notation just to check the formulas. (Notice that only relative referencing is used with
R1C1 Notation.)
Figure 3.5 Copying formulas using R1C1 notation shows the same formula in each
copied cell. Here, the formulas in R2C1 through R5C1 are all =RC[2] + R[1]C[2].
Excel offers a formula view that displays cells by their formulas instead of their
calculated values. To use it, we choose Tools > Options from the menu and click on the
View tab. Then, we select Formulas from the Windows Options. For our example above,
the screen resembles Figure 3.6. Again, the formulas in cells R2C1 to R5C1 are all the
same.
Figure 3.6 The formula view shows only the cell formulas and not the calculation
results.
R1C1 notation also becomes important when working with VBA code. When writing
code, R1C1 notation is the clearest way to ensure that we are referencing cells correctly
in our formulas.
R1C1 notation: Refers to a cell’s position relative to the origin cell where the
formula is entered.
Summary
Formulas can also reference cells outside of the sheet in which the formula is written. To
do this, we simply type the name of the sheet followed by an exclamation mark (!) before
the cell or range name. Suppose that we have several data tables in “Sheet1” of our
workbook and we keep all of our formulas in “Sheet 2.” To refer to the data value in cell
A1 in “Sheet1” while typing a formula in “Sheet2,” we use the format:
=Sheet1!A1
Assume we have created two sheets, “Data Sheet” and “Formula Sheet,” as illustrated in
Figure 3.7. In the “Data Sheet,” we have two columns of data: “Data A” and “Data B.” In
the “Formula Sheet,” we want to find the product of these two columns of data (see
Figure 3.8). To reference the data in the “Data Sheet” in a formula, we must type in the
“Formula Sheet.” As demonstrated in the format above, we must use the name of the
“Data Sheet” before we type the appropriate cell name from the “Data” columns. Notice
however that these sheet names have spaces in them; if there is a space in the sheet
name, we must use single quotation marks (‘') around the sheet name and before the
exclamation mark.
Figure 3.7 Two sheets in a workbook: one for data and the other for formulas.
Therefore, to multiply the first value of “Data A” and “Data B, ”which are cells B3 and C3
respectively in the “Data Sheet,” we use the following formula in the “Formula Sheet”:
(See Figure 3.8b). We can then copy this formula to the “Product” column in the
“Formula Sheet” to reference the other values from the “Data Sheet.”
(a)
(b)
Figure 3.8 The formulas in the “Formula Sheet” reference the data in the “Data
Sheet.” (a) Two columns of data in the “Data Sheet.” (b) The product of these two data
columns in the “Formula Sheet.”
To refer to a cell in a different workbook, we include the title of this workbook and the
sheet number in the reference. For example, if we want to refer to the data cell A1 in the
first sheet of a workbook titled “Data,” we use the format:
=[Data.xls]Sheet1!A1
Note that if there is a space in our workbook title, for example “Collected Data,” we must
add single quotation marks around the workbook and worksheet name; in this case, the
format is:
=‘[Collected Data.xls]Sheet1’!A1
Also note that if the workbook we are referring to is not currently open in Excel, we must
type the complete path of the workbook and worksheet surrounded by single quotation
marks, for example:
The first possible solution to this problem requires us to rearrange our references or to
modify our formula. However, if neither cannot be done, Excel offers another tool to aid
in sequential calculations. If we go to Tools > Options > Calculations and select Iteration,
Excel will then perform a specified number of iterations, or repetitions, of the
calculations, to try to find a solution applicable to all equations. The values of the
variables are changed by a mathematical model (the Gauss-Seidel Iteration), which
converges (or stops) when the maximum change in any cell between iterations is at
most .001 (or any other number specified in the Calculations tab).
The easiest way to create a cell or range name is by using the name window. To
highlight a cell or a range of cells, we click on them then, we click in the name window,
which appears at the upper left-hand part of the screen (just above cell A1) and type a
name for the range. To name the first value in the table in Figure 3.9(a) as “Length,” you
click on cell C3 and then enter the name in the name window, as illustrated in Figure
3.9(b). To see all current names in the active workbook, we simply click on the drop-
down arrow next to the name window; F3 works as a shortcut for this operation. We can
find the cell references of these range names by clicking on a name from this list; the
referenced cell will then automatically be selected.
To move a range name, that is to change the cells it references, we can simply click-
and-drag the cell or range to another location. That is, if we name the cell C3 in Figure
3.9 as “Length” and then decide to shift the data column to the right, we just click-and-
drag this cell to the right so that the data cell name remains the same. The reference for
the “Length” range name is now D4. We can work with cell references directly by using
the Define option for creating names.
3.3.2 Define
To define a name in Excel, we choose Insert > Name from the menu and select Define.
For example, if we have a table with some data, we can name the values with a
description. In Figure 3.9a, the default name for the first cell in the “Parameters” column
is C3; this name appears in the upper left-hand corner of the window.
(a) (b)
Figure 3.9 Defining a cell name. (a) The name of the cell with the value 45 is C3. (b)
The name of the same cell is “Length.”
To define another name for this cell, we choose Insert > Name > Define from the menu.
The screen shown in Figure 3.10 then appears. First, we check that we are naming the
correct cell by looking at the bottom of the window under Refers to. The formula needed
to name C3 in “Sheet 1” is the following:
=’Sheet1’!$C$3
If this information is correct, we can then enter a name for this cell at the top of the
window under Names in workbook. Excel automatically provides a name, called a
natural range name, if there is an obvious reference from our spreadsheet. In this case,
the name “Length” automatically appears. However, we could use a different name by
typing it in this area. Now, we confirm that the name of the cell in Sheet1!$C$3 has been
changed to “Length.” Figure 3.9b demonstrates that the new name “Length” now
appears in the upper left-hand corner for cell C3.
Figure 3.10 Selecting Sheet1!$C$3 as the cell to be named and entering the name
“Length” in the top of the window.
We can continue this process by naming C4 “Width” and C5 “Depth.” These cell names
stay the same even if we change their data values. This tool is particularly useful in VBA
coding when referring to cell names in calculations. The names can also be used to
reference these cells in formulas. This simplifies the cell referencing options discussed in
Section 3.1 so the cell name alone can refer to specific values. For example, to refer to
the “Length” value, we can now type the following. (Notice no quotations are used.)
=Length
We can also name a range of cells. To do so, we highlight the entire range and again
chose Insert > Name > Define from the menu (see Figure 3.11a). We define
Sheet1!$C$3:$C$5 as the entire range (as seen at the bottom of the Define Name
window) and rename this range “Parameters.” When we select that range of cells in our
worksheet, the name “Parameters” appears in the upper left-hand corner of the window
(see Figure 3.11b).
(a) (b)
Figure 3.11 (a) Selecting and renaming the entire range to be defined. (b) The range
name “Parameters” now appears in the upper left-hand corner of the window when that
range is highlighted.
Several range names can be added in the Define Name dialog box by using the Add
button. Name references can also be modified in this window by selecting a name from
the list and then changing the values in the Refers to section of the dialog box. Names
can also be deleted using the Delete button; we just select a name from the list and click
Delete. (Note that only one name can be deleted at a time.)
3.3.3 Apply
If we have previously referenced cells or ranges in a formula before naming them, the
names will not be automatically updated. For example, in Figure 3.12a, “Volume” is
calculated by multiplying all three data values; the formula is represented by the names
of the original cells:
=C3*C4*C5
To update this formula with the new names, we click Insert > Name > Apply from the
Excel menu. The window in Figure 3.13 then appears. Here, we can select the names
that we wish to apply and leave the default options selected. Excel then searches the
worksheet for any formulas that reference these cell names and updates them. In Figure
3.12b, Excel is updating the original formula for “Volume” to reflect the defined cell
names:
=Length*Width*Depth
(a) (b)
Figure 3.12 Updating a formula so it correctly references newly named cells. (a) Even
though cells C3 through C5 have been renamed, the formula previously entered still
references their original names. (b) After choosing Insert > Name > Apply from the
Excel menu, the formula is updated to reflect the new names of the referenced cells.
Figure 3.13 Selecting the names desired to apply to the formula cell. Other options are
also shown in this screen.
3.3.4 Create
Create provides another quick way to name cells when we already have data labels in
our table. Our example in Figure 3.12a already includes the labels “Length,” “Width,” and
“Depth” in the table. Instead of defining each cell name separately, we can alternately
highlight the entire table (labels and cell values) and select Insert > Name > Create from
the menu. The window in Figure 3.14(a) then appears. We select Left column from the
Create Names screen to indicate that we want all of the names in the left column of the
highlighted selection to be the names of the corresponding data cells. Figure 3.14(b)
reveals that the names have been created for cells C3 through C5. When using this
method, we must remember that it is necessary to highlight the table labels before
creating the range names.
(a) (b)
Figure 3.14 Creating names by selecting a table with values and labels. (a) Choosing
Left column because the cell labels are in the left column of the selected table. (b) Using
the names of the labels to name the corresponding cells.
The same procedure can be followed to name an entire range of data. For example, if
our “Parameters” table is expanded to include several different products, we could name
each row and column of data by their labels. In Figure 3.15a, we create range names for
each row of the table by highlighting all of the rows and their table labels (B3:E5),
choosing Insert > Name > Create, and selecting Left column. We create range names
for each column of the table by highlighting all of the columns and their table labels
(C2:E5), choosing Insert > Name > Create, and selecting names for each column by
selecting Top Row (see Figure 3.15b).
(a)
(b)
Figure 3.15 Creating range names by (a) highlighting the row labels and the data cells
and (b) selecting the column labels with all of the data cells.
Note that the cells do not have names now, only the same ranges. To name each
element of our table completely, we must use the Define Name option again. We select
an element from the table, choose Insert > Name > Define from the menu and type an
appropriate cell name. For example, in Figure 3.16a, we select the first element of our
table, C4, and use the method just described to go to the Define Name window. We
define the name “Length_Prod1” for this cell. In Figure 3.16(b), this element is named.
We continue this process to name all the other elements in the table. Alternately, we
could select the entire table, and use Insert > Name > Define to give the name
“Parameters_Table” to the entire range of cells. Note that we cannot use spaces when
we are naming cells. Instead, we must replace a desired space with an underscore (_).
(a)
(b)
Figure 3.16 Using Define Name to create names for each element. (a) Using the name
“Length_Prod1” to name cell C4 in “Sheet1.” (b) The name now appears for this element
of the table.
We can also define names for formulas and constants. Creating a name for a constant is
simple. First, we select Insert > Name > Define from the menu. Then, instead of
selecting a cell or range, we type an “=” followed by a number value in the Refers to area
at the bottom of the window (see Figure 3.17). This method is most helpful if we are
defining multiplier values such as rates or weighted constants.
Figure 3.17 Defining a constant by the name “Constant” and giving it the value 3.5.
We can define a formula name by typing the formula in the Refers to area at the bottom
of the Define Name window. For example, in Figure 3.18a, we create a formula named
“product” and define this formula in the Refers to area:
=Sheet1!D4*Sheet1!D5*Sheet1!D6
The substitution of names for formulas simplifies calculations that use the results of
several different formulas. For example, we may want to take the average of a product
calculation and sum calculation. In Figure 3.18(b), we define the formula for sum by
using the Excel function SUM. We examine functions in detail in Chapter 4; here, the
function allows us to determine the sum of a given cell range:
=SUM(Sheet1!D4:D6)
To find the average of the results of these two formulas, we refer to the named formulas.
We use another Excel function called AVERAGE to calculate the average value:
=AVERAGE(product, sum)
(a)
(b)
Figure 3.18 Creating two formula names. (a) Defining the product formula as the
product of cells D4 through D6. (b) Defining the sum formula as the sum of cells D4
through D6.
Again, naming constants and formulas can be very useful when using VBA code. As you
will see in Part II, VBA code becomes much clearer when we use names instead of cell
and range references in formulas. Naming also helps us keep data and calculations
organized in a spreadsheet application.
Excel offers another feature that allows us to convert column and row titles of data tables
into corresponding range names. To do so, we go to Tools > Options > Calculations and
select Accept Labels in Formulas. This option allows us to refer to a row or column of
data from our table by the corresponding row or column title as if we had created a
range name. For example, in the data table of Figure 3.15, instead of using Create or
Define to make range names, we can simply use the row title “Length” to refer to all of
the data in the first row. Likewise, we can use the column title “Product 1” to refer to all
the data in the first column. Spaces are allowed in these names because they have been
taken directly from the spreadsheet; that is, underscores do not have to replace spaces
as when naming ranges.
Formulas or references that use these natural range names also automatically shift
respectively when copied or moved. That is, if we enter the formula
=Length*Width*Depth in cell C6 of the table in Figure 3.15, it would take the values for
each respective column as it was copied to cells D6 and E6. Note that all three cells
would have the same formula, but different values. This can be an easy and convenient
way to name cells; however, in some situations, it may be limiting.
3.4 Summary
¾ There are four basic types of referencing. In relative referencing (B2), row and
column values change. In absolute referencing ($B$2), neither the row nor column
value changes. For row absolute (B$2), the row value does not change, but the
column value does. For column absolute ($B2), the column value does not change,
but the row value does.
¾ R1C1 notation refers to a cell’s position relative to the origin cell where the formula is
entered. This type of referencing is important in VBA coding.
¾ In referencing worksheets and workbooks, worksheets in the same workbook are
referenced as Sheet1!A1. Workbooks are referenced as [Data.xls]Sheet1!A1 and
workbooks with spaces in the title are referenced as ‘[Collected Data]Sheet1’!A1.
Workbooks not currently open in Excel must be referenced as ‘C:\My
Documents\Project\[Collected Data]Sheet1’!A1.
¾ Circular referencing can occur when there is a referencing loop between cells. To
correct this error, use the Tools > Options > Calculations > Iterations option.
¾ Use defining names to name cells, ranges, constants, and formulas. Use creating
names when row and column labels are already given in a table. Applying names is
necessary to update formulas with new cell and range names.
¾ Names can also be created for formulas and constants to make referencing in longer
formulas clearer.
¾ Two simpler ways to create range names are using the name window or natural
range names.
3.5 Exercises
3.5.1 Review Questions
Enter the data above into a spreadsheet. Use column headings to describe the
contents of each column. Then, using the Create option, give the range of cells in
each column the same name as the column heading. Finally, define a name for the
whole table.
Create a table of the engineer’s results in an Excel spreadsheet. Define names for
each row and column of your table. Also define a name for the whole table.
3. The spreadsheet below is used to calculate the area of a rectangle given the
parameters length and width.
4. The spreadsheet below is used to calculate the circumference and area of a circle
using the circle’s radius and the constant pi.
5. One must be very careful when using named columns or rows in a spreadsheet.
Create a ten-cell column that begins in cell A1 and contains the integers 1 through
10, so that each cell contains one integer. Define this range of integers with the
name “column.” Give the responses when the formula “=column” is placed in the
following cells:
a) C1
b) D7
c) E11
6. A tile manufacturer uses spreadsheets to monitor the quarterly sales of each of its
regional distributors. The tables below display the sales for the years 2000, 2001,
and 2002. Create each of the tables on a separate worksheet. Name the worksheets
“2000,” “2001,” and “2002,” respectively.
Table 1
2000
Sales
Region Quarter 1 Quarter 2 Quarter 3 Quarter 4
Northeast $1,583,952 $1,739,820 $1,693,203 $1,520,394
Midwest $1,948,383 $1,837,853 $1,747,283 $1,893,852
South $1,305,239 $1,294,029 $1,259,305 $1,385,921
Southeast $1,849,281 $1,758,234 $1,820,349 $1,859,381
Table 2
2001
Sales
Region Quarter 1 Quarter 2 Quarter 3 Quarter 4
Northeast $1,492,847 $1,395,928 $1,482,739 $1,523,948
Midwest $1,704,929 $1,691,029 $1,693,829 $1,710,394
South $1,282,727 $1,182,939 $1,203,848 $1,284,980
Southeast $1,748,599 $1,693,829 $1,739,849 $1,752,839
Table 3
2002
Sales
Region Quarter 1 Quarter 2 Quarter 3 Quarter 4
Northeast $1,603,954 $1,582,030 $1,593,828 $1,620,293
Midwest $1,820,382 $1,782,984 $1,802,030 $1,815,982
South $1,502,938 $1,482,039 $1,459,390 $1,529,402
Southeast $1,938,192 $1,895,829 $1,882,939 $2,013,020
A final table is used to compile all of the sales over the three-year period. On a fourth
worksheet titled “Summary,” create this table as shown below. Complete the table
with the regional sales for each quarter of each year by referencing the first three
tables.
7. The chart below is a From/To chart used by a manufacturing plant to indicate the
number of products flowing from each station A-E to another station. Enter the chart
into a spreadsheet. Create names for each row and column of data. Also define
names for the formulas located in the “From Sum” column and the “To Sum” row.
(Note: These generic formulas will be replaced with actual formulas that perform
calculations in the next chapter).
From/To Chart
From/To To A To B To C To D To E From Sum =
From A 0 30 20 19 6 =From Sum
From B 9 0 40 30 14 =From Sum
From C 7 15 0 53 19 =From Sum
From D 3 12 15 0 60 =From Sum
From E 0 4 10 7 0 =From Sum
To Sum = =To Sum =To Sum =To Sum =To Sum =To Sum
8. Checking accounts at a local bank earn 4% interest annually. The table below is
used to compute the amount of interest earned for each account:
Create this table in an Excel spreadsheet and use it to perform the following
functions:
a) Drag the formula used in cell C2 to cells D2 and E2. Modify the formulas in
cells D2 and E2 so that each refers to the appropriate interest rate.
b) Using absolute referencing to refer to the appropriate interest rate, drag
your formulas in cells D2 and E2 through the remaining cells in each
column to compute the amount of interest earned on the other accounts at
5% and 6%.
10. The table below is used to compute the velocity of a vehicle given the time and
distance traveled. Enter the table below into a spreadsheet, and perform the
following functions:
11. Refer to the table in Chapter 2, Problem 9. Paste the table onto a spreadsheet so
that the upper left cell, “transaction number,” is located in cell A1. Then, for each
transaction, give the location of the cell containing the amount of the transaction by
using R1C1 notation that references cell A1.
12. The table below is used to compute the force per square meter of a falling object
hitting the ground. Enter the table into an Excel spreadsheet as shown below.
Surface Area (m^2) Mass (kg) Force (N) Force Per Area (N/m^2)
0.5 3.0
1.0 2.3
0.6 1.6
0.9 3.8
0.4 2.1
1.1 4.3
1.0 3.5
13. The spreadsheet below is used to convert a table of data in degrees Fahrenheit into
degrees Celsius. Below is the formula to perform this conversion:
Constants: 5 9 32
14. The table below records the June retail sales of ten southeastern states.
a) Name each cell in the range B2:B12 by its corresponding state abbreviation
from column A. (For example, cell B2 should be named “FL.”)
b) Name the entire Monthly Sales column “Monthly Sales.”
c) In cell A14, calculate the sum of the sales values using range names. (This
can be done using names from part a or part b above.)
16. In the table below, two production plants have recorded their shipments made to
three different cities. Using natural range names, find the total number of
shipments made by each plant and the total number received by each city.
18. A furniture store has $7500 cash at the beginning of January. Cash inflow and
outflow from January to May are recorded in the table below. Using natural range
names and referencing, compute the cash amount at the beginning and end of
each month.
Track the following plane route: Houston to Los Angeles to Seattle to Chicago,
back to Houston. For all flight legs, each plane uses 0.1 gallons per mile. The
average fuel per leg = 0.5 * (initial fuel + final fuel). In Houston, the plane begins
with 1000 gallons of fuel. Using the distance of each flight leg in the table below,
compute the amount of fuel left when the plane returns to Houston.
19. In August, at the beginning of the semester, Mary has $500 in her savings
account. At the end of each month, she earns 2% interest. Each month’s interest
is based on the average of the month’s beginning and ending balance. Create a
flow table of balances for the beginning and end of each month in the academic
year (August 1 to May 1 = 9 months). Assuming that she doesn't make any
withdrawals, how much money does Mary have in her savings account in May?
20. John borrowed $20,000 from the Credit Union Bank to buy a car. He will be
paying this amount back in 5 equal end-of-year payments. The bank uses an
APR of 2% to calculate the yearly payments.
a. Calculate the John’s yearly payments using the formula:
$20,000
Payment = 5
∑ (1 + 0.02) − j
j =1
b. Use natural range names and referencing to calculate the amount due in
the beginning and end of each year and the amount of interest paid each
year. Note that: Int. Paid = Beginning Balance * 0.02; End Balance =
Beginning Balance – Principal Payment; Principal Payment = Yearly
Payment – Int. Paid.
21. A distribution center is considering four possible locations for their new facility.
The following table (in DC.xls) presents the x and y coordinates of these
locations.
The management is planning to choose the location that would minimize the
total distance traveled to serve their customers. File Customers.xls,
worksheet Locations contains the location of the customers.
Calculate the total distance traveled from each candidate location to the
customers. Consider Euclidean distance between locations. Calculate the
distances in DC.xls referencing customer locations in Customers.xls.
Using the Create Name option, name the range of cells that presents the
distances from a particular location to all customers. Use the formulas
‘=sum(A)’ to ‘=sum(D)’ to calculate the total distance from a candidate
location to all customers.
b. Use referencing to calculate the revenues for the rest of the months.
c. Create names for the columns: ‘Price’, ‘Jan’,…, ‘Dec’.
d. Create names for the rows: ‘Chair’, ‘Desk’ and ‘Futon’.
e. Define a name for the table.
f. Calculate the monthly revenues, the annual revenues per items and the
annual revenues of the store. In calculating the revenues use the
formulas: ‘=sum(Jan)’,…,’=sum(Futon)’, etc.
23. Consider the information given in Hands-on Exercise 3.23. The following table
presents the purchasing cost per item at “Contemporary Furniture” store.
b. Copy the monthly revenues from the spreadsheet with the solution to
Hands-on Exercise 3.23 and paste in E2:E13.
c. Calculate the monthly expenses referencing the unit costs and monthly
sales presented in the Excel file (or spreadsheet) with the solution to
exercise 3.23.
d. Create names for the columns: ‘Revenues’, ‘Expenses’, and ‘Profits’.
e. Define a name for the table.
f. Calculate the monthly profits using the formula Profits = Revenues –
Expenses.
g. Calculate the annual profits for this business.
24. A health care center is considering locating their new facility either in Miami,
Orlando or Jacksonville in Florida. In making their decision, the following factors
are considered: total patient miles per month, facility utilization, average time per
emergency trip, expressway accessibility, land and construction costs and
employee preferences. The management evaluated each possible location using
these criteria. The following table presents weight given to each factor and
scores for each location.
a. Create names for the columns: ‘Weight, ‘Jacksonville’, ‘Orlando’ and ‘Miami’.
b. Define a name for the table.
c. Calculate the weighted score for each location using the formula ‘Weighted
Score = Weight*Score’.
d. Define names for the columns that present the weighted scores for each
location.
e. Identify the best location for the health care center. Note that the best is the
location that maximizes the total weighted score. This method of selecting
among different alternatives is known as the Preference Matrix method.
Functions and
Formulas
Chapter Overview
4.1 Introduction
4.2 Formulas and Function Categories
4.3 Logical and Information Functions
4.4 Text and Lookup & Reference Functions
4.5 Date & Time Functions
4.6 Mathematical and Trigonometry Functions
4.7 Statistical and Financial Functions
4.8 Conditional Formatting Formulas
4.9 Auditing
4.10 Summary
4.11 Exercises
Chapter 4: Functions and Formulas 2
4.1 Introduction
This chapter presents an overview of the many functions available in Excel. One of the
strengths that Excel has to offer as a spreadsheet software is the many built-in functions
that can be used for simple or advanced calculations. As this book focuses on the
development of spreadsheet-based decision support systems, we recommend that these
spreadsheet functions be incorporated into the DSS applications. In this chapter, we
seek to illustrate the many categories of functions available in Excel.
Figure 4.1 A basic operator performs simple calculations by entering numbers and
operator signs into a cell.
Functions are the largest category of formulas. Excel has several pre-programmed
functions that we can reference from the function menu. There are actually ten
categories of functions: Financial, Date and Time, Mathematics and Trigonometry,
Statistical, Lookup & Reference, Database, Text, Logical, Information, and Engineering.
If we choose Insert > Functions from the menu (or click on the fx icon) we see the entire
list of function categories as displayed in Figure 4.2. If we do not see the fx icon, we can
add it to the Standard toolbar by clicking on the drop-down arrow at the end of the
toolbar to Add or Remove Buttons. See Chapter 2 for further details on adding or
removing items to or from the toolbar. Clicking any of these categories displays a list of
the functions in that category. Selecting one of these displayed functions provides a brief
description of that function at the bottom of the window.
For example, in Figure 4.2, we have selected Math and Trig from the category list and
SUM from the function name list. At the bottom of the window, we can see the basic
format and description for the SUM function. Once we select a function, another display,
shown in Figure 4.3, appears; it gives further instructions about how to perform the
function we have selected. We can select the icon in either of these windows. This icon
provides Help with this Function, and gives a detailed explanation of the function as well
as an example of how to use it. The Most Recently Used option, found at the top of the
Function list, shows approximately the last ten functions used. The All option in the
Function category list displays all of the functions from all categories.
Figure 4.2 There are 10 function categories. Each category has several functions
from which to choose.
Figure 4.3 Instruction boxes appear after a function is selected. These boxes allow a
user to define the parameter(s) of the function.
We will describe the most basic functions, SUM, AVERAGE, MIN, and MAX, to illustrate
the common spreadsheet operations that we can perform easily with functions.
SUM, a Math and Trig function, adds any range of data, including selected columns,
rows, or tables of numbers. The SUM function can also be applied to non-consecutive,
or non-adjacent, cells. SUM can take either of the following formats:
=SUM(number1, number2, …)
=SUM(range_name)
The range name is the column or row of numbers of which we want to take the sum. We
can also find the sum of a range of values taken from multiple rows and columns. The
result of the sum calculation appears in the cell in which the formula is typed. For
example, in Figure 4.4(a), each cell in the Sum row contains the SUM function to
calculate the sum of the column above it. That is, cell D21 has the function
“=SUM(D5:D19),” which results in the value of the sum of all of the numbers in the
column above.
Recall that the naming techniques discussed in Chapter 3 allow us to use created range
names in formulas and functions. For example, in Figure 4.4(a), we could have named
the first column “Diameter,” and then entered the function in the Sum row as
“=SUM(Diameter).”
(a)
(b)
(c)
Figure 4.4 The SUM function (a) takes all of the numbers in a column (or row) and
calculates their sum. The AVERAGE function (b) takes all of the numbers
in a column (or row), calculates their sum, and divides by the number of
cells in that column (or row). The values calculated from the MIN and
MAX functions (c) shown at the bottom of the screen refer to each column
of data above the cells containing the formula.
The AVERAGE function takes a range of numbers (a column or row), sums them, and
then divides by the number of cells in that range. Note however, that if there are empty
cells in the selected range, that is cells without numbers, these cells are not included in
the count of numbers used in the dividend of this function. A Statistical function,
AVERAGE takes one of the following formats:
=AVERAGE(number1, number2, …)
=AVERAGE(range_name)
In Figure 4.4(b), cell E22 displays the average of the column E5:E19, which is the range
name shown in the function at the top of the window.
Two other statistical functions, MIN and MAX, take all of the cell values in a range
(column or row) and compare them to determine which number is the smallest or largest.
The formats for the MIN and MAX functions are:
=MIN(number1, number2, …)
=MIN(range_name)
=MAX(number1, number2, …)
=MAX(range_name)
In Figure 4.4(c), cell C23 is the minimum value of column C5:C19 and cell C24 is the
maximum of this same column. Note that the range C5:C19 appears in the MIN function
formula at the top of the window.
Formulas are an important part of spreadsheet applications. They are not only
necessary for basic calculations, as explained above, but they are also an integral part
of VBA coding. We will now explain some of the most commonly used functions within
each of the other categories.
Formula Types:
Simple values
Basic operators
Naming and referencing
Summary
Functions
We can organize the six Logical functions, listed under Function name in Figure 4.5, into
three pairs of related functions: TRUE/ FALSE, IF/ NOT, and AND/ OR.
Figure 4.5 The six Logical functions: TRUE, FALSE, AND, OR, NOT, and IF.
The TRUE and FALSE functions simply display the words TRUE and FALSE. There are
no parameters to enter for this function; we just type the function name:
=TRUE
=FALSE
We may want to use these functions in a manual evaluation of our data. For example,
we want to determine which numbers are greater than 100 in the list featured in Figure
4.6. We have placed the function =TRUE next to each cell that contains a number larger
than 100. When reviewing our list of numbers, we can immediately see which numbers
are greater than 100. Alternately, we could place the FALSE function next to the cells
with numbers less than 100.
Figure 4.6 The TRUE and FALSE functions display the words True and False,
respectively.
The NOT function contrasts a cell with any other cell, and can be used as long as the
cell that we are reviewing has a value of either TRUE or FALSE. Use the following
formula to specify the opposing cell:
=NOT(cell_name)
So, if we place the NOT function next to every cell where we previously placed a TRUE
function, we should see the result FALSE in each cell. Figure 4.7 illustrates that F8, with
a value of FALSE, opposes E8, which is TRUE.
Figure 4.7 The NOT function displays the opposite value of a cell. Since E8 is TRUE,
NOT(E8) is FALSE.
The IF function allows us to evaluate data by using a specified condition that determines
whether data is true or false. An IF function requires us to specify three parameters:
condition to be met, return value if true, and return value if false. For example:
Returning to the data in Figure 4.6, we can easily use the IF function to determine which
values are greater than 100. Therefore, in Figure 4.8(a), we have added the IF function
to the last column on the right. The formula for the IF function, =IF(C5>100, TRUE,
FALSE), directs the computer to return the value TRUE if C5 is greater than 100. In all
other circumstances, it will return the value FALSE. We can copy this function to the
entire column in order to evaluate all of the numbers in the table. Note that we can put
any text or numerical value in the return parameters. For example, we could return the
words “Big Number” if our number is greater than 100 and “Small Number” if it is less
than 100. Likewise, we could display a “1” for all numbers greater than 100 and a “0” for
all numbers less than 100. We can even display nothing by placing empty quotation
marks “” in the return parameters, as displayed in Figure 4.8(b).
(a)
(b)
Figure 4.8 The IF function takes three parameters: condition to be met, return value
if true, and return value if false. (a) In this example, TRUE is displayed if the number is
greater than 100, and FALSE is displayed if it is less than 100. (b) In the second example,
nothing is displayed if the condition is not met.
The AND and OR functions return the values TRUE and FALSE depending on certain
conditions. For the AND function, all of the conditions listed must be true in order for
TRUE to be displayed. If any of the conditions are violated, FALSE is returned. The
format for the AND function is:
=AND(condition1, condition2, …)
We can have up to 30 conditions. For the OR function, only one of the conditions needs
to be true for TRUE to be the result, while all of the conditions would have to be violated
for FALSE to be displayed. The OR function uses a similar format:
=OR(condition1, condition2, …)
In Figure 4.9(a), we have added the AND function to evaluate whether or not all of the
numbers in our table are greater than 100. Our AND function requires every cell to be
greater than 100:
This function results in FALSE because some of the numbers in the data set do not
match the conditions of this function. In Figure 4.9(b), by contrast, we have paired the
same conditions with an OR function. Because only one of the conditions needs to be
met, this function results in TRUE since only one of the conditions needs to be met.
(a)
(b)
Figure 4.9 The AND and OR functions evaluate several conditions. The AND
function requires all conditions to be met in order to return TRUE, while OR requires
only one condition to be met to be TRUE. (a) The AND function returns FALSE because
not every number in the table is greater than 100. (b) The OR function returns TRUE
because at least one number in the table is greater than 100.
The IF, AND, and OR functions are very useful in advanced applications. For some
problems, there may be several complex conditions that need to be checked. These
functions can be used to evaluate data to ensure that those conditions are met and to
signal which data violates those conditions.
Logical Functions:
TRUE Returns the value True.
FALSE Returns the value False.
NOT Returns the opposite value.
IF Evaluates a condition and returns one value if it is true and another
value if it is false.
Summary
AND Evaluates several conditions and returns True if all conditions are
met and False if any condition is not met.
OR Evaluates several conditions and returns True if any condition is
met, and False if all conditions are not met.
There are several different Information functions, all of which provide some basic
descriptive information about the data. Figure 4.10 lists some of these functions. One
group of these functions, IsFunctions, answer whether a cell value has or does not
have a specified characteristic. If we do not see some of these IsFunctions in our list of
Information functions, we may need to add the Analysis Toolpack Add-In. To do so, we
simply go to Tools > Add-Ins and select the Analysis Toolpack option.
The ISEVEN and ISODD functions, for example, determine whether or not a number in a
cell is even or odd. We only need to specify the number itself or the name of the cell that
contains the number that we are evaluating:
=ISEVEN(cell_name) or =ISEVEN(number)
=ISODD(cell_name) or =ISODD(number)
If the number is even, the ISEVEN function returns TRUE and the ISODD function
returns FALSE. The opposite would occur if the number were odd.
The data table in Figure 4.11 contains both even and odd numbers. We have used the
ISEVEN function in Column 2 to evaluate the data in Column C. So, in Figure 4.11(a),
the formula for E5 determines whether or not C5 is even. In Figure 4.11(b), we have
added the ISODD function in Column F. The TRUE and FALSE values in this function
column are exactly the opposite of the ISEVEN values.
(a)
(b)
Figure 4.11 The ISEVEN and ISODD functions display TRUE if the data value is even
or odd, respectively. (a) The ISEVEN function value is TRUE for even numbers and
FALSE for odd numbers. (b) The ISODD function value is TRUE and FALSE for the
opposite situations.
Additional examples of IsFunctions are the ISTEXT and ISNUMBER functions. The
ISTEXT function returns TRUE if a cell value is text and FALSE if it is not. To enter a
numerical value as text, we must either include it in double quotation marks or begin with
a single quotation mark. Either a cell reference or a value can function as the parameter
of this function:
=ISTEXT(cell_name)
=ISTEXT(value)
The ISNUMBER function returns TRUE if a cell value is a number and FALSE if it is not.
The format for ISNUMBER is similarly:
=ISNUMBER(cell_name)
=ISNUMBER(value)
Column C of Figure 4.12 lists numbers 1 through 7. While some of the numbers are
written as text, others are presented in numerical form. Column E evaluates the cells
through the ISTEXT function, shown in Figure 4.12(a), while Column F, featured in
Figure 4.12(b), displays the ISNUMBER function.
(a)
(b)
Figure 4.12 ISTEXT and ISNUMBER return TRUE if the data value is text or a
number, respectively. (a) ISTEXT shows FALSE when the data is a numerical value. (b)
ISNUMBER shows TRUE for the same data since it is a number.
The other IsFunctions are similar. If we click on the function names in the function
display window, as shown in Figure 4.10, we can view a description of each function.
The TYPE function, another Information function, evaluates the data type of a value.
Data type, a descriptive category of the different types of values possible in Excel, can
take the form of number, text, or logical values. For example, let’s consider the data
types of number, text, and logical values. Excel uses numbers to designate each data
type: the number 1 signifies a numerical data type; the number 2 signifies a text data
type; and the number 4 signifies a logical value data type. The TYPE function evaluates
our data and returns one of these numerical category descriptions. This function’s
parameter is either a cell reference or a value:
=TYPE(cell_name)
=TYPE(value)
In Figure 4.13, we have examples of these three data types and the numerical results
that the TYPE function returned. Data types are important when using VBA.
Figure 4.13 The TYPE function returns a 1 for numerical data types, a 2 for text data
types, and a 4 for logical value data types.
Information Functions:
IsFunctions Checks whether or not a cell value has a specified characteristic.
ISEVEN Returns True if the number is even, False if the number is odd.
ISODD Returns True if the number is odd, False if the number is even.
ISTEXT Returns True if the value is text, False if not.
ISNUMBER Returns True if the value is a number, False if not.
Summary
Data Types Categories of different possible values.
TYPE Returns the numerical signifier of a data type category.
Excel features many different Text functions, but, in the interest of brevity, we will only
discuss a few of those most commonly used. We will discuss some of the other functions
later when explaining their applications to VBA programming.
The first functions, UPPER and LOWER, convert a cell or a range of cells with text
values into all uppercase or all lowercase text. The only parameter to enter in the
function is the cell or range name where the text is located.
=UPPER(range_name)
=LOWER(range_name)
We can use the UPPER function to convert the first sentence in Figure 4.15 to all
uppercase and the LOWER function to convert the second sentence to all lowercase.
Figure 4.15 The UPPER and LOWER functions change the case of text.
=CONCATENATE(cell1, cell2, …)
Figure 4.16 provides an example of how this function can be used to join several
phrases into a single sentence. The parameters for this function are the locations of the
fragments that we are joining. In our example, we join the words in C3, D3, E3, and F3
to get a complete sentence; note that spaces are included in each group of words.
Figure 4.16 The CONCATENATE function joins text from multiple cells into one
phrase or sentence.
The ampersand, “&,” can also be used to concatenate text when it is placed between
various values or functions in a cell. Here are some examples:
We will see the ampersand used with the IF function in a later section. In VBA,
programmers frequently use the ampersand to combine text and variable values when
creating output strings.
The last Text function we will describe, the SUBSTITUTE function, exchanges old text
for new text in a cell. We can specify the cell reference, the old text, and the new text in
the function parameters. We can also specify how many instances of the old text we
would like to replace; if this is left blank, Excel replaces all instances of the old text with
the new text. We use the following format for this function:
In Figure 4.17a, we illustrate the example of substituting every “a” with an “e.” Note,
however, that Excel is case-sensitive and distinguishes between lower- and upper- case
letters. In order to also change every “A” to an “E,” we must repeat the SUBSTITUTE
function, this time using the result of the first function as our text. Therefore, the
substitute function refers to C5 instead of C4.
(a)
(b)
Figure 4.17 The SUBSTITUTE function changes elements in a specified block of text.
(a) All of the lower case a’s are changed to e’s. (b) All of the uppercase A’s are changed to
E’s.
Text Functions:
UPPER Changes all letters to uppercase.
LOWER Changes all letters to lowercase.
Summary
CONCATENATE Joins separate text fragments into one phrase or sentence.
SUBSTITUTE Changes specified letters or words into new ones in
somegiven text.
Several Lookup & Reference functions are useful in collecting information about data.
These functions will also become more important once we begin discussing VBA. For
now, we would like to explain the VLOOKUP, HLOOKUP, and MATCH functions.
The VLOOKUP and HLOOKUP functions allow users to search for data in a
spreadsheet. The “V” and “H” denote vertical and horizontal lookup techniques.
VLOOKUP searches for a value in the left-most column of a table, marks the row that
contains that value, and then returns a value from that row for a specified column. The
parameters for the VLOOKUP function are the following:
• A value in the first column of the table. This value marks the row that we willsearch.
• The range of cells for the target table. This range must include the first column.
• The index number, or the number of the column that we are searching.
The final parameter, the range_lookup value, regulates the exactitude of the search for
the first parameter value. Therefore, if we set the range_lookup as True, the VLOOKUP
finds the closest match. Setting this value to False results in an exact match. The default
value, True, can be used by simply leaving the parameter blank. We should designate
the range_lookup value as True if the data for the first column is arranged in ascending
or descending order.
Note that the titles or indices in the first column must be unique. Additionally, the index
number we use for the third parameter begins at one. VLOOKUP also requires that the
table be sorted in ascending order by default.
Let’s consider an example in which we have recorded times and distances from 3 trials
of 12 products; assume that we are testing how far a product rolls and how long it takes
to stop rolling. The range of cells in Figure 4.18 extends from B4 to H16 (demarcated as
B4:H16). We include the “Product” column in the cell range because the lookup_value
comes from this column. To find the “Time” that “Product” 5 took in trial 2, we specify our
desired value by giving the “Product” value of 5 as our first parameter. We have
specified B4:H16 already as our second parameter and we give the value 3 for the
column index of our desired data as the third parameter. We can leave the range_lookup
value blank or type True, since the product numbers are arranged in ascending order.
As presented in Figure 4.18, our function takes the following form:
The VLOOKUP function therefore searches the table B4:H16 along the row whose first
column value is 5 in order to find the data value in column 3 of that row. The result, 8.18
seconds, appears in E19. We could perform a similar search for the “Distance” that
“Product” 5 rolled in trial 2. Our parameters here would be the same, except that the
column number would be 6 instead of 3. The result for that distance, 24.53 meters,
appears in E20.
Figure 4.18 The VLOOKUP function takes a row, specified by a value, from a table of
data and returns the value from a given column.
The HLOOKUP function searches in a similar fashion. However, in this function, the first
and third parameters, lookup_value and column_index_number, change. HLOOKUP
searches for a value in the top row of a table, marks the column that contains that value,
and then returns a value from that column for a specified row. Instead of beginning with
a value from the first column of the table (marking the row that we will be searching), the
HLOOKUP function takes a value from the first row of the table (marking the column that
we will be searching) as its first parameter. Also, instead of using the index number for
the column that we want to search, the HLOOKUP function uses the index number for
the row that we are searching as the third parameter. Note that we must also include the
first row, which includes the first parameter of the function, when specializing the cell
range of the table. The complete format for the HLOOKUP function is:
Using the same example as before, we will now use the HLOOKUP function to find the
same values. Note that we search by T2 and D2 instead of by “Product” 5. That is, to
find the time “Product” 5 took in trial 2, we specify the text “T 2” as our first parameter.
We select C4:H16 as our second parameter, this time including the first row of the table
instead of the first column, and we give the value 6 for the row index to denote “Product”
5 as the third parameter. We can set the range_lookup value to False, since the values
in row 4 are not in ascending order. Our function (as presented in Figure 4.19) is
therefore:
The HLOOKUP function searches the table C4:H16 along the column that begins with
D4 to find the data value included in row 6. The result, 8.18 seconds, appears in J19.
We could perform a similar search for the “Distance” that “Product” 5 took in trial 2. Our
parameters here would be the same, except that the row cell reference in our first
parameter would now be “D 2”. The result for the distance is 24.53 meters. We verify
that both of these functions return the same values using different search methods.
When working with either of the LOOKUP functions, we may have to manipulate our
table data if we are searching by a value that is not in the first row or the first column of
the table and trying to find a value that is in the first row or column of the table. Suppose
that we want to find the product that had a time of 8.18 in Trial 2. Because the first
parameter of the VLOOKUP must appear in the first column of the specified data range,
we need to copy the product column to the end of the table. In this example, we copy
B4:B16 to I4:I16. Now we can use the LOOKUP functions as previously described, but
we have to specify the range such that the first row or column is the row or column with
our search data. To find the product number with a time T2 of 8.18, we move the product
column to Column I and type:
Figure 4.19 The HLOOKUP function takes a column that is specified by a value and
returns the value from a given row.
Another useful Lookup & Reference function, the MATCH function, searches a table of
data and returns the location of a desired value. The parameters for this function are the
following: the value for which we are searching, the table in which we are searching,
and a code that guides the result. This code, called match_type, can be 0, 1, or –1. The
code 0 returns the location of the first found value that is equal to the value for which we
are searching. The code 1 returns the location of the largest value that is less than or
equal to our specified value (given that the data is in ascending order). The code –1
returns the location of the smallest value that is greater than or equal to our value (given
that the data is in descending order). We can select this parameter based on the sorting
order of our table to be 1 or -1 or we can search for the first matching value it finds using
0, which is the default.
Let’s consider another example. Figure 4.20 features a list of numbers in cells C3
through C12 that are arranged in ascending order. Using the MATCH function, we
search for the number 50 using the three different match_types. Our general format is
now the following:
If we use match_type 0, the result is “N/A,” which means that the number 50 is not in our
data. If we use match_type 1, the result is 4. This means that the largest number that is
less than or equal to 50 is in the fourth row of our table: 42. If we use match_type –1, we
must switch our table to descending order, as displayed in Figure 4.20(b), using the sort
feature described in Chapter 2. The MATCH function now returns the value 6, which
signifies that the smallest number that is greater than or equal to 50 is in the sixth row of
our table: 63. Note that when we switch our table to descending order, the value of the
MATCH function using match_type 1 becomes “N/A,” as it requires the table to be in
ascending order.
(a)
(b)
Figure 4.20 The MATCH function searches for a specified value in a given table and
returns its location, guided by a determined return code. (a) The return code 1 is used
when the data is in ascending order and returns the location of the largest number less
than or equal to the number for which we are searching. (b) Return code –1 is used when
the data is in descending order and returns the location of the smallest number greater
than or equal to the desired value.
In another example, we could search the rows of data in a table so that a column
number is returned as the location of the desired value. In Figure 4.21, we have a row of
letters. If we search for the letter “d” using return code 0, we find that it is in column 4 of
our data.
=MATCH(“d”, C3:G3, 0)
Figure 4.21 Return code 0 is used to find the letter d, which is in column 4 of the
table.
The INDEX function, like the MATCH function, allows us to find an entry in a specified
row and column of a range of cells.
The first column and row of a range are numbered 1. Let’s consider an example in which
this function may be useful. The table below (Figure 4.22) stores the distances between
ten US cities. Since each city is numbered, we can simply use the INDEX function with
these city numbers to find the distance between any two cities in the table.
For example, to find the distance between Boston and Denver, we would use the city
numbers 2 and 4 in the INDEX function as follows:
=INDEX(C5:L14, 2, 4)
We can repeat this process to find the distances between Seattle and Miami, and Atlanta
and Houston (see Figure 4.23). The range is the same for all distances; only the city
numbers change. We could also name the range and use the range name with the
INDEX function. For example, if we name the distance range “AllDistances,” then the
INDEX function to compute the distance between Boston and Denver becomes:
=INDEX(AllDistances, 2, 4)
Figure 4.23 Computing the distances between specific cities using the INDEX
function.
We can also use the INDEX function to refer to an entire row or an entire column. To do
this, we simply specify the row or column number that we want to select, and then make
the column or row number 0 in the function statement. So, the first element of the table
in the above example (the distance from Atlanta to Atlanta) is:
=INDEX(AllDistances, 1, 1)
The first row of the table (all the distances from Atlanta) is:
=INDEX(AllDistances, 1, 0)
And the first column of the table (all the distances to Atlanta) is:
=INDEX(AllDistances, 0, 1)
We need to enter this function into a single cell, so only the first value of the entire row or
column captured by the INDEX function appears. To see all other values, we copy this
function to the rest of the row or column range (see Figure 4.24).
Figure 4.24 The INDEX function captures the entire row of distances from Atlanta.
Capturing an entire row or column of data can also be useful when we are computing
calculations such as SUM, AVERAGE, MIN, or MAX. So, we can find the city farthest
from Atlanta by typing:
=MAX(INDEX(AllDistances, 1, 0))
This function evaluates the entire first row of data from the table and returns the largest
number. For this example, the result is 2719, which is the distance to Los Angeles. (How
could you find the name, and not the distance, of the city farthest from Atlanta? Hint: Use
a LOOKUP function with the previous example.)
Another useful Lookup and Reference function is the OFFSET function. The OFFSET
function references a cell that is a given number of rows and columns from a specified
cell, or range of cells.
Let’s use the table of numbers in Figure 4.25 to demonstrate the use of the OFFSET
function. First, we name the cell C2 the “RefCell” since we will reference this cell most
often.
Figure 4.25 In this table of numbers, the first cell is named the “RefCell.”
To find the last value in the first row of this table, we can reference the “RefCell” and
look on the same row, four columns to the right. We therefore enter the OFFSET
function as follows:
=OFFSET(RefCell, 0,4)
We have ignored the height and width parameters since we are looking for a single cell
value. To find the last value in the first column, we reference the “RefCell” and look two
rows down in the same column.
=OFFSET(RefCell, 2, 0)
See Figure 4.26 for the results of these examples. If we start in the last value of the first
column and want to find the “RefCell” value, we type:
=OFFSET(C4, -2, 0)
Figure 4.26 Using the OFFSET function to find the last values of the first row and the
first column.
The OFFSET function can also be useful in combination with other functions. For
example, to find the sum of the values in the last column of the table, we use both the
SUM and OFFSET functions; this time, we also use the height and width parameters of
the OFFSET function. We start at the “RefCell” and move to the last value in the first row
as we did previously; now we give a height value of 3 and a width value of 1 to capture
the entire column starting at the offset cell.
=SUM(OFFSET(RefCell, 0, 4, 3, 1))
Similarly, we can calculate the sum of values in the last row by typing:
=SUM(OFFSET(RefCell, 2, 0, 1, 5))
See Figure 4.27 for the results of these examples. The referencing technique used in the
OFFSET function is common in VBA.
The first two functions, TODAY and NOW, simply display the current date and time,
respectively. There are no parameters for these functions; we simply type the following:
=TODAY()
=NOW()
Excel produces the results of these functions by using the initial starting point discussed
above to determine the serial number of the current date. Note that these functions
always update their results as the current day and time changes. In Figure 4.29(a) and
4.29(b), we show how these two functions work.
(a)
(b)
Figure 4.29 The TODAY (a) and NOW (b) functions display the current date and time.
Three other Date & Time Functions are NETWORKDAYS, DAYS360, and YEARFRAC.
These functions calculate the workdays, total days, and fraction of a year between two
dates. All three functions have a start_date and end_date as their first two parameters.
The third parameter for the NETWORKDAYS function is the number of holidays that
occur between the two dates, where a holiday is any day when employees do not work.
The DAYS360 function has a specified method as its third parameter, which determines
either a US standard, where the method is equal to False or left blank, or a European
standard, where the method is equal to True, for counting the days. These two methods
differ in that the US standard begins counting on the day after the start_date, while the
European method includes the start_date. The YEARFRAC function also has a third
parameter, basis, which specifies a counting method. For the DAYS360 and
YEARFRAC functions, we recommend leaving the third parameter blank unless there is
a specific need to apply an alternate counting method. The general formats of these
functions are the following:
Let’s consider an example of a company that receives shipments of office supplies each
month. Given the dates in Figure 4.30, the manager of the company wants to construct a
report that displays the number of workdays and total days that pass between the date
the supply is shipped and when it is received. Suppose he wants to determine which
supplier takes the least amount of time to transport items to his office.
To find the number of workdays that pass between the date shipped and the date
received, we enter the following function:
=NETWORKDAYS(C4, D4, 0)
If there aren't any holidays between the start and the end date, we are free to leave that
parameter blank. As seen in Figure 4.31, we can copy this function for every supplier.
However, in some cases, we must edit the third parameter. For example, for “Pencils,”
there are two national holidays between the “Date Shipped” and “Date Received”;
therefore, the function for that supplier is:
=NETWORKDAYS(C8, D8, 2)
The NETWORKDAYS function returns the total number of working days between each
set of dates for each supplier.
Figure 4.31 The Shipping Report calculates both the number of workdays (using
NETWORKDAYS) and total days (using DAYS360) between the “Date Shipped” and the
“Date Received” for the various incoming supplies.
If we now use the DAYS360 function, we receive some different results. As we have
mentioned, the US method, which is the default method parameter, begins counting on
the day after the start_date. The NETWORKDAYS function, however, considers the
start_date in its count. To calculate the total number of days, we use the following
formula:
=DAYS360(C4, D4, )
We can view the results of these different counting methods in the shipment for
“Scissors,” which has more workdays than total days counted (see Figure 4.32).
December 2, 2002 is a Monday (a workday) and December 5, 2002 is a Thursday (also
a workday). Therefore, the NETWORKDAYS function counts Monday through Thursday,
which is four days. However, DAYS360 begins counting on Tuesday and only counts
three days.
Figure 4.32 The DAYS360 function counts the total number of days between two
dates starting on the day after the start_date.
The YEARFRAC function can now determine how much of a year has passed between
two dates. In the report from this shipping example, the manager of the company may be
interested in the shipping period he has observed. He considers the period between the
first and last “Date Received”:
=YEARFRAC(D4,D9)
Note that we have again left the third parameter blank in this case. In Figure 4.33, we
see that the result of this function is 0.25, which is a quarter of a year.
Figure 4.33 Using YEARFRAC to compute the fraction of a year for which this report
has been constructed.
Other Date & Time functions allow us to enumerate and manipulate days and times. One
of these, the MONTH function, determines the month of a given date. The months are
numbered from 1 (January) through 12 (December). There is only one parameter for this
function, which is the date. We can actually enter a date into the function or refer to a
cell that has a date as its value:
=MONTH(date)
=MONTH(cell_name)
For example, suppose the date 1/2/03 is in cell B2 of a given table. If we type the
function =MONTH(B2), its result is 1, since January is the first month of the year.
Likewise, the WEEKDAY function determines the day of the week specified by a given
date. There are two parameters in the WEEKDAY function: the date and the numbering
method of our choice. We can enumerate Sunday as day 1 and Saturday as day 7 by
entering a 1 as our second parameter; this is also the default if we leave this parameter
blank. We can also enumerate Monday as day 1 and Sunday as day 7 by entering 2 as
the parameter. Finally, we can enumerate Monday as day 0 and Sunday as day 6 by
entering 3 as the parameter. Again, we can enter a date into the function or refer to a
cell that has a date as its value.
=WEEKDAY(date, method)
=WEEKDAY(cell_name, method)
For example, if the date 1/2/03 is in cell B2, we type the following:
=WEEKDAY(B2, 1)
This function returns a 5, which, because we used the first numbering method, implies
that this date is on a Thursday.
Let’s consider an extended example to demonstrate these two functions. In Figure 4.34,
we have a “Flight Sheet” with recorded flight dates and times during the last few months
of the year 2002. The times are recorded as “Clock-in” and “Clock-out” times to
represent the beginning and end of the flights. Suppose that the supervisor of this flight
agency wants to know which flight dates correspond to which days of the week.
We need to use two functions to accomplish this task: the WEEKDAY function and the IF
function, which we discussed earlier in this chapter. Using the default numbering method
with Sunday as 1 and Saturday as 7, we assign words to each day of the week for each
corresponding numerical result. That is, if the result of the WEEKDAY function is 1, we
want to display the word “Sunday.” We therefore use the IF function to check the
numerical result of the WEEKDAY function and then display the corresponding day
name. Our function turns out to be the following (see Figure 4.35):
Note that we do not enter our numbering method as a parameter of the WEEKDAY
function since option 1 is the default. Also, remember that the parameters of the IF
function follow the conditions result if true, and result if false. Our result if our condition is
false in this case is another IF function. That is, the above function tells Excel to print the
word “Sunday” if the result of WEEKDAY is 1, “Monday” if the result is 2, and so on
through the days of the week.
Figure 4.34 This Flight Sheet records the dates, clock-in, and clock-out times for the
flights in the last few months of the year 2002.
Figure 4.35 Using the WEEKDAY function with the IF function to display the name of
the day of the week corresponding to the date in the table.
Figure 4.36 displays the name of the month for each date using the IF function again,
this time with the MONTH function. We type the following:
Figure 4.36 Using the MONTH function with the IF function to display the name of
the month corresponding to the date in the table.
Note in the function in Figure 4.36 that we have joined two different IF functions using
the “&” sign. The “&” sign, as we have already mentioned, is another way to concatenate
text, values, or formulas in a cell. We joined two IF functions in this example because an
IF function can only have up to seven nested entries.
Remember that the MONTH function only has one parameter, so there is no counting
method signified in the function. The above function reads “If the result of MONTH is 1,
print the word ‘January,’ otherwise check if the result of the MONTH function is 2 and
print the word ‘February,’ otherwise check if the result of the MONTH function is 3,” and
so on.
Another way to print the appropriate month names would be to use one of the LOOKUP
functions. To do so, we can simply create a small table with the month numbers and
names in two columns. Then, we use the VLOOKUP function in the following manner to
find and print the appropriate month names in the flight table:
Now suppose that the supervisor needs to determine the number of hours worked each
day by a pilot in order to determine her pay. We must determine the difference in times
by subtracting the number of hours and minutes from the “Clock-in” and “Clock-out”
times. To do so, we need two new functions: HOUR and MINUTE. The function HOUR
takes the time (the only parameter) and returns the number of the hour to which it
belongs using a numbering system from 12:00 AM as 0 to 11:00 PM as 23. The MINUTE
function performs a similar operation by taking the time (again the only parameter) and
returns a minute number from 0 to 59. We can enter a time into the function or refer to a
cell that has a time as its value. The format for these functions is:
=HOUR(time)
=HOUR(cell_name)
=MINUTE(time)
=MINUTE(cell_name)
For example, the time 4:45 AM results in 4 for the HOUR function and 45 for the
MINUTE function. 4:45 PM results in 16 for the HOUR function and 45 for the MINUTE
function.
To work with the “Clock-in” and “Clock-out” times of each flight, we first check which time
has the larger hour value. For whichever is larger, we subtract the positive difference
between the two values. We check these conditions using the IF function. After ensuring
that these time differences are reflected correctly, we divide the minute values by 60 to
show the fraction of an hour that has been worked. We then take this result with the
difference in the hour values to find the total time worked for the day in terms of hours.
We type the following to calculate the first time difference:
=IF(HOUR(C4)>HOUR(D4),HOUR(C4)+MINUTE(C4)/60-
(HOUR(D4)+MINUTE(D4)/60),HOUR(D4)+MINUTE(D4)/60-
(HOUR(C4)+MINUTE(C4)/60))
The supervisor can now easily find the total hours worked for the cumulative time of this
Flight Sheet by copying the above formula for each flight and using the SUM formula to
add the values in this column (refer to Figure 4.37). These are just some of the Date and
Time functions, but the others are also useful for manipulating date and time information.
Figure 4.37 Calculating the time worked in hours using the HOUR and MINUTE
functions with the IF function.
The first function, ABS, finds the absolute value of any number or expression. Its format
is:
The ABS function may be used with a list of numerical values or with another function.
For example, we could use the ABS function as follows:
=ABS(SUM( …))
=ABS(MAX(…))
Another function, the PRODUCT function, finds the product of several independent
numbers or a range of numbers. This function, which is useful for finding the product of a
large list of values, takes the following format:
Suppose that after generating this data we determine the volume created for each set of
Height, Width, and Depth and then sum those volumes to determine our total volume
capacity from this randomly generated data. We create a new column for Volume and
calculate the volume of each row using product operators. For example, we calculate the
first value in the Volume column using the following:
=C5*D5*E5
To find the sum of these volumes, we use SUM for the values in the Volume column
(see Figure 4.38a):
=SUM(G5:G14)
However, another function, the SUMPRODUCT function, accomplishes this same goal in
one step and does not require the creation of a new table. SUMPRODUCT takes several
arrays as its parameters and then finds the sum of the product of each corresponding
element in these arrays.
=SUMPRODUCT(array1, array2, …)
Continuing with the volume example, the arrays we choose are the Height, Width, and
Depth columns featured in Figure 4.38. So, our formula is:
Given that these are our arrays, SUMPRODUCT takes the first entry from each column
and calculates the product of the three values. The SUMPRODUCT of the first row,
19.10, results from the product of 1.72, 4.97, and 2.24. SUMPRODUCT then repeats this
operation for the second entry in each column and adds the two products. It finds the
product of the three entries in each column and adds this value to the sum found in the
previous step. This process continues until each entry in each array has been multiplied
to find a product and until all of the products have been summed. We can see in Figure
4.38(b) that this function yields the same result as the more labor-intensive process of
creating a new table for volumes and using the SUM function.
The SUMPRODUCT function is important to understand. We will use this function often
when preparing the spreadsheet for solving optimization problems with the Solver. We
discuss using this function with the Solver in Chapter 8 and Chapter 19.
(a)
(b)
Figure 4.38 In (a), volumes are calculated for each row and the SUM function is used
to total this column of volume values. The SUMPRODUCT function used in (b)
multiplies the Height, Width and Depth columns and then adds the resulting volumes to
arrive at the same total volume value.
The MMULT function also provides a convenient way to work with arrays. This function
multiplies two matrices, or ranges, of values:
=MMULT(array1, array2)
=MMULT(range_name, range_name)
The MMULT function can only multiply two matrices if the number of columns of one
matrix equals the number of rows of the other matrix. For example, in Figure 4.39, we
have two matrices: one has two columns and five rows, and the other has two rows and
three columns.
Figure 4.39 The number of columns of the first matrix equals the number of rows of
the second matrix.
To find the matrix that results from the multiplication of these ranges, we first need to
highlight a range of cells that takes the dimension of the number of rows of the longer
matrix by the number of columns of the wider matrix. In the above example, the resulting
matrix from the product of the two matrices shown has five rows and three columns, as
shown in Figure 4.40.
Figure 4.40 Hold down SHIFT and CTRL to see the entire range of values in the
product matrix.
To see all values of the resulting matrix from the MMULT function, we hold down the
SHIFT and CTRL keys while pressing ENTER. If we do not hold down SHIFT and CTRL,
we will only see the upper left-hand value of the matrix product. Remember, this function
can also be used with range names. For instance, instead of using B3:C7 and E3:G4 as
the arrays in the above example, we could name the first matrix “matrixA” and the
second “matrixB” and use these names in the MMULT function (see Figure 4.41).
Figure 4.41 Range names can also be used with the MMULT function.
Notice that after entering the function for the range of cells, there are automatically “{“
signs before and after the function, and the function becomes the value for any cell in
that range. The SHIFT plus CTRL technique can be applied to fill in other formulas or
functions for any range of cells.
Some other useful Math & Trig functions have simple parameters and are easy to use.
We recommend experimenting with SQRT, which finds the square root of a number. Its
only parameter is a number or cell reference. Some other useful functions are SIN, COS,
and TAN, which find the sine, cosine, and tangent of an angle. To use these angle
functions, we need to enter an angle in radians as its parameter, or refer to a cell that
has an angle in radians as its value. Therefore, we also need the function PI, which
takes no parameters but returns the value for pi. We recommend looking through the
function list in this category and use the function instructions as a guide while you
experiment with data.
We will discuss in detail the RAND and RANDBETWEEN functions, random number
generators, in Chapter 9.
We will describe in detail such statistical functions as MEAN and STDEV in Chapter 7,
as well as such distribution functions as NORMDIST, BETADIST, CHIDIST, and
EXPONDIST. At that time, we will also provide useful tools for data analysis. In Chapter
10, we will discuss the COUNT functions, COUNTIF and SUMIF, with databases
functions.
Many Financial functions are available in Excel. We will describe six common ones and
some of the depreciation functions as well. We will use a more detailed example to help
you understand the applications of these functions.
Figure 4.42 presents a table of the Financial Debts and Assets for a series of machines,
perhaps used in farming or manufacture. For each machine bought, we know the
“Unpaid Debt,” the “Monthly Loan Rate,” the “Date Purchased,” the number of
“Payments Left,” and the monthly “Payment” to be made. To begin, suppose that we do
not know the number of “Payments Left” for machines B and E. To calculate this
number, we can use the function NPER. This function uses the rate, payment per period,
and unpaid amount as parameters to calculate the total number of remaining payments.
The parameter present_value refers to the amount that remains unpaid. As we will see
later, the future value can also be used as a parameter. While we do not need to include
both the present_value and future_value when using the NPER function, we do need to
include at least one of these values. The final parameter, type, specifies if payments are
made at the beginning of the period, 0 (the default), or at the end of the period, 1.
In this example, we type the following function for Machine B. Notice that we leave the
last two parameters blank since we consider our unpaid debt to be the present value of
the payment and we use the default parameter value for type.
We find that if we are paying $4,000.00 per month (period_payment) at a loan rate of 12
percent per month (rate), it will take us 8.09 months (or payments) to pay the entire
$20,000 debt (present_value). The result for this calculation appears in G5 in Figure
4.42(a).
Likewise, if we assume that the monthly payment amounts for Machines A, C, and D are
unknown, then we must calculate them. To do so, we use the function PMT, which uses
the parameters of rate, number of payments left, and unpaid amount to calculate the
monthly payment amount. Again, the unpaid amount is entered as the present value of
the payment, and the future value can be used as a parameter. PMT uses the same
criteria for the parameter type as NPER:
We find that we must pay $1,627.45 per month to finish paying the $10,000 debt in 10
payments (Nper) at a 10 percent monthly loan rate (rate) (see Figure 4.42(b)).
(a)
(b)
Figure 4.42 (a) The NPER function uses the rate, monthly payment amount, and
unpaid amount to calculate the number of remaining payments. (b) The PMT function
uses the rate, number of payments left, and unpaid amount to calculate the amount to be
paid monthly.
Now let’s consider a record of “Investments” (see Figure 4.43). For each fund, we know
the “Months Left” from which we can invest in this fund, the “Monthly Payments” we
make to the fund, the “Interest Rate,” and the “Ending Value” we plan to receive from the
investment. If we need to calculate the “Monthly Payments” for Fund C, we could again
use the function PMT. However, here we do not have an “Unpaid Debt” to pay off, but
rather an “Ending Value” that we are trying to achieve. We therefore have to leave the
parameter of “Unpaid Debt” (or present value) blank and enter another parameter for the
“Ending Value” (or future value). We type the function:
We can see in Figure 4.43(a) that our “Monthly Payment” (or investment) amount should
be $132.33 if we wish to receive $12,500.00 (future_value) in 30 months (Nper) at a 7
percent interest rate (rate).
We can also find the “Interest Rate” if it is missing. If we know the number of “Months
Left” to invest (D5), the “Monthly Payment” amount (E5), and the “Ending Value” (G5) we
want to achieve for Fund B, we can enter these values as parameters in the RATE
function. The parameters for this function are the number of periods, payment per
period, present value or future value, and the type:
For Fund B, then, we enter the following parameters into the function. Note that we have
left the present value parameter blank and used the future value instead,
Lastly, let’s discuss how to calculate the “Ending Value.” For Fund A, we can use the
given “Interest Rate” (F4), “Monthly Payment” amount (E4), and number of “Months Left”
(D4) to determine what value the investment will return. Here, we use the FV function to
calculate the future value with these known values as our parameters:
In Figure 4.43(c), we have entered this function for the cells shown:
We conclude that investing in Fund A will return $6,590.00 at the end of 10 months.
(a)
(b)
(c)
Figure 4.43 (a) Using the PMT function with the future value instead of the present
value to determine the monthly payments that should be made. (b) The RATE function
uses the number of months, payment amount, and future value to find the interest rate.
(c) The FV function calculates the future value using the other known values as
parameters.
The NPV and IRR functions are two other important functions to become comfortable
using. NPV calculates the net present value given the interest rate and payments each
period. Unlike the above functions, NPV allows us to consider varying payment amounts
per period. This function provides 30 different payment values that we can enter as
parameters. If these multiple payments are kept in a range of cells, we can enter the
range name as the parameter:
(a)
(b)
Figure 4.44 (a) Using the NPV function to calculate the net present value given the
interest rate and payments per period. (b) The IRR function calculates the internal rate
of return given the payments per period, including the initial investment.
If the payment periods are irregular, that is, if they occur at irregular intervals, we can still
calculate the NPV using the XNPV function. Its format is:
The range of values should have the earliest date listed first, but other dates do not need
to be sorted.
We can also calculate the internal rate of return of these payments. We do this using the
IRR function, which takes the payments for all periods (including the initial investment)
as a range of cells. It also has a guess parameter used to estimate the IRR; the default
value is 0.10, or 10 percent, if left blank.
=IRR(payment_range, guess)
In Figure 4.44(b), we have entered the following function. (Note that we left the guess
value blank.).We can observe the IRR calculated for each location in the figure.
=IRR(F5:F10)
Excel also has some depreciation functions. Let’s now look at the functions that perform
straight-line depreciations, sum of year’s digits, and fixed declining balance. The SLN
function calculates the straight-line depreciation of our assets. It takes the initial cost, the
salvage at the last period, and the useful life of our asset (the number of periods that our
asset is worthwhile).
In Figure 4.45, we use the same data from the “Manufacturing Plant” example to
calculate these depreciations. In this example, we use the SLN function to determine the
straight-line depreciation of the Orlando data. Notice that the initial cost is written with a
negative sign:
=SLN(-E5, E11, 5)
Figure 4.45(a) displays the results of the SLN function after calculating the straight-line
depreciation.
For the sum of year’s digits, we use the SYD function, which calculates the sum of the
year’s digits for a given period in the depreciation calculation. The parameters are the
initial cost, the salvage at the last period, the useful life of the asset, and the period in
which we are interested.
Figure 4.45(b) illustrates this calculation and its results for the Miami supplier. (Note
again that the initial cost has a negative sign.)
=SYD(-F5, F11, 5, 1)
Like the SYD function, the fixed declining balance function DB calculates depreciation
using the initial cost, the salvage at the last period, the useful life of the asset, and the
period of interest as its parameters:
See Figure 4.45(c) for the results of the calculations of the following DB function for the
Pensacola supplier:
=DB(-G5, G11, 5, 1)
(a)
(b)
(c)
Figure 4.45 (a) The SLN function calculates the straight-line depreciation. (b) The
SYD function calculates the sum of year’s digits for a period of interest. (c) The DB
function calculates the fixed declining balance.
Financial Functions:
NPER Determines the number of periods needed to pay off an
unpaid amount.
PMT Determines the payment for each period.
RATE Calculates the interest rate.
FV Calculates the future value of investments or payments.
NPV Calculates the net present value of an investment.
IRR Calculates the internal rate of return for an investment.
Summary
SLN Finds the straight-line depreciation for assets.
SYD Finds the sum of year’s digits for a given period.
DB Finds the fixed declining balance for assets.
If we select this option, the second drop-down list, where we have previously worked
with such options as “greater than” and “less than,” disappears (see Figure 4.46). Now
we can write a formula in this area.
The formulas used in Conditional Formatting are similar to those of the Logical category.
For example, if we type “=E5>E4” in the formula area of the Conditional Formatting
dialog box, Excel checks if this statement is true or false. If it is true, then the formatting
specified is applied to the initially selected cell, or range of cells. Note that we cannot
use absolute referencing when entering formulas here.
Figure 4.47 A record of sales for each quarter over three years.
We now begin by selecting the range B3:B14 (or C3:C14). We select Conditional
Formatting from the Format menu option and then Formulas Is from the first drop-down
list. We now type the following two conditions (see Figure 4.48):
(Remember that we add the second condition by selecting the Add button and again
selecting Formulas Is from the drop-down list.)
Note that we compare the first cell in our range with an empty cell outside of the range in
order to initialize the comparisons. The purple quarters in Figure 4.49 now have
increased revenues and the yellow quarters have decreased revenues from previous
quarters.
4.9 Auditing
Excel’s auditing feature aids in data validation and verification. Auditing unveils all of the
data cells involved in a function or reference. That is, if our function finds the sum of
several values, auditing reveals the values from which the sum is calculated. To use the
auditing feature, we select Tools > Auditing from the menu. We find a few options that
we will describe briefly, including the Auditing toolbar, which we will use in our examples
(see Figure 4.50).
Figure 4.50 The Auditing toolbar displays all of the auditing options.
Auditing performs two primary actions: tracing precedents and tracing dependents.
Tracing a precedent of a selected cell establishes which cells provide data for a function
or
reference in that selected cell. For example, suppose we have used the SUM function
to calculate the sum of a set of values. If we select the cell with the SUM
function and click Trace Precedents, which is the first icon on the Auditing toolbar, then
the cells
that we are summing are highlighted and an arrow points from those cells to our
selected SUM cell.
For example, in Figure 4.51 we have a SUM function in cell D3 whose formula is:
=SUM(B3:B9)
If we click on cell D3 and then press the Trace Precedents icon on the Auditing toolbar,
the arrow shown in the figure appears. It points from the highlighted column B3:B9 to the
Sum cell, implying that the SUM formula in the Sum cell references this column of cells.
Figure 4.51 Selecting the Sum cell (D3) and choosing Trace Precedents from the
Auditing toolbar displays an arrow pointing from all of the cells involved in the SUM
function (B3:B9).
The next icon in the Auditing toolbar removes the arrows that were previously created.
Tracing the dependent of a selected cell, the third icon in the toolbar, presents all of the
cells that are dependent upon the selected cell to perform a function. Using the same
example above, if we now select one of the cells from the Values column, say cell B5,
and click on Trace Dependents, the third icon in the Auditing toolbar, then an arrow
points from our selected cell to the Sum cell showing that the Sum cell uses this value.
Figure 4.52 Selecting one of the Value cells (B5) and choosing Trace Dependents
from the Auditing toolbar displays an arrow pointing to the Sum cell (D3), which uses the
Value cell to perform a function.
The next icon in the Auditing toolbar also removes the arrows that were previously
created. The other actions in the Auditing feature include tracing errors, highlighting
invalid data, and posting notes in our spreadsheet.
4.10 Summary
¾ There are ten different categories of functions: Financial, Date and Time,
Mathematics and Trigonometry, Statistical, Lookup and Reference, Database, Text,
Logical, Information and Engineering.
¾ There are only six Logical functions: TRUE, FALSE, NOT, IF, AND, and OR. These
are important functions for determining whether certain criteria are met. The IF, AND,
and OR functions are more commonly used for both spreadsheet applications and
VBA programming.
¾ There are several different Information functions, all of which provide general
information about data. The IsFunctions, a category of information functions,
determine whether a value has a specified characteristic. The TYPE function
categorizes the data type of any possible value and is an important concept to know
for VBA programming.
¾ Text functions manipulate text in any cell or range. Aside from simple changes in text
format, you can also CONCATENATE, or join, two or more segments of text and
SUBSTITUTE specified letters or words.
¾ The “&” sign denotes CONCATENATE.
¾ Lookup and Reference functions search through data to find values that match
certain criteria. The VLOOKUP, HLOOKUP, and MATCH are the most commonly
used functions in this category. The INDEX and OFFSET functions are also very
useful reference functions.
¾ Date and Time functions work with calculations involving dates and times. Nine of
these functions are: TODAY, NOW, NUMWKDAYS, DAYS360, YEARFRAC,
WEEKDAY, MONTH, HOUR, and MINUTE.
¾ There are several Math & Trig functions, some of which we already discussed in
Chapter 2. Some other common functions in this category are RAND,
RANDBETWEEN, and SUMPRODUCT. The MMULT function multiplies two
matrices, or ranges, of values. You should also be familiar with SQRT, SIN, COS,
TAN, and PI.
¾ Nine Financial functions you should know are NPER, PMT, RATE, FV, NPV, IRR,
SLN, SYD, and DB.
¾ The Formula Is option of Conditional Formatting allows you to specify conditions
using logical comparisons of several cells.
4.11 Exercises
4.11.1 Review Questions
1. How many function categories does Excel have? What are they?
2. What are the six logical functions?
3. What three parameters does the IF function require?
4. What are the three data types that can be determined with the TYPE function?
Provide the number used to represent each data type.
5. What does each of the match type codes of the MATCH function return?
6. What is a serial number for in Excel? What are the initial starting points of the serial
numbers for the date and time?
7. If you want to count the days from a Monday to the next Monday without any
holidays, how many days would Excel calculate with the following functions?
a. NETWORKDAYS
b. DAYS360
8. What are the different options for the second parameter of the WEEKDAY function
and what do they represent?
9. What are two ways to generate a random number between 1 and 20?
10. What does the NPER function do and what information does it require?
11. What information is needed to calculate the sum of year’s digits using Excel?
12. What are the four categories of formulas?
13. Which icon leads to the function menu?
14. How do you enter the value pi into a cell?
15. Do cells that you want to concatenate need to be neighboring each other?
16. Which Excel function returns the number of empty cells in a specified range of cells?
17. Give an example of an instance when the YEARFRAC function would be useful.
18. How does the HLOOKUP function work?
19. Compare the usefulness of the INDEX and OFFSET functions.
20. What are the requirements for the size of the arrays that can be used with the
MMULT function?
1. Enter the following set of numbers into an Excel spreadsheet, putting each number in
a separate cell: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}.
a. Give the results of using the MATCH function to search for the number 5 in this
range of cells with each of the following match type codes:
• 1
• 0
• -1
b. Did any of the results you obtained in part (a) contain errors? If so, why did
these errors occur?
2. Enter the following set of numbers defined as S into an Excel spreadsheet, putting
each number in a separate cell: S = {10.1, 7.3, 1.9, 5.6, 12.7, 14.0, 4.1, 5.6, 8.9}.
Then find the sum, average, minimum value, and maximum value of the set by using
mathematical and statistical functions.
3. The table below is used to compute the height of a cylinder given the cylinder’s
volume and radius. Enter the table into an Excel spreadsheet and use the following
formula to compute the value of the height for each of the given radii using the
volume shown:
Height = Volume/(Radius^2)
Volume (ft^3): 10
Radius (ft) Height (ft)
3
2
4
3
4. Given the following series of future cash flows, use Excel functions to determine the
net present value of an investment made at an interest rate of 8%: {-$1200, $300,
$450, $585, -$275, $645, $800}.
5. Wasser & Wasser Distributors, Inc. wants to find and buy a building to use as a new
distribution center. The ideal building would be at least 175 feet in length, at least
100 feet in width, have a ceiling height of at least 30 feet and be no more than 5
miles from a highway. The company will pay up to $2,000,000 for a building that
meets these specifications. The company will also consider buildings that are at least
165 feet in length, at least 90 feet in width, have a ceiling height of at least 25 feet
and are no further than 10 miles from a highway. The company is willing to pay no
more than $1,800,000 for a building that meets, but does not exceed, this second set
of guidelines.
a. Enter the table below into a spreadsheet. Using the Create option, define
names for each column.
b. To complete the last column of the table, use logical functions to determine
which buildings are acceptable for the company to consider.
Closest
Highway
Building Length (ft) Width (ft) Ceiling (ft) (miles) Price Acceptable?
A 200 90 35 1 $1,958,274
B 155 105 40 4 $1,849,582
C 165 110 40 9 $1,683,912
D 185 120 30 3 $2,039,487
E 140 110 25 5 $1,930,492
F 160 125 30 10 $1,759,302
G 175 115 20 2 $1,758,292
H 185 100 35 3 $1 950 321
7. A medical center must pay close attention to several health factors to determine what
type of meals patients should receive. The factors to consider include the patient’s
age, weight, blood pressure, and cholesterol level. Patients should receive low
sodium meals if their blood pressure is high and if they meet at least one of the
following conditions: their age is at least 70 or their weight is at least 200. Patients
should receive low cholesterol meals if their cholesterol level is high and if they meet
one of the following conditions: their age is at least 70; their weight is at least 200; or
their blood pressure is high. Patients may also have low sodium and low cholesterol
meals if they satisfy the conditions for both the low sodium meal and the low
cholesterol meal. If the patient does not satisfy the conditions for either the low
sodium meal or the low cholesterol meal, then he or she receives a regular meal.
Enter the table below into a spreadsheet. Create names for the columns of data.
Using logical functions and the names you defined, find the type of meals each
patient should receive. Results should be one of the following: “Regular Meal,” “Low
Sodium Meal,” “Low Cholesterol Meal,” or “Low Sodium/Low Cholesterol Meal.”
8. The table below is used to compute the average acceleration of a moving object
given the time and velocity of the movement. Define names for each of the columns,
and calculate the missing data by referring to the cells by their defined names.
Avg. Acceleration
Time (s) Velocity (m/s) (m/s^2)
1 5
5 15
10 40
20 75
30 85
45 90
60 90
Max Velocity:
Max Avg.
Acceleration:
9. The manager of a manufacturing plant is about to place an annual order for new
material handling equipment for the plant. Every year he must determine which
equipment to replace and which to keep for another year. The factors involved in this
decision include the cost of new equipment, the age of the current equipment, the
equipment’s useful life, and the salvage value of the equipment. The table below
displays the data for each of these factors. Any piece of equipment that is as old as
its useful life must be replaced. The manager will also replace any piece of
equipment with a net cost (the cost for a new piece of equipment minus the
equipment’s salvage value) under $250. Using logical functions and the conditions
mentioned above, complete the final column of the table with a “Yes” or a “No” to
indicate whether or not the equipment should be replaced.
Equipment Type Cost For New Age Useful Life Salvage Value Replace?
boom crane $ 575.50 1 3 $300.00
chain hoist $ 149.99 5 5 $100.00
electric winch $ 497.50 1 6 $250.00
fork lift $2,499.95 3 7 $2,000.00
leveling jack $ 112.50 4 4 $75.00
motorized trolley $1,299.95 2 5 $1,000.00
10. An electrical engineer has designed a circuit with three light emitting diodes (LEDs)
placed at various points along the circuit to detect if a current is running through that
section of the circuit. Two switches are also located at various points along the circuit
to control the flow of the current. The engineer tries closing every combination of
switches to see which LEDs will light. She obtains the following results:
The engineer created the table below to display her results. Using logical functions,
show each LED as being either “On” or “Off” for each of the trials.
Switch 1 Switch 2
Trial No. Closed Closed LED 1 LED 2 LED 3
1 Yes Yes
2 Yes No
3 No Yes
4 No No
11. Use Excel functions to determine the future value of a $5000 loan made at a 5%
interest rate from the lender’s perspective. Assume that the investment is made in
individual payments of $1000 over the course of five periods and that all payments
are made at the beginning of each period.
12. Enter the following list into an Excel spreadsheet. Then, in a separate column,
use a function to determine which cells do not contain text values. Text values
should return the word “FALSE” and all other values should return the word “TRUE.”
1
2
hi
hello
5
6
goodbye
7
13. Enter the following table into a spreadsheet. Add an additional column that uses
functions to concatenate the given data into sentences of the following form: “First
Name Last Name is Age years old.” For example, the first sentence should be: “Jon
Henley is 39 years old.” Be sure to use appropriate spacing between words.
14. A payroll worker uses the table below to determine the number of days an
employee works during a given pay period. To do this, he must first determine the
number of workdays in the pay period less any holidays given to the employees and
then subtract any sick days taken by an employee. Complete the table by using
functions to calculate this value.
15. A college professor uses the spreadsheet shown below to tabulate her students’
grades. Each test has a maximum score of 100 points and constitutes 30% of a
student's final grade. The remaining 10% of a student's grade comes from the
averages of the three homework assignments, each worth a possible total of fifteen
points.
a. Enter the table of grades into an Excel spreadsheet.
b. Using the VLOOKUP function, fill in the values for the Test 1 Score, Test
2 Score, Test 3 Score, and Average Homework Score located at the
bottom of the spreadsheet for student number 3.
c. Use formulas to compute the Final Course Grade for student number 3.
Student
ID Test 1 HW 1 Test 2 HW 2 Test 3 HW 3
1 78 10 93 11 85 13
2 89 15 88 11 83 15
3 84 12 76 15 55 14
4 64 13 72 14 79 14
5 77 15 85 13 82 14
6 92 15 90 15 67 8
7 65 9 87 11 77 0
8 58 12 91 10 65 12
9 81 8 79 9 88 12
10 91 14 83 14 78 15
16. A student uses the Excel spreadsheet shown below to assist him in planning
important dates in one of his courses. Enter this table into a spreadsheet. Complete
the column labeled “Days Until Next Event” with functions to calculate the number of
days since the last event that the next event will occur. Then, complete the column
labeled “Day of Week” with functions to determine the day of the week on which
each event occurs. Display the values in this column as “Sunday,” “Monday,”
“Tuesday,” etc.
Today 8/26/03
17. A clothing retailer maintains a table of information about customers who have
signed up for a store credit card, as shown below. These customers receive special
promotional discounts, including a coupon to use during the month of their birthdays.
Recreate this table in an Excel spreadsheet.
a. Some customers’ birth dates are missing. Create a new column in the
table that displays the word “TRUE” if the date of birth is missing for a
particular customer.
b. If the customer’s birth date is present, create another new column in the
table that displays the month of the customer’s birthday.
c. Create a final column to indicate whether a birthday coupon should be
sent to a customer this month. If the month of the customer’s birthday is
the same as the current month, display the word “SEND” to indicate that
the coupon should be sent. If the birth month is not the same as the
current month, display the phrase “DO NOT SEND.”
18. An airline uses the table below to track information about each of its scheduled
flights. Recreate this table in an Excel spreadsheet.
a. After its flight from Orlando to Los Angeles, the Boeing 737 requires
maintenance and is unable to fly as scheduled. A Boeing 727 will fly in its
place. Use an Excel function at each instance after the Orlando to Los
Angeles flight to substitute every instance of the phrase “Boeing 737” with
the phrase “Boeing 727.”
b. Due to a snowstorm, all flights out of Chicago have been cancelled. Using
the VLOOKUP function, return the flight number of all the cancelled flights
in a cell at the bottom of the spreadsheet.
c. The flight attendants need to know the number of minutes after the hour
each flight arrives at its destination. In a new column, calculate this value
for each flight based on the departure and travel times.
Flight No. Type Start City Destination Departure Time Travel Time
1 Boeing 737 Orlando Los Angeles 12:15 7:15
2 Boeing 747 Chicago Dallas 17:25 5:20
3 Boeing 737 New York Las Vegas 14:00 8:40
4 MD 180 San Jose Seattle 6:35 1:50
5 MD 200 San Francisco Sacramento 9:50 3:45
6 Boeing 737 New York Detroit 11:25 2:00
7 Boeing 737 Tampa Chicago 13:30 5:05
As he buys more ads, the price per ad drops; in other words, if he buys 8 adds,
he pays $11,000 per ad, while if he buys 14 ads he pays $10,000 per ad. Write a
formula that determines the total cost of purchasing any number of ads from 1 to
25.
20. In the table below, the units sold of a particular product are stored for several
countries along with the total revenue and variable cost of selling to those
countries.
Now create a formula to find the Units Sold, Revenue, and Variable Cost for
any given country.
21. The table below displays the varying annual rates charged by a bank for varying
amounts of years of money borrowed.
This data can be used to interpolate the annual rate for years not recorded in the
table. For example, the annual rate for borrowing money for 15 years can be
calculated by noting that 15 is ¼ of the time between 10 years and 30 years, so
the annual rate would be:
Create a formula that will find the annual rate for any period between 1 and 30
years.
22. Refer to the distance table provided in the example for the INDEX function. Find
the following:
a. The distance between Los Angeles and Chicago.
b. The distance between New York and Atlanta.
c. The total miles traveled on a trip from Chicago to Boston to Atlanta to
Miami.
d. The city closest to New York.
e. The city farthest from Seattle.
23. The table below records high and low temperatures for each month of the year.
Use the OFFSET and other LOOKUP functions to determine the following:
a. The month with greatest High Temperature.
b. The month with the least Low Temperature.
c. The average High Temperature for the months of June, July, and
August.
d. The average Low Temperature for the months of December, January,
February.
24. Using the table from exercise 21, determine the following by creating formulas:
a. How many countries bought more than 400 units?
b. Which country had the largest Revenue?
c. Which countries had a Variable Cost between $500 and $600?
25. Using the information given in problem 3.22 calculate the Euclidean and Rectilinear
distance from the candidate locations for the DC to customer locations. In calculating
the distances use the Excel functions ABS, SQRT, POWER, and SUM. Present the
total distance traveled from each candidate location to the customers.
26. A manufacturing company is planning to buy new equipment that cost $100,000.
Because of this investment, they estimate the following annual savings during the
next 7 year: $14,000; $17,000; $23,300; $22,000; $21,000; $18,000; $16,700. Use
Excel functions to calculate the following:
a. Net present value of this investment. Assume that the return in investment for
this company is 5%.
b. Internal rate of return.
c. Payback period.
d. Since the APR given by a local bank is only 3%, the company is planning to
borrow the money needed to buy the equipment. Identify the yearly payments
the company should make to pay off the loan in 5 years.
28. Parasol Systems sells motherboards for personal computers. They use the following
model to price their products: for quantities up to 30, the company charges $325.00;
for quantities between 30 and 50 they charge $300.00 for each board beyond 30;
and they charge $275.00 per board for quantities beyond 50. The following table
presents the last the orders for motherboards. Calculate the total revenues.
29. A furniture manufacturing company uses a special type of woodworking glue in the
assembly of its furniture. During the last 36 weeks, the following amounts of glue (in
gallons) were used by the company.
30. Over a period of 12 consecutive production hours, samples of size 50 resulted in the
following proportion of defective items:
a. Calculate the 3-sigma upper and lower control limits for this process. Note: (i)
UCL = mean + 3*std. dev; and LCL = mean – 3*std. dev. (ii) the lower control
limits cannot be negative.
b. Count the number of sample points that fall out of these limits.
Charts
Chapter Overview
5.1 Introduction
5.2 Creating Charts with Chart Wizard
5.3 Working with Chart Options
5.4 Creating Dynamic Charts
5.5 Summary
5.6 Exercises
Chapter 5: Charts 2
5.1 Introduction
This chapter shows the user how to create and work with charts in Excel. This is mostly
an introductory chapter for the inexperienced Excel user; however, even an experienced
user may benefit from reviewing the section on dynamic charts. Charts will be used
extensively in creating DSS applications as they provide a great visual aid in displaying
output to the user. Understanding dynamic charts will be important when charts are
manipulated with VBA. Dynamic charts will be used in simulation and other user
interface for many DSS applications.
Suppose we want to compare which Car Model sold the most and which Car Model
generated the most revenue. We will create two different charts to do this: a bar graph
and a pie chart. We begin by creating a bar graph of the number sold for various models.
First, we need to highlight the data that we want to graph. In our example, we highlight
the cells B4:B10 and E4:E10 (by holding down the CTRL key). We have highlighted the
column titles so that we can label data automatically when creating the chart. To begin
using the Chart Wizard, we either choose Insert > Chart from the menu or click the
icon at the top of the window. The following screen then appears (see Figure 5.2).
Figure 5.2 The Chart Wizard allows a user to create a chart from data by following a
series of simple steps and option selections.
Step 1 features a selection of Chart types. Clicking on each Chart type displays all of the
Chart sub-types, which are the various display options associated with that particular
Chart type. First we want to create a vertical bar graph from our data. To do so, we
select Column from the list of Chart types and click on the top left Chart sub-type. We
will only discuss the Standard Chart Types in this chapter; however, the selection
process for the Custom Chart Types parallels what we will be demonstrating.
After we click Next, A new window with two tabs, Data Range and Series, appear. A
Data Range is the range of cells that we initially highlight to plot. In this tab, we are
asked to verify or change our selected Data Range (see Figure 5.3(a)). The following
Data Range appears in the window:
This step allows us to verify that we have highlighted the correct data range. Therefore,
we do not modify our Data Range. After checking the data range, we can then select an
option to define our Series. Series are various sets of data.
We can view the ranges of each individual series of data by selecting the Series tab at
the top of the window (see Figure 5.3(b)). For each Series listed in the window, we can
see the corresponding range of data in the Values section. Series1 corresponds to the
first column of the data we selected while the other series apply to their corresponding
columns.
(a)
(b)
Figure 5.3 Step 2 of the Chart Wizard allows a user to verify or change the Data
Range and determine how to define the Series. (a) Data Range selection and the row
versus column option for Series. (b) Range selections and names for each Series.
We can also assign each series a Name. Note that if we include column titles while
highlighting our initial data, then Excel automatically uses those titles to create the series
names.
Pressing Next again brings us to the third step of the Chart Wizard that defines the final
chart options (see Figure 5.4). In the first tab, Titles, we create labels for our axes and
the chart itself (see Figure 5.4(a)). In the Axes tab, we can decide whether we want to
show the values of the x- and y-axes (see Figure 5.4(b)). In the Gridlines tab, we can
determine how many gridlines should be shown in the x- and y-directions of the graph
(see Figure 5.4(c)). In the Legend tab, we can choose the location of our legend or
choose not to have it displayed at all (see Figure 5.4(d)). In the Data Labels and Data
Table tabs, we can determine which, if any, labels will appear with the data points and
whether we want the original data table to appear below the graph (see Figures 5.4(e)
and 5.4(f)).
(a)
(b)
(c)
(d)
(e)
(f)
Figure 5.4 Step 3 of the Chart Wizard defines the Chart Options. Each tab at the top
of the window presents options for each category.
We again press Next to arrive at the final step of the Chart Wizard, which allows us to
select the location of the chart (see Figure 5.5). If we choose As new sheet, Excel
maximizes the chart to fill an entire new worksheet. We can then label the name of this
worksheet. Here it is automatically given the name “Chart1.” Choosing the chart as an
object in a sheet leaves it as a separate smaller window in the current worksheet or in
another sheet of our selection. In this figure, we have chosen to include it automatically
as an object in the current sheet.
Figure 5.5 Step 4 of the Chart Wizard is to select the location of the chart.
The resulting bar graph is in Figure 5.6. We can now determine that the Aco3500 had
the highest sales.
We can now follow the Chart Wizard steps again to create a pie chart of the Revenue
Generated by each car model. The final chart should look like the chart shown in Figure
5.7. Here, we can see that the Cam3200 generated the most revenue.
Figure 5.7 The pie chart of Revenue Generated per car model.
Chart Wizard
Step 1: Select the Chart Type and options.
Step 2: Verify or change the Data Range and define the Series.
Step 3: Determine Chart Options.
Summary Step 4: Choose the location of the chart.
The Format Chart Area option also allows us to modify the general patterns, font, and
properties of the chart. By clicking on different areas of the chart, we can format other
elements as well. For example, if we right-click on the main plot of the chart (which
usually has a default grey background), we can Format Plot Area and change the
background color. We can also right-click on the elements of the chart, including points,
lines, and bars, to Format Data Series. This allows us to change point shapes, line
styles, colors, and views of our data series. We can also just select a specific data point
and Format Data Point by right-clicking.
To Format Axis, we can select the x-axis or y-axis (or z-axis if we have a 3D chart) and
right-click. Here, we not only have pattern and font options, but also scale, number type,
and alignment. The scale option can be very useful if we are trying to “zoom-in” on or
“zoom-out” of the data (see Figure 5.8). Gridlines can be formatted by right-clicking on
any of the gridlines in the chart and selecting Format Gridlines; they also have a scale
option, which can be modified. Remember, we can choose which gridlines to have on
our chart by returning to Chart Options. The chart title, axis labels, and legend fonts and
text can be modified by directly clicking on them.
Figure 5.8 The Format Axis option is one of many formatting options available for a
chart.
The Chart Wizard also has other options beneficial for complicated data or charting
specifications. Let’s look at another example to explore some of these options. Consider
an experiment that measures the growth of a plant relative to the amount of minerals
added to or removed from the surrounding soil. The data recorded from this experiment
is provided in Figure 5.9. To graph this data, we highlight the values in B3:C22 and click
on the Chart icon .
In the first step of the Chart Wizard, we select a Scatter graph from the list of Chart
types. The Chart sub-type choice features a line that connects the data points (see
Figure 5.10). In the next step, presented in Figure 5.11, we verify that our Data Range
contains all of the data in the table (B3:C22) and that our data will be grouped by
columns. Note that our Data Range contains our column titles as well. Therefore, we will
not have to name our series, as they will automatically be created from these column
titles.
We only have one series of data, the “Minerals” level, so we do not need to show this in
a legend next to our graph. Therefore, in the next step, under the Legend tab, we
deselect the Show Legend option (refer to Figure 5.12).
Figure 5.12 Deselecting the Show Legend checkbox removes the series legend from
the graph.
We may also decide to remove all of the gridlines from the graph. We can do this in the
Gridlines tab by deselecting all of the checkboxes (see Figure 5.13). We can now click
Next and choose to place the chart as an object in the same worksheet as our table. Our
chart is now finished (see Figure 5.14).
Figure 5.13 Removing all gridlines by deselecting all checkboxes in the Gridlines tab.
After analyzing the data relationship shown in the graph, we may decide that the growth
progress would be clearer if the mineral levels were shown as the x-values and the
growth amounts were shown as the y-values. To make this switch, we right-click on the
graph area and select Source Data from the list of options. Under the Series tab, we find
the current data selected for the x- and y-values (see Figure 5.15a). We can redefine
this data by clicking on the x-value box, highlighting the “Minerals” column from our
table, and then clicking the y-value box and highlighting the “Growth” column. We have
now switched the x- and y-values for the chart (see Figure 5.15b).
(a) (b)
Figure 5.15 (a) The x-values are the growth and the y-values are the mineral levels. (b)
The x-values are the now the mineral levels and the y-values are now the growth.
We now click OK and observe that Excel has updated our chart (see Figure 5.16).
Figure 5.16 The chart is updated to reflect the new x-values as mineral levels and the
new y-values as growth amounts.
The Excel Chart Wizard allows us to define many features of a chart. It is important to
make sure that we represent our data clearly so that Excel can easily conduct analysis.
The chart is an important part of the spreadsheet application, and we recommend that
you now familiarize yourself with all of its options.
A chart is linked directly to the Data Range specified in the Source Data step of the
Chart Wizard. If any points in this range of data are modified, the chart is automatically
updated to reflect a new corresponding data point. In reality, we could specify an empty
range as the Data Range if we created the chart before the data was entered into the
spreadsheet. Then, the chart would be updated as new data was entered. In VBA, we
will see how to perform these actions automatically so that the user can observe a chart
being created one data point at a time. However, before we learn any VBA coding, we
should use some Excel functions we have already learned to create a dynamic chart.
We will use three main Excel concepts to create a dynamic chart: defining names, the
OFFSET function, and the COUNT function. We will first use the OFFSET and COUNT
functions to create some range names and then set the Series of the chart to these
dynamic ranges. We will use the following example to demonstrate this process. Figure
5.17 features production data that records the “Units Sold” for each month of the past
year. We have created a Scatter Chart for this data using the Chart Wizard. In this
figure, the Source Data is currently linked to the range B4:C15.
Figure 5.17 The original data and chart; the chart is currently not dynamic.
Our first step is to create two dynamic ranges: one for “Months” and one for “Units Sold.”
By creating these dynamic ranges, we ensure that our range names include new data
points as they are entered into either column of the table. We therefore use the OFFSET
and COUNT functions in the following manner:
=OFFSET(initial_data_location, 0, 0, COUNT(entire_column), 1)
The rows_to_move and columns_to_move parameters are set to 0 because we are only
interested in the column in which our reference_cell ( = initial_data_location) is located.
The width is again set to 1, since we are interested only in one column. The height
parameter is found using the COUNT function. The COUNT function reviews the entire
column of the relative data and counts how many cells have numeric values. So, the
height of our range becomes dynamic as the amount of numeric values in the column
increases.
We now create two dynamic ranges, named “Months” and “UnitsSold,” with the Define
Name option. In the Refers To window of this dialog box, we use the above functions as
follows:
In the Define Name window, we also use sheet names and absolute referencing (see
Figure 5.18).
Figure 5.18 Defining the dynamic ranges using the OFFSET and COUNT functions.
Now, we can simply use these dynamic range names to define our data range. We right-
click on the already-created chart and choose Source Data from the list of options. We
then go to the Series tab and modify the X Values and Y Values windows. Using the
workbook name, we place the dynamic ranges in their corresponding windows as follows
(see Figure 5.19):
=WorkbookName.xls!Month
=WorkbookName.xls!UnitsSold
Figure 5.19 Entering the dynamic ranges as the corresponding series values.
Now, if we enter data for sequential months in the data table, the chart is updated
automatically. In Figure 5.20, we have added data for months 13 to 16; without
modifying the data range again, the chart is updated to reflect the values of these new
points. We have therefore created a dynamic chart.
Figure 5.20 The dynamic chart is now created. As new data values are entered, the
chart is automatically updated.
5.5 Summary
¾ Excel Charts allow you to illustrate your data in order to perform better analysis.
When using charts, you should understand Data Ranges, Ranges, and Series.
¾ A Data Range is the group of cells selected to create a chart. A Range is any
group of cells that forms a row, column, or area.
¾ Series are various sets of data. You can modify the number of series and the data
sources for each series using the Chart Wizard.
¾ There are four basic steps in the Chart Wizard:
Step 1: Select the Chart Type and options.
Step 2: Verify or change the Data Range and define all Series.
Step 3: Determine the Chart Options.
Step 4: Choose the location of the chart.
¾ A chart can be modified after it is created by right-clicking on the chart or different
parts of the chart. You can change basic settings as well as formatting.
¾ The Data Range and Series can be modified to further manipulate the presentation
of data.
¾ A dynamic chart can be created using the OFFSET and COUNT functions to
create dynamic ranges used as Series in the Source Data.
5.6 Exercises
5.6.1 Review Questions
1. Refer to the spreadsheet created in Chapter 4, Problem 8.. Using the Chart Wizard,
create a line chart that plots velocity and average acceleration versus time. Give
each series an appropriate label and place the chart legend at the bottom of the
table. Title the chart “Velocity and Acceleration of a Moving Object” and label the x-
axis “Time (s).”
2. Using the set S defined in Chapter 4, Problem 2, create the following series plots of
the values in S:
• A scatter plot.
• A line plot with nodes.
• A line plot without nodes.
3. Create a table of x- and y-values for the equation y = ex using the following values for
x: 0, 1, 2, 4, 6, 8, 10, 12, 14 and 16. Create a scatter chart of the data with the points
connected by a smooth line. Locate the chart on the spreadsheet.
a. Describe the curvature of the line. Is it convex up or convex down?
b. Change the y-axis to a logarithmic scale. Is the line concave up or
concave down?
Worker Average Hours Average Units Output Average Units Not Meeting
Name Worked Per Week Per Week Specifications Per Week
Steve 40 100 5
Dan 44 120 10
Bob 36 95 2
Tom 41 105 6
Michael 41 100 4
Luther 45 115 9
Robby 41 105 5
Greg 46 125 7
Adam 35 90 1
Kenneth 39 100 4
6. Using your solution to Chapter 4, Problem 3, plot the heights of a cylinder versus the
radii for the given volume. To best depict the data, display the results in the form of
cylinders of various heights. Label the x-axis :Radius (ft)” and the series “Height.”
Title the chart “Height of a Cylinder with Volume = 10 ft^3.”
For this application, tensile strength is the most important property, followed by
fracture toughness and then thermal conductivity. Use Excel to create a chart that
illustrates this data. Select the most appropriate chart type and justify your selection.
Your chart should assist the engineer in making a final material selection.
8. The table below displays the high, low, and closing prices for stock XYZ on the 15th
of each month in the year 2002. Create a stock chart to depict this information.
Display the date on the x-axis and the price on the y-axis. Title the chart “Stock
Prices for XYZ in 2002.” Do not show a legend.
9. Given that Series 1 equals the set {56, 49, 52, 45, 40, 55, 58} and Series 2 equals
the set {23, 24, 32, 26, 26, 19, 29}, create the following 3-dimensional charts to plot
the two series:
• A surface chart
• A 3-dimensional line chart
• A stacked 3-dimensional area chart
10. In the above problem, which chart was best suited for the data? What scaling
limitations, if any, did you find for different chart types? What happens if you plot by
rows instead of columns?
11. Now suppose that the two series given in Problem 9 represent the high and low
temperatures for each day of a week in February. Create a 3-dimensional chart to
display these temperatures. Select the chart type you think is most appropriate to
depict this type of information. Include a data table of the high and low temperatures
with the chart.
Direct Indirect
Work Work Travel Breaks Other
Monday 0.35 0.25 0.1 0.2 0.1
Tuesday 0.42 0.23 0.15 0.15 0.05
Wednesday 0.4 0.17 0.2 0.21 0.02
Thursday 0.32 0.15 0.14 0.27 0.12
Friday 0.27 0.13 0.21 0.31 0.08
Create a separate pie chart for each day of the week that depicts the percent of time
workers spent doing each activity. Also, create a pie chart of the overall percent of
time spent doing each activity for the entire week. On each chart, display data labels
of the percentages represented by each section of the pie.
13. From the chart resulting from the above problem, answer the following questions:
a. On which day was “Travel” the highest?
b. On Thursday, in which category was the most time spent?
c. When did “Direct Work” time exceed “Indirect Work” time?
14. The industrial engineering team in Problem 12 now wants to compare the types of
activities done on each day of the week on a single graph. Use a doughnut graph to
plot the activity distributions for each day of the week on a single graph. Determine
which ring of the doughnut represents which day of the week.
15. The table below compares five different cities in terms of the overall population,
population per square mile, and total land area occupied by the city. Create a bubble
chart of this data that displays the total population on the x-axis, the population per
square mile on the y-axis, and the area of the city as the size of the bubble. Title this
chart “Population and Size Comparisons of Five Cities” and label the x- and y-axes
appropriately.
16. Plot the series of future cash flows from Chapter 4, Problem 4 as a column chart. Do
not show labels on the x-axis. Title the series ‘Cash Flow Amount” and show the
legend on the left of the chart. Label the amount of each cash flow (with currency
symbols) by the corresponding bar. Finally, title the chart “Cash Flow Summary.”
17. The manager of a production facility closely monitors the energy consumption of the
facility. As the number of units produced in the facility increases, the amount of
energy consumed also increases. The manager uses the forecasted demand for
upcoming months to predict the number of units that will be produced and the
resulting energy costs that should be included in the budget. The table below
displays her predictions for demand and energy consumption:
Number of Units
Month Demanded Predicted Energy Consumption (kWh)
January 3000 1700
February 5500 3275
March 5800 3450
April 7265 5100
May 8850 5950
June 6540 4200
18. A company is comparing the expected sales figures of three different products it
produces to the actual sales of those products. The table below displays this
information:
Expected Actual
Product A 45000 33500
Product B 78000 83000
Product C 65500 92000
Plot this information as a line chart with nodes titled “Product Sales: Expected vs.
Actual.” Depict each product as a separate series. The x-axis should represent the
type of sales (expected or actual) and the y-axis should represent the amount of
sales. Label the y-axis “Sales ($).” For each series, label the nodes with the
appropriate values. Display a legend to the right of the chart.
19. In the table below, the average stock returns for a portfolio are stored for various
quarters.
Create a chart for this data and answer the following questions:
a. At what time was the portfolio value highest?
b. At what time was the portfolio value lowest?
c. What period of time (year and quarter) had the best average portfolio
value?
d. Does there seem to be a trend in this data? That is, can you guess what
the next quarter’s value will be?
e. Do all chart types work well with this data? Can you identify a single chart
type that is best suited for this data?
Management is trying to track a trend in sales so that each month's inventory can
be better forecasted. Create a dynamic chart for this data and enter the following
new data points:
Month Sales
9 $4000
10 $4700
11 $5800
22. Consider the hands-on exercise 3.10. Cerate a bubble chart using the data
given in this exercise. Display Time on the x-axis, Distance on the y-axis and let
Velocity determine the size of the bubble. Give a title to this chart and label x and
y axis.
23. Consider the hands-on exercise 3.21. Cerate a line chart to graph the interest
payments and amount paid in the course of five years.
a. Do you see any trend in the amount of interest paid in each payment?
b. Do you see any trend on the principal amount paid in each payment?
c. Title the chart and label the x-axis.
d. Place the legend in the top of the chart.
e. Name the series of data.
24. Consider the hands-on exercise 3.21. Cerate a doughnut chart to present the
interest payments and amount paid in the course of five years. Each ring of the
doughnut should represent a payment. Name each data series. Determine which
ring of the doughnut presents a particular payment. Why do you think this chart
would be useful?
25. An athlete is planning to buy his daily vitamins. He is concerned about taking the
right amount of vitamins C, A, E and B1. In the drug store he found 4 different
brand names (brand name X, Y, Z and W). Brand X contains 50% of the daily
value (DV) for Vitamin C, 25% of DV for Vitamin A, 15% of DV for Vitamin E, and
20% of DV for Vitamin B1. Brand Y contains 30% of DV for Vitamin C, 20% of DV
for Vitamin A, 10% of DV for Vitamin E, and 5% of DV for Vitamin B1. Brand Z
contains 45% of DV for Vitamin C, 25% of DV for Vitamin A, 25% of DV for
Vitamin E, and 15% of DV for Vitamin B1. Brand W contains 40% of DV for
Vitamin C, 10% of DV for Vitamin A, 30% of DV for Vitamin E, and 10% of DV for
Vitamin B1. Create a radar chart to identify the brand with the highest vitamin
content.
Pivot Tables
Chapter Overview
6.1 Introduction
6.2 Pivot Tables
6.2.1 Terminology
6.2.2 Creating a Pivot Table
6.3 Further Modifications
6.3.1 Pivot Table Toolbar and Options
6.3.2 Grouping
6.3.3 Calculated Fields and Items
6.3.4 GETPIVOTDATA Function
6.4 Pivot Charts
6.5 Summary
6.6 Exercises
Chapter 6: Pivot Tables 2
6.1 Introduction
This chapter instructs the reader on how to create pivot tables and pivot charts. We have
found that even experienced Excel users are not familiar with the benefits of using these
tools. Pivot tables and pivot charts are a great tool for organizing large amounts of data
in a format which is clearer for the user to understand. They can be useful tools for
displaying large amounts of output to a user in a DSS application. For example, in Part
III of this book, we describe a Supply Chain Management DSS in which we use several
pivot tables and pivot charts to present the output of the application to the user. We will
also discuss how to create pivot tables and pivot charts using VBA in Chapter 21.
The table in Figure 6.1 contains the “Costs” for varying “Maximum Weights” and the
number of “Days to Arrive” for two different “Shipping Companies.” Suppose that an
employee is assigned the task of comparing the performance of these shipping
companies and presenting the results to his manager. The employee wants to
summarize this data so that a clear comparison can be made between the “Cost” values
of each company. We can observe in this table that there are three common values of
“Max Weight” (5, 20 and 20+) and four common values for “Days to Arrive” (1, 2, 4 and
8); therefore, the “Cost” values can be compared for each of these common “Max
Weight” and “Days to Arrive” values. That is, in reorganizing the table by these common
values, the manager can see what the “Costs” of the varying companies are for a given
combination of “Max Weight” and “Days to Arrive.”
Figure 6.1 The above data can be used in a comparative experiment, but would be
better organized with a pivot table.
Figure 6.2 displays the final reorganized table, a pivot table. Observe that the common
values for “Max Weight” are only shown once and the “Days to Arrive” values are only
repeated for each of the “Max Weight” values. This arrangement allows for a higher level
of organization than the repetitive nature of the original table. The costs of the two
companies are shown comparatively for each combination of “Max Weight” and “Days to
Arrive.” The “Minimum Cost” for each row of data is also revealed. So, the minimum
cost of the two companies for each combination of “Max Weight” and “Days to Arrive”
can be found in the “Minimum Cost” column.
Figure 6.2 The final pivot table reorganizes the data so that costs can be easily
compared.
6.1.1 Terminology
In this table, “Max Weight” and “Days to Arrive” are referred to as Row Fields; each
value in the fields from Figure 6.1 is shown as a row in the pivot table (Figure 6.2).
“Shipping Companies” is a Column Field, since its values serve as column headings in
the pivot table. The “Cost” values that appear in cells C6:E21 compose the Data Field
that is used to create the pivot table. The Page Field, a larger category that can group
all of the data in the table, would be seen at the top of the worksheet. While we have not
selected a page field for this table, an example like “Shipping Regions” would work well.
The “Minimum Cost” column is an example of the Grand Totals feature of pivot tables,
which we will discuss in more detail in a later section. Since the minimum costs are
computed for each row, this is a Row Grand Total; there are also Column Grand
Totals. The title of the table, “Min of Costs,” indicates that any Grand Totals would
calculate minimums of the data in the Data Field. The Field Settings of the Data Field
determine these data relations. Field Settings can also be applied to Row or Column
Fields to create Subtotals; we will also discuss Subtotals in more detail in a later
section.
There is a drop-down arrow next to each field on the pivot table that gives a list of all
values, called Items, in the corresponding field. For example, if we click on the drop-
down arrow next to “Max Weight,” we see a list with the values 5, 20, and 20+ (see
Figure 6.3a). Using this drop-down list, we can select or deselect the values that we
want to display in the pivot table. If we deselect 20+ from the drop-down list of “Max
Weight” and press OK at the bottom of the list, the pivot table no longer displays any
data associated with a “Max Weight” of 20+ (see Figure 6.3b). This is a great tool for
filtering or searching for particular data.
(a)
(b)
Figure 6.3 The fields have drop-down lists of their corresponding values that can be
selected or deselected to change the pivot table. (a) Deselecting the value
20+ from the “Max Weight” field. (b) The updated pivot table no longer
displays data for the 20+ value of “Max Weight.”
Now we will explain how to create a pivot table. First, we select Data > Pivot Table and
Pivot Chart Report from the menu. The window in Figure 6.4 thenappears. We need to
follow three simple steps in order to create a pivot table. These are: specify the data
location, specify the data, and create the table.
The second question in this first step offers two options: Pivot Table or Pivot Chart(with
Pivot Table). The second option, Pivot Chart, creates a chart with filtering tools similar to
those found in pivot tables. We will discuss pivot charts in more detail in a later section.
For now, we will choose Microsoft Excel list or database and Pivot Table so that we can
recreate the pivot table from the above example. We click Next to go to Step 2.
Figure 6.4 Step 1: Specifying the location of the data and the type of report to create.
‘Data-Shipping’!$B$3$E$27
Figure 6.5 Step 2: Selecting the actual data by highlighting a range of cells or
choosing a file with the Browse option.
The third and most important step, shown in Figure 6.6, has three sub-steps. These
steps allow us to specify exactly how we want to reorganize our data in the new table.
The first sub-step, in the middle of the screen in Figure 6.6, designates the location of
the new pivot table. This option allows us to place the pivot table in a new worksheet or
in a particular area of the current worksheet. For this example, we have chosen to put
our pivot table in a new worksheet.
Figure 6.6 Step 3: Specifying the location of the pivot table and working with Layout
and Options features.
The next sub-step, activated by pressing the Layout button, allows us to select our
layout. The Layout sub-step, detailed in Figure 6.7(a), allows us to organize our data by
determining which column heading from the original data we want to become a Row,
Column, Data, or Page Field. We select one of our field names from the right-hand side
of the window and drag it to one of the layout areas corresponding to the different field
types. Because we want “Max Weight” to be a Row Field, we click and drag it to the Row
layout area. We click on “Days to Arrive” and drag it to the same layout area since we
also want it to be a Row Field. We then click on “Shipping Companies” and drag it to the
Column layout area so that it becomes a Column Field. Lastly, we click and drag “Costs”
to the Data area since this is the main data that we want to summarize and it will serve
as the Data Field of the pivot table (see Figure 6.7b).
(a)
(b)
Figure 6.7 Layout sub-step: Click and dragging field names to the layout area shown
in the center of the window, which will determine which fields become Row Fields,
Column Fields, and the Data Area. It is also possible to specify a Page Field.
To return to the window shown in Figure 6.6, we press OK. Note that we do not have to
specify the layout during the Layout sub-step. In fact, we could skip this sub-step
completely and then use the Pivot Table Fields List, a window that lists all the fields in
the pivot table, to create fields in the spreadsheet. We will provide an example of this
technique in a later section. The third sub-step, Options, which we explore in a later
section, allows us to manipulate various features of the pivot table. Now, we click the
Finish button to display the completed pivot table, presented in Figure 6.8.
Figure 6.8 The final pivot table after specifying the source data and layout.
Now that we have created the pivot table, we can use the drop-down buttons to search
for specific data. We will discuss further data searching and manipulation in the next
section.
Figure 6.9 This table records “Units Produced” for various combinations of
operators, machines, and shifts.
In Step 3, we determine a layout for the pivot table. This time, however, instead of using
the Layout option in the Wizard, we use the Pivot Table Fields list to set the pivot table
fields in the spreadsheet (see Figure 6.10). We set “Number of Operators” and “Machine
Number” as Row Fields and “Shift” as a Column Field. We then set “Units Produced” as
our Data Field, since we are most interested in this value. Figure 6.11 reveals the
completed pivot table.
Figure 6.10 Setting the Row Fields, Column Fields, and Data Field in the spreadsheet
using the Pivot Table Field List.
The pivot table analyzes the set of data included in the Data Field and the Field Setting
for the table manipulates the presentation of this data. In the table created in the above
example, the default Field Setting applied to the “Units Produced” data field is Sum,
which can be noted by the “Sum of Units Produced” title automatically given to the table
(see Figure 6.11). Any Grand Totals or Sub Totals calculated would show sums of the
“Units Produced” values in relative row fields or column fields. We can use the Options
setting of the pivot table to show or hide these Grand Totals.
For example, in Figure 6.12, the Grand Totals option has been included for both rows
and columns. In this figure, a new column to the right of the table features the row sums
of the “Units Produced” while a new row below the table contains the column sums. That
is, we can observe the total amount of “Units Produced” for each combination of
“Number of Operators,” “Machine Number,” and “Shift” in the “Grand Total” column on
the right, and the total number of “Units Produced” during each “Shift” in the “Grand
Total” row below the table.
Figure 6.12 Row and column Grand Totals show the sums of “Units Produced.”
There are other Field Setting options aside from Sum (as we saw in the first example
with shipping data when showing “Minimum Costs”). To look at these, we double-click on
the “Sum of Units Produced” title, or we can select a cell in the Data Field and use the
Pivot Table Toolbar, which we will discuss later. The dialog box shown in Figure 6.13
then appears.
Figure 6.13 Field Settings for the “Units Produced” Data Field.
We can select any of the preset calculations in the Summarize by window. For example,
we can choose Max to better analyze the combination of operators, machines, and shifts
that produces the most units (see Figure 6.14). With Max, we can clearly see the
combination of “Number of Operators” and “Machine Number” that produces the most
units during any “Shift.” Any Grand Totals or Subtotals now display the maximum values
of “Units Produced.” The Name value should automatically update depending on the
summary chosen; this name can also be changed manually. Clicking on the Number
formatting button displays the same dialog box used to format cells. We can use this
option to make values appear as currency, dates, etc.
Figure 6.14 The Field Setting on the “Units Produced” Data Field has now been
changed to Max. All Grand Totals have been updated.
We can use the Options button to modify the data further after the main summarizing
has been completed. For example, we can choose to show the data as a percentage of
a given set of field values (see Figure 6.15).
Figure 6.15 The Options button allows a user to further modify data from a selected
field.
Subtotals are Field Settings applied to Row or Column Fields. Because totals for Column
Fields can be calculated using Column Grand Totals, we will only focus on Subtotals of
Row Fields. For example, to perceive the total number of “Units Produced” for each
“Number of Operators” or for each “Machine Number,” we add Subtotals to either of
these fields. To do so, we simply click in one of these fields and then select the Field
Settings option, either by right-clicking in the field or from the Pivot Table Toolbar, which
we will discuss later. The window in Figure 6.16 then appears. This window resembles
Figure 6.13, which allows users to modify Field Settings for Data Fields. Again, we can
choose from the list of preset summarizing, or subtotaling, options in the central window.
Figure 6.16 Using Field Settings for Row Fields to create Subtotals.
In this PivotTable Field dialog box, we have selected the Max subtotal option to add
Subtotals for the “Number of Operators” Row Field. The Pivot Table then summarizes
the maximum number of “Units Produced” for each “Number of Operators” value in
separate rows (see Figure 6.17). This option also adds Max Subtotals for the “Machine
Number” Row Field, which are summarized at the bottom of the table. Calculating these
Subtotals allows us to easily find which shift had the most units produced for any number
of operators or on any machine.
Figure 6.17 Adding Subtotals for the “Number of Operators” and “Machine Number”
Row Fields to the pivot table.
With the Pivot Table Toolbar, we can easily modify the pivot table after it has been
created. This toolbar features icons (see Figure 6.18) as well as a drop-down list of
options (see Figure 6.19). We can also access several of these options by right-clicking
in any field of the pivot table. Note that we must select a cell in the pivot table in order to
activate the Pivot Table Toolbar.
Figure 6.19 The list of drop-down options available from the Pivot Table Toolbar.
We will discuss eleven of the main Toolbar Options, giving a brief description of each in
the following order: Format Report, Pivot Chart, Hide/Show Details, Refresh, Field
Settings, Pivot Table Fields List, Wizard (and Layout), Grouping, Formulas (or
Calculated Fields and Items), and Table Options. The order of these options correlates
with their location on the toolbar.
Icons can be added to or removed from the toolbar at any time by using the “Add
Remove Buttons” arrow found on the right-end of the toolbar (see Figure 6.20).
The first icon on the pivot table toolbar is , Format Report, changes the format of a
pivot table. When we click this icon, we see the window shown in Figure 6.21. We can
scroll down the list to choose a report style. In Figure 6.22, the pivot table is
automatically adjusting to the new style.
Figure 6.21 The first icon on the pivot table toolbar, Format Report, allows a user to
select a report style for a table.
Figure 6.22 The pivot table reflects the updated report style.
The next item is Pivot Charts. We will actually discuss this icon in a later section, but, for
now, note that we can create a Pivot Chart directly from the Pivot Table with the toolbar
or by right-clicking on the table.
The next two icons on the Pivot Table Toolbar, Hide Detail and Show Detail, are simply
visual preferences that allow us to hide or view all related values for a selected item. We
can also double-click on any item in a field to hide or view related data.
The next icon is the Refresh option, , which updates the data shown in our pivot table
if we change our source data. For example, if we update the “Units Produced” data for 2
operators on all machines in Shift 1 in the original table, clicking the refresh icon reflects
this change in the pivot table (see Figure 6.23). In the figure, the data values are
updated and any Grand Totals or Subtotals are recalculated.
(a)
(b)
Figure 6.23 (a) The data in the original table is changed. (b) The refresh button
reflects these changes in the pivot table.
We have already described the next icon, which is used to modify Field Settings, in the
previous section.
The subsequent icon shows the Pivot Table Fields List, which creates the layout of the
pivot table if Step 3’s Layout sub-step of the Wizard is not used. We simply select a field
name from the list and drag it to the table. To remove a field from the table, we click on
the field title in the table and drag it outside of the table. It is also helpful to observe all
fields available from the original data if some fields are hidden in the pivot table. If we do
not wish to modify the layout using the Pivot Table Fields List, we can choose the
icon (or Wizard from the list of Pivot Table drop-down options) to return to Step 3 of the
Pivot Table Wizard. We choose the Layout button and again see the layout sub-step
window, enabling us to change our Row Fields. When we click OK in the layout window
and Finish in the Step 3 window, our pivot table is updated (see Figure 6.24).
Figure 6.24 The pivot table is updated to reflect the new layout. We have switched the
“Machine Number” and “Shift” fields.
The next two items to discuss are Grouping and Formulas, both of which can be found in
the Pivot Table drop-down list in the toolbar. We discuss Grouping in the following
section and Formulas, or Calculated Fields and Items, in the section after.
To work with pivot table options, we choose Table Options from the Pivot Table drop-
down list in the toolbar. The window in Figure 6.25 then appears. We can also access
the Options window by right-clicking on the pivot table, if it has already been created,
and choosing Table options from the drop-down list. Remember that this is also a sub-
step of Step 3 of the Wizard. This table provides a variety of options, beginning with the
option to enter a name for the pivot table in the text area next to Name at the top of the
window. Besides Name, this table contains two main categories: Format options and
Data options.
Under Format options, we can edit some of the details of the table. The first two options
allow us to display Grand Totals for our columns or rows. Another formatting option,
Page layout, features a drop-down list that allows us to alter our data display. In Figure
6.25, we have selected the default option of Down, then Over. Below that option, we can
specify how many fields should appear in a column; the default value is zero. Next, we
can choose to replace empty or erroneous cells with specific text or symbols. These
features are easy to experiment with.
Data options allow us to work with our data source. First, we can choose whether to
save the data in our table by checking Save data with table layout. Then we can select
or deselect Enable drilldown to view the contributing cells of our data. The drill down
option shows all the data created in a field. We can also double-click on a cell to drill
down. The Refresh on open option updates our pivot table if any changes have been
made in our data source. These options helps us to further organize the display of our
information.
Figure 6.25 Options Step: Select Format and Data options to fine-tune a final pivot
table’s appearance and features.
6.2.2 Grouping
Grouping items in a Row or Column Field allows us to further manipulate how we view
or search for data in a pivot table. To group field values, we select the field and choose
Group and Show Detail from the Pivot Table drop-down options; or, we can right-click on
the table. For example, suppose we want a more general analysis of how the “Number
of Operators” affects production. We can form two intervals of values for this field: 2 to 5
and 6 to 10. To do so, we first select a cell in the “Number of Operators” Row Field and
choose the Group and Show Detail and Group Data options (see Figure 6.26).
Figure 6.26 Applying grouping to the values of the “Number of Operators” Row Field.
The Grouping window in Figure 6.27 then appears. In this window, we specify the
intervals that we want to create. Here, we designate 2 and 10 as our starting and ending
values, and set an interval of 4 operators. The updated pivot table is shown in Figure
6.28.
Figure 6.28 The updated pivot table now shows the values for “Number of Operators”
as intervals.
We can ungroup any field by selecting Group and Show Detail and Ungroup Data.
To create a Calculated Field or Calculated Item, we click on Pivot Table > Formulas >
Calculated Field (or Calculated Item) in the toolbar drop-down options. The window in
Figure 6.29 then appears. Here, we can name and define a formula associated with the
creation of a new field or item. This formula is stored with the pivot table. Unlike Grand
Totals or Subtotals, we can enter any formula to be applied to the values in the selected
field, and a new field will then be created.
For example, say we want to determine the variable cost in every combination of
operators, machines, and shifts. The variable cost is defined in terms of the number of
units produced, our Data Field. So, we select the Data Field and choose Pivot Table >
Formulas > Calculated Field from the toolbar options. In the window shown in Figure
6.29, we create a name for the new field, “Variable Cost,” and enter the formula in terms
of the Data Field listed in the Fields list. We assume here that the cost per unit is $12.00.
A new data field called “Variable Cost” has been added to the pivot table in Figure 6.30.
Also note that there is now a drop-down arrow for “Data” at the top of the table; it allows
us to select a set of data fields to show. That is, we could now hide “Max of Units
Produced” and show “Variable Cost” only.
Calculated Items create a single row rather than an entire new column of data. First we
select a Row Field, and then we go to Pivot Table > Formulas > Calculated Item from
Pivot Table Toolbar.
Summary
The desired_field is the field that contains the value we are searching for. The
range_of_desired_data is the range in the pivot table that contains this field. The
remaining field and item values allow us to refine our search if desired. The field and
item values can be listed in any order, regardless of the pivot table layout.
For example, suppose we want to find the number of units produced during Shift 2 on
Machine 2 when 8 operators are working. We type the following in a cell on the
worksheet:
The value for this search is 59 units produced (see Figure 6.31). GETPIVOTDATA can
be a very useful function when pivot tables are involved in a large spreadsheet with
other applications. That is, this function can be used on a “Report” sheet that refers to
information searched from a pivot table. It can also be useful when a user interface is
created, as we will see in applying VBA.
Figure 6.31 The GETPIVOTDATA function can search for a particular field in a pivot
table based on other field value criteria.
We can see the different times for each layout in a bar above the corresponding job type
and run number. Note the field names “Number of Operators” and “Machine Number” at
the bottom of the chart. We can select these fields to vary the items displayed. Assume
we only want to view the “Units Produced” on Machine 3. We select the field “Machine
Number” and unclick the numbers 1 and 2 (see Figure 6.33). Any change in viewing field
items made to the chart is reflected in the pivot table, and likewise, any changes to the
viewing field items in the pivot table are reflected in the chart. These options are useful
when presenting or analyzing data.
Figure 6.32 Clicking the chart icon on the pivot table toolbar automatically creates a
chart. The data here shown is here relative to the layout options selected in creating the
pivot table.
Figure 6.33 The chart is updated to show the units produced on Machine 3 only.
If we do not like the Pivot Chart format created by Excel, we can modify the Chart Type
using the same techniques described in Chapter 5. That is, we can right-click on the
chart and select Chart Type from the list of options (see Figure 6.34). With this list of
drop-down options, we can also modify other chart details, such as formatting. To do so,
we have to make sure that we link the Source Data to the pivot table so we can transfer
the filtering capabilities to the pivot chart.
Figure 6.34 Changing the chart type from a Bar Graph to a Pie Chart. The data is still
filtered for Machine 3.
Pivot Chart A chart created from a pivot table or directly from the
source data using the same layout features and other
options as a pivot table.
Summary
6.4 Summary
¾ Pivot tables transform large amounts of data from a table or database into an
organized summary report.
¾ The three steps to create a pivot table are: Specify Location, Select Data, and
Create Table Layout with specified options.
¾ From categories in your original table, select Row and Column Fields to group
your data. You also choose a Data Area of comparative values. A Page Field can
be created to organize your entire table by different overall categories.
¾ Toolbar options include: Format Report, Pivot Chart, Hide/Show Details, Refresh,
Field Settings, Pivot Table Fields List, Wizard (and Layout), Grouping, Formulas
(or Calculated Fields and Items), and Table Options.
¾ Use Grouping to create intervals in field values.
¾ You can create Calculated Fields or Calculated Items to further analyze the data
in your table. Some common formulas already constructed by Excel include
SUM, MIN, and MAX. Grand Totals and Subtotals use the SUM formula to
display these calculations of your row or column data.
¾ GETPIVOTDATA searches for data in a pivot table using field value criteria.
¾ Pivot Charts use pivot tables as their Source Data so that filtering options are
transferred to the chart as well.
6.5 Exercises
6.5.1 Review Questions
1. The “Store Chain” example on the included CD displays the location, size category,
owner, and quarterly sales for each store owned by a particular store chain.
Organize this data into a pivot table. Show quarterly results using the city as the first
criteria, the size second, and the owner third. Do you notice any trends in this
method?
3. A student traveling home for the holidays is comparing flights in an effort to find a
cheap flight with few connections at a desirable time. The student wishes to travel
from the state of Florida to the city of Pittsburgh. There are numerous combinations
of flights available to travel this route. Using the data provided in the “Fly Home”
spreadsheet on the included CD, create a pivot table that will allow the student to
quickly pull up flights between various cities to determine which flight or combination
of flights is the most desirable. The table should also highlight the least expensive
flight departing each city. Format your table using an existing report style.
4. Refer to the soft drink bottling plant introduced in Chapter 4, Problem 6. In an effort
to reduce the number of nonconforming bottles, the plant manager has decided to
reallocate the products it offers to the bottling lines that package those products.
Before doing this, the manager must be able to compare the current costs of bottling
each product. Using the data provided in the “Soft Drink” spreadsheet on the
included CD, create a pivot table that will enable the manager to perform this cost
comparison based on the drink type, number of fluid ounces, and material of each
bottle. Format your table using an existing report style.
5. Use your solution to the previous problem to create a pivot chart that compares the
costs of bottling each product. What are the characteristics of the most expensive
product to bottle?
6. The following table displays postal service rates for shipping packages. Packages
can be shipped via Express, First Class, Priority, or Standard mail. Express and First
Class mail may also be certified, which means that the sender can receive
verification upon delivery. The postal service charges one rate for the first ounce that
a package weighs and another rate for each additional ounce. Enter this table into an
Excel spreadsheet.
Rate After
Shipment Type Certification First Ounce Rate First
Express Yes $1.24 $0.89
Express No $1.52 $0.89
First Class Yes $0.83 $0.67
First Class No $1.52 $0.67
Priority Yes $0.65 $0.42
Standard Yes $0.37 $0.23
7. The owner of a campus textbook store has noticed that the store’s customers must
wait in particularly long lines during the first few weeks of classes. Concerned about
losing business to stores with speedier lines, the owner wants to analyze the length
of the store’s queues for comparison with queues at competing stores. To do so, she
measures the following values at five random times throughout the day, every day for
a week: the length of the queue, the number of cashiers attending customers, and
the number of idle cashiers. The “Queue” spreadsheet on the included CD contains a
table of her results. Create a pivot table of the information she collected that
averages the store’s queue lengths for different times of the day and week and for
different numbers of busy and idle cashiers. Apply formatting to complete your table.
8. Use the pivot table you created in the previous problem to determine which hour of
the day has the highest average queue lengths. Then create a pivot chart to display
the average queue lengths for that hour of the day on each different day of the week.
9. A do-it-yourself retail center sells home care products in various categories, including
cleaning, gardening, and hardware. The “Home Care” spreadsheet on the included
CD is used to forecast the demand of products in each of the product categories for
the next four months for each of the center’s regions. Create a pivot table that totals
the demand for each type and category of product for each region.
10. A swarm of locusts is infesting the Mid-Atlantic region, so the projected insecticide
demand has increased. The forecasted demand (in thousands of units) is now 85 for
January, 89 for February, 87 for March, and 82 for April. Record these changes in
the data table on the “Home Care” spreadsheet. Then refresh the data on the pivot
table to reflect these changes.
11. Sammy owns a lemonade stand and is doing some market research in an attempt to
better understand his neighborhood customers. For a random sample of customers,
he is given the income, gender, and number of days per week they pass by his
stand. Use this information to determine how gender and income influence the
frequency with which a customer buys his lemonade. Use the data given below to
create a pivot table.
13. Jessie is a manager at a company that specializes in at-home sales. She is given
transactions for various salespersons for various products sold for the month of
March. Use the data given below to create a pivot table that shows the number of
sales transactions for each salesperson.
14. Using the data from problem 13, create a pivot table that displays the total revenue
by product for each salesperson. Use the GETPIVOTDATA function to find Danny’s
vacuum sales.
15. Using the data from problem 13, create a pivot table that displays the total revenue
by salesperson for each week. (Hint: Use grouping.)
Statistical Analysis
with Excel
Chapter Overview
7.1 Introduction
7.2 Understanding Data
7.2.1 Descriptive Statistics
7.2.2 Histograms
7.3 Relationships in Data
7.3.1 Trend Curves
7.3.2 Regression
7.4 Distributions
7.5 Summary
7.6 Exercises
Chapter 7: Statistical Analysis with Excel 2
7.1 Introduction
This chapter illustrates the tools available in Excel for performing statistical analysis.
These tools include new functions, the data analysis toolpack, and some new chart
features. This chapter is not intended to teach the statistical concepts which can be used
in Excel’s analysis, but rather demonstrate to the reader that several tools are available
in Excel to perform these statistical functions. Statistical analysis is used often in DSS
applications for analyzing input and displaying conclusive output. These tools will be
especially used in applications involving simulation. We discuss the application of
statistical analysis in simulation in Chapter 9 and again in Chapter 20 with VBA. We
have several DSS applications which use statistical analysis tools, such as Queuing
Simulation.
One of the basic statistical calculations to perform is finding the mean of a set of
numbers; the mean is simply the average, which we learned how to calculate with the
AVERAGE function in Chapter 4:
=AVERAGE(range or range_name)
Figure 7.1 displays a table of family incomes for a given year. We first name this range
of data, cells B4:B31, as “FamIncome.” We can now find the average, or mean, family
income for that year using the AVERAGE function as follows (see Figure 7.2):
=AVERAGE(FamIncome)
Figure 7.2 Calculating the mean, or average, of all family incomes using the
AVERAGE function.
Similar to the mean, the median can also be considered the “middle” value of a set of
numbers. The median is the middle number in a list of sorted data. To find the median,
we use the MEDIAN function, which takes a range of data as its parameter:
=MEDIAN(range or range_name)
To determine the median of the above family incomes, we enter the MEDIAN function as
follows:
=MEDIAN(FamIncome)
We can check whether or not this function has returned the correct result by sorting the
data and finding the middle number. Since there are an even number of family incomes
recorded in the table, we must average the two middle numbers. The result is the same
(see Figure 7.3).
Figure 7.3 Using the MEDIAN functionand verifying the result by sorting the data
and finding the middle value.
Another important value, standard deviation, is the square root of the variance, which
measures the difference between the mean of the data set and the individual values.
Finding the standard deviation is simple with the STDEV function. The parameter for this
function is also just the range of data for which we are calculating the standard deviation:
=STDEV(range or range_name)
In Figure 7.4, we calculate the standard deviation of the family income data using the
following function:
=STDEV(FamIncome)
Statistical Functions
AVERAGE Finds the mean of a set of data.
MEDIAN Finds the median of a set of data.
Summary
STDEV Finds the standard deviation of a set of data.
Figure 7.5 The Data Analysis dialog box provides a list of analytical tools.
(Note: Before using the Analysis Toolpack, you must ensure that it is an active Add-in.
To do so, choose Tools > Add-ins from the Excel menu and select Analysis Toolpack
from the list. If you do not see it on the list, you may need to re-install Excel on your
computer. After you have checked Analysis Toolpack on the Add-ins list, you should find
the Data Analysis option under the Tools menu option.)
The Descriptive Statistics option provides a list of statistical information about our data
set, including the mean, median, standard deviation, and variance. To use Descriptive
Statistics, we go to Tools > Data Analysis > Descriptive Statistics. Choosing the
Descriptive Statistics option from the Data Analysis window (shown in Figure 7.5)
displays a new window (shown in Figure 7.6).
Figure 7.6 The Descriptive Statistics dialog box appears after it is chosen from the
Data Analysis list.
The Input Range refers to the location of the data set. We can check whether our data is
Grouped By Columns or Rows. If there are labels in the first row of each column of data,
then we check the Labels in First Row box. The Output Range refers to where we want
the results of the analysis to be displayed in the current worksheet. We could also place
the analysis output in a new worksheet or a new workbook. The Summary Statistics box
calculates the most commonly used statistics from our data. We will discuss the last
three options, Confidence Level for Mean, Kth Largest, and Kth Smallest, later in the
chapter.
Let’s now consider an example in order to appreciate the benefit of this tool. In Figure
7.7 below, there is a table containing quarterly stock returns for three different
companies. We want to determine the average stock return, the variability of stock
returns, and which quarters had the highest and lowest stock returns for each company.
This information could be very useful for selecting a company in which to invest.
We use the Descriptive Statistics tool to answer these questions. In the Descriptive
Statistics dialog box (see Figure 7.8) we enter the range B3:D27 for the Input Range.
(Notice that we do not select the first column, Date, since we are not interested in a
statistical analysis of these values.) Next, we check that our data is Grouped By
Columns; since we do have labels in the first row of each column of data, we check the
Labels in First Row box. We now specify G3 as the location of the output in the Output
Range option. After checking Summary Statistics, we press OK (without checking any of
the last three options) to observe the results shown below in Figure 7.9.
Figure 7.8 Filling the Descriptive Statistics dialog box for the above example data.
Figure 7.9 The results of the Descriptive Statistics analysis for the example data.
First, let’s become familiar with the Mean, Median, and Mode. As already mentioned, the
Mean is simply the average of all values in a data set, or all observations in a sample.
We have already observed that without the Analysis Toolpack, the mean value can be
found with the AVERAGE function in Excel. The Median is the “middle” observation
when the data is sorted in ascending order. If there is an odd number of values, then the
median is truly the middle value. If there is an even number of values, then it is the
average of the two middle values.
The Mode is the most frequently occurring value. If there is no repeated value in the data
set, then there is no Mode value, as in this example. The Mean is usually considered the
best measure of the central data value if the data is fairly symmetric; otherwise the
Median is more appropriate. In this example, we can observe that the Mean and Median
values for each company differ slightly; however, we use the Mean value to compare the
average stock returns for this company. This analysis alone implies that GE and INTEL
have higher stock returns, on average, than MSFT. But these values are still very close,
so we need more information to make a better comparative analysis.
Now, let’s consider the Standard Error, Standard Deviation, and Sample Variance. All of
these values measure the spread of the data from the mean. The Sample Variance is
the average squared distance from the mean to each data point. The Standard Deviation
is the square root of the Sample Variance and is more frequently used. Looking at these
values for the example data, we can observe that INTEL has a highly varied stock
return, while GE’s is more stable. Therefore, even though they have the same Mean
value, this difference in the Standard Deviation makes GE a more favorable stock in
which to invest.
We will discuss Standard Error, which is used in connection with trends and trendlines,
in more detail later.
For example, if we create a chart of the GE data, we can observe that the second data
value is an outlier since it is ±2s = ±2*0.05 = ±0.1 from the mean (0.02); in other words,
any value above 0.12 or below -0.08 is an outlier. The second data value for GE is +0.19
(see Figure 7.10). This figure may imply that something significant happened to GE as a
company during Q2 1995, that something affected the national economy, or that they
faced any number of (un)predictable situations.. However, since the second data value
is the only outlier in the last five years of quarterly data for GE, it seems that the mean
and standard deviation are accurate measures of the behavior of GE stock returns.
Figure 7.10 The second data point is an outlier since it is greater than 2s from the
mean.
We can identify outliers by looking at a chart of data, or we can actually locate values in
the data set that are greater than +2s and smaller than -2s. To do so, we can place the
following formula in an adjacent column to the data:
This formula states that if the absolute value of the difference between the data value
and the mean is greater than 2s, then the word “outlier” will appear in the cell. We
reference the mean and standard deviation values from the results of the Descriptive
Statistics analysis. We can now easily identify outliers by looking for the word “outlier” in
the adjacent column. Using just the column of GE data and this formula, we can observe
that we have identified the same outlier point for GE (see Figure 7.11). (Another formula
could have been used with the IF and OR functions as well.)
Figure 7.11 Identifying the outlier by using a formula with the IF and ABS functions.
Another way to discover outliers is by using Conditional Formatting with the Formula Is
option. With the formula below, we can simply select the column of values in our data set
and fill in the Conditional Formatting dialog box to highlight outlier points:
Again, concerning the GE data, we can apply Conditional Formatting to identify the
outliers as cells highlighted in red. In Figure 7.12, we demonstrate how we applied the
Formula Is option.
In Figure 7.13, we can observe that the same outlier point has been formatted.
Let’s now return to the Descriptive Statistics results to understand the remaining analysis
values. Kurtosis is a measure of peakedness in the data. It compares the data peak to
that of a Normal curve (which we will discuss in more detail in a later section). The
Skewness is a measure of how symmetric or asymmetric data is. A Skewness value
greater than +1 is the degree to which the data is skewed in the positive direction;
likewise, a value less than -1 is the degree to which the data is skewed in the negative
direction. A Skewness value between -1 and +1 implies symmetry. The Skewness
values for MSFT and INTEL imply that their data is fairly symmetric; however, the
Skewness value for GE is 1.69, which implies that it is skewed positively. That is, there
is a peak early on in the data and then the data is stable (as we saw in the GE data
graph in Figure 7.10).
The Range is the difference between the minimum and maximum value in the data set.
The smaller this value is, the less variable is the data and therefore, the more desirable.
The Minimum, Maximum, and Sum values are self-explanatory. The Count number
reveals the quantity of values in the data set.
The last three options in the Descriptive Statistics dialog box, Confidence Level for
Mean, Kth Largest, and Kth Smallest, can provide some extra information about our
data. The Confidence Level for Mean calculates the mean value in the Descriptive
Statistics report constrained to a specified confidence level. The mean is calculated
using the specified confidence level (for example, 95% or 99%), the standard deviation,
and the size of the sample data. The confidence level and the calculated mean are then
added to the analysis report; we can compare the actual mean to this calculated mean
based on the specified confidence level.
The Kth Largest and Kth Smallest options provide the respectively ranked data value for
a specified value of k. For example, for k = 1, the Kth Largest returns the maximum data
value and the Kth Smallest returns the minimum data value. The value of k can range
from 1 to the number of data points in the input.
For example, let’s create the same Descriptive Statistics report, but this time check the
last three options of the initial dialog box (see Figure 7.14). We have specified a
confidence interval of 95%, and we want to know the 2nd largest value and 2nd smallest
value.
Figure 7.14 This time, specifying the last three options in the Descriptive Statistics
dialog box.
In the new results, there are three additional rows of data (see Figure 7.15). Notice that
there is a slight difference in the mean calculation with the enforced confidence level.
Figure 7.15 The results of the Descriptive Statistics analysis with three additional
rows.
Similar to the Kth Largest and Kth Smallest options with Descriptive Statistics, the two
Excel functions PERCENTILE and PERCENTRANK are valuable when working with
ranking numbers. The PERCENTILE function returns a value for which a desired
percentile k of the specified data_set falls below. The format of this function is:
=PERCENTILE(data_set, k)
For example, let’s apply this formula to the MSFT data. If we want to determine what
value 95 percent of the data falls below, we type the function:
=PERCENTILE(B4:B27,0.95)
The result is 0.108, which means that 95 percent of the MSFT data is less than 0.108.
The PERCENTRANK function performs the complementary task; it returns the percentile
of the data_set that falls below a given value. The format of this function is:
=PERCENTRANK(data_set, value)
For example, if we want to know what percent of the MSFT data falls below the value
0.108, we type:
=PERCENTRANK(B4:B27, 0.108)
The result is then 0.95, or 95 percent. This function proves beneficial when we want to
discover what percent of the data falls below the mean. Using the MSFT data set again,
we type:
=PERCENTRANK(B4:B27, 0.01)
The result is that 0.388, or about 39 percent of the data, is less than the mean. These
Excel functions, along with the others mentioned above, when combined with the
Descriptive Statistics analysis tool, can help determine much constructive information
about data.
Descriptive Statistics
Any values in the data set that lie more than ±2s from the
Outliers
mean.
Summary A function that returns a value for which a desired
PERCENTILE
percentile k of the specified data_set falls below.
A function that returns the percentile of the data_set that
PERCENTRANK
falls below a given value.
7.2.2 Histograms
The Input Range is the range of the data set. The Bin Range specifies the location of the
bin values. Bins are the intervals into which values can fall; they can be defined by the
user or can be evenly distributed among the data by Excel. If we specify our own bins, or
intervals, then we must place them in a column on our worksheet. The bin values are
specified by their upper bounds; for example, the intervals (0-10), (10-15), and (15-20)
are written as 10, 15, and 20. The Output Range is the location of the output, or the
frequency calculations, for each bin. This location can be in the current worksheet or in a
new worksheet or a new workbook. The chart options include a simple Chart Output (the
actual histogram), a Cumulative Percentage for each bin value, and a Pareto
organization of the chart. (Pareto sorts the columns from largest to smallest.)
Let’s look at the MSFT stock return data from the examples above. We may want to
determine how often the stock returns are at various levels. To do so, we go to Tools >
Data Analysis > Histogram and specify the parameters of the Histogram dialog box (see
Figure 7.17). Our Input Range is the column of MSFT data, including the “MSFT” label in
the first row. For now, we leave the Bin Range blank and let Excel create the bins, or
intervals. We check Labels since we have included a label for our selected data. We pick
a cell in the current worksheet as our Output Range and then select Chart Output. The
resulting histogram and frequency values are shown in Figure 7.18.
Figure 7.18 The resulting histogram and frequencies for the example data.
First, let’s discuss the Bin values. Remember that each bin value is an upper bound on
an interval; that is, the intervals that Excel has created for this example are (below -
0.16), (-0.16, -0.08), (-0.08, -0.01), (-0.01, 0.07), and (above 0.07). We can deduce that
most of our data values fall in the last three intervals. It may have been more useful to
use intervals relative to the mean and standard deviation of the MSFT data. In other
words, we could create the intervals (below -2s), (-2s, -s), (-s, mean), (mean, s), (s, 2s),
and (above 2s). To enforce these intervals, we create our own Bin Range. In a new
column, we list the upper bounds of these intervals using the mean and standard
deviation values from the Descriptive Statistics results for the MSFT data. We also
create a title for this column to include in the Bin Range (see Figure 7.19).
Figure 7.19 Creating the Bin Range for the example data.
We now choose Tools > Data Analysis > Histogram from the menu again and this time
add the Bin Range (see Figure 7.20).
Figure 7.20 The Histogram dialog box now has a specified Bin Range.
Our Bin Range now calculates the frequencies and creates the histogram (see Figure
7.21). We can analyze this data to determine that the majority of our data lies above the
mean (15 points above the mean verses 9 points below the mean). This conclusion
validates the result of the PERCENTRANK function, as discussed in the previous
section where we learned that 39 percent of the data values are below the mean;
therefore 61 percent, or the majority, of our data is above the mean. We can also
observe from this histogram result that there is one outlier; in other words, there is one
data point that falls below -2s. We will perform some more analysis with these histogram
results later in the chapter.
Figure 7.21 The resulting histogram uses the specified Bin Range.
A histogram can also be formatted. As with any chart, we right-click on the histogram
and change the Chart Options or other parameters. For example, we have removed the
Legend from the histograms shown above. If desired, we can also modify the font of the
axis labels by right-clicking on the axis and choosing Format Axis.
We can also remove the gaps between the bars in the histogram to better recognize
possible common distributions of the data. To remove these gaps, we right-click on a bar
in the graph and select Format Data Series from the list of drop-down options. Then, we
select Options and set the Gap Width to 0 (see Figure 7.22).
The histogram results can now be easily outlined to identify common distributions or
other analyses (see Figure 7.23). We will discuss distributions later, but for now, let’s
define some common histogram shapes.
Figure 7.22 Removing the gaps by right-clicking on the bars, choosing Format Data
Series, and setting the Gap Width to 0.
The histogram’s four basic shapes are symmetric, positively skewed, negatively
skewed, and multiple peaks. A histogram is symmetric if it has only one peak; that is, if
there is a central high part and almost equal lower parts to the left and right of the peak.
For example, test scores are commonly symmetric; they are sometimes referred to as a
bell curve because of their symmetric shape.
A skewed histogram also only has one peak; however, the peak is not central, but far to
the right with many lower points on the left, or far to the left with many lower points on
the right. A positively skewed histogram has a peak on the left and many lower points
(stretching) to the right. A negatively skewed histogram has a peak on the right and
many lower points (stretching) to the left. Most economic data sets have skewed
histograms.
Multiple peaks imply that more than one source, or population, of data is being
evaluated.
In our example, the MSFT stock returns seem to be fairly symmetric. Remember, the
Skewness value from the Descriptive Statistics analysis was also between -1 and 1.
However, we can also observe that there is some negative skewness.
Histograms
The intervals of values for which frequencies are
Bins
calculated.
Summary A histogram with only one peak: a central high part with
Symmetric
almost equal lower parts to the left and right of this peak.
A histogram with a peak on the left and many lower
Positively Skewed
points (stretching) to the right.
A histogram with a peak on the right and many lower
Negatively Skewed
points (stretching) to the left.
A histogram with multiple peaks suggests that more than
Multiple Peaks
one source, or population, of data is being evaluated.
Relationships in data are usually identified by comparing two variables: the dependent
variable and the independent variable. The dependent variable is the variable that we
are most interested in. We may be trying to predict values for this variable by
understanding its current behavior in order to better predict its future behavior. The
independent variable is the variable that we use as the comparison in order to make the
prediction. There may be various independent variables with known values that we can
use to analyze the relationship against the dependent variable. However, only one
independent variable provides the most accurate understanding of the dependent
variable’s behavior.
Note that since our prediction of the dependent variable relies on the independent
variable, we can only predict values corresponding to known independent variable
values. In other words, we cannot predict beyond the scope of available independent
variable data, nor can we predict the independent variable itself.
We can graph this data (with the XY Scatter chart type) by placing the independent
variable on the x-axis and the dependent variable on the y-axis and then using a tool in
Excel called a trend curve to determine if any relationship exists between these
variables.
To add a trend curve to our chart, we right-click on the data points in our XY Scatter
chart and choose Add Trendline from the drop-down list of options. There are five basic
trend curves that Excel can model: Linear, Exponential, Power, Moving Average, and
Logarithmic. Each of these curves is illustrated in the Add Trendline dialog box, which
appears in Figure 7.24.
Figure 7.24 The five trend curves that Excel can fit to data.
We will discuss how to identify linear, exponential, and power curves in a chart. If a
graph looks like a straight line would run closely through the data points, then a linear
curve is best. If the dependent variable (on the y-axis) appears to increase at an
increasing rate, then the exponential curve is more favorable. Similar to the exponential
curve is the power curve; however, the power curve has a slower rate of increase in
terms of the dependent variable.
Depending on which curve we select, Excel fits this type of trend curve to our data and
creates a trendline in the chart. There are different equations for each trend curve used
to create the trendline based on our data. We will discuss this in more detail later. For
Linear trend curves, Excel produces the “best fitting” trendline of the selected trend
curve by minimizing the sum of the squared vertical distances from each data point to
the trendline. This vertical distance is called the error, or residual. A positive error
implies that a point lies above the line, and a negative error implies that a point lies
below the line. This trendline is therefore referred to as the least squares line.
After we select the curve that we feel best fits our data, we click on the Options tab (see
Figure 7.25). The first option to set is the trendline’s name; we can either use the
automatic name (default) or create a custom name. The next option is to specify a period
forward or backward for which we want to predict the behavior of our dependent
variable. This period is in units of our independent variable. This is a very useful tool
since it is one of the main motivations for using trend curves. The last set of options
allows us to specify an intercept for the trendline and to display the trendline equation
and the R-squared value on the chart. We will usually not check to Set Intercept;
however, we always recommend checking to Display Equation and Display R-Squared
Value. We will discuss the equation and the R-squared value for each trend curve in
more detail later.
Figure 7.25 The Options tab of the Add Trendline dialog box.
We can also right-click on any trendline after it has been created and choose Format
Trendline from the list of options. This selection allows us to modify the Type and
Options initially specified as well as to change any Patterns on the trendline (see Figure
7.26).
Let’s compare some examples of these three different trend curves. We will begin with
Linear curves. Suppose a company has recorded the number of “Units Produced” each
month and the corresponding “Monthly Plant Cost” (see Figure 7.27). The company may
be able to accurately determine how much they will produce each month; however, they
want to be able to estimate their plant costs based on this production amount. They will
therefore need to determine, first of all, if there is a relationship between “Units
Produced” and “Monthly Plant Cost.” If so, then they need to establish what type of
relationship it is in order to accurately predict future monthly plant costs based on future
unit production.
The dependent variable is therefore the “Monthly Plant Cost” and the independent
variable is the “Units Produced.” We begin this analysis by making an XY Scatter chart
of the data (with the dependent variable on the y-axis and the independent variable on
the x-axis). Figure 7.28 displays this chart of “Monthly Plant Cost per Units Produced.”
Figure 7.27 A record of the “Units Produced” and the “Monthly Plant Cost” for twelve
months.
Figure 7.28 The XY Scatter Chart for the “Monthly Plant Cost per Units Produced.”
We can now right-click on any of the data points and choose Add Trendline from the list
of drop-down options (see Figure 7.29). The Linear trend curve seems to fit this data
best. (You might also think the Power trend curve fits well. It is okay to try different trend
curves to evaluate which gives you the most accurate relationship for predictions.) We
select Linear from the Type tab and then select Display Equation on Chart from the
Options tab (see Figure 7.30).
Figure 7.29 Selecting the Linear trend curve from the Type tab.
The trendline and the equation are then added to our chart, as illustrated in Figure 7.31.
Let’s now decipher what the trendline equation is. The x variable is the independent
variable, in this example, the “Units Produced.” The y variable is the dependent variable,
in this example, the “Monthly Plant Cost.” This equation suggests that for any given
value of x, we can compute y. That is, for any given value of “Units Produced,” we can
calculate the expected “Monthly Plant Cost.” We can therefore transfer this equation into
a formula in our spreadsheet and create a column of “Predicted Cost” relative to the
values from the “Units Produced” column.
In Figure 7.32 the following formula operates in the “Predicted Cost” column:
=88.165*B4 – 8198.2
We copy this formula for the entire “Predicted Cost” column using relative referencing for
each value in the “Units Produced” column. We then create an “Error” column, which
simply subtracts the “Predicted Cost” values from the actual “Monthly Plant Cost” values.
As the figure suggests, there is always some error since the actual data does not lie on a
straight line. (Again, you could try calculating the “Predicted Costs” using a Power trend
curve to compare the “Error” values.)
Figure 7.32 Adding the “Predicted Cost” and “Error” columns to the table using the
Linear trendline equation.
Now we have enough information to address the initial problem for this example:
predicting future “Monthly Plant Costs” based on planned production amounts. In Figure
7.33, we have added “Units Produced” values for three more months. Copying the
formula for “Predicted Cost” to these three new rows gives us the predicted monthly
costs.
Figure 7.33 Calculating the “Predicted Cost” for the next three months.
(Note that the independent variable must be known for the time period for which we want
to predict the dependent variable values. That is, we are only able to predict the monthly
costs for this example because we assume that the production amount is known for the
next three months.)
Now let’s discuss Exponential trend curves. In Figure 7.34, we have “Sales” data for ten
years. If we want to be able to predict sales for the next few years, we must determine
what relationship exists between these two variables. So, our independent variable is
“Years” and our dependent variable is “Sales.”
After creating the XY Scatter chart of this data (x-axis as “Year,” y-axis as “Sales”), we
right-click on a data point to add the trendline (see Figure 7.35). This time, we choose an
Exponential curve to fit our data. (Again the Power curve seems like another possible fit
that we could test.) We also choose to display the trendline equation on the chart. Figure
7.36 displays the resulting chart with the trendline.
Let’s analyze the equation provided on the chart. Again, the y variable represents the
dependent variable, in this example, “Sales.” The x variable represents the independent
variable, in this example, “Year.” We can therefore transform this equation into a formula
in our spreadsheet and create a “Prediction” column in which we estimate sales based
on the year. In Figure 7.37, we have done so using the following formula:
=58.553*EXP(0.5694*A4)
The EXP function raises e to the power in parentheses. We have copied this formula for
all of the years provided in order to compare our estimated values to the actual values.
Notice that there are some larger “Error” values as the years increase.
Figure 7.37 Calculating the “Prediction” values with the Exponential trendline
equation.
We can now use this formula to predict sales values for future years. However, the
Exponential trend curve has a sharply increasing slope that may not be accurate for
many situations. For example, in six years from our current data, year 16, we have
estimated about 530,000 sales using the Exponential trendline equation. This amount
seems a highly unlikely number given previous historical data (see Figure 7.38). Even
though the Exponential trend curve increases rapidly towards infinity, it is unlikely that
sales will do the same. Therefore, for predicting values much further in the future, we
may consider using a different trend curve (perhaps the Power curve).
Figure 7.38 Using the Exponential trendline equation to predict sales for year 16.
Now let’s consider an example of a Power trend curve. In Figure 7.39, we are presented
with yearly “Production” and the yearly “Unit Cost” of production. We want to determine
the relationship between “Unit Cost” and “Production” in order to be able to predict future
“Unit Costs.”
We begin by creating the XY Scatter chart and then right-clicking on a data point to add
a trendline. This time we choose a Power curve to fit the data (see Figure 7.40).
(Exponential may also be an appropriate fit for this data, but the slope of the recorded
data points does not seem to be that steep.) Even though our data is decreasing, not
increasing, it is the slope of the data points that we are observing in order to find a
suitable fit. Again, we choose to display the trendline equation with the Options tab.
Figure 7.41 demonstrates the resulting trendline with the charted data points.
Figure 7.41 Fitting the Power curve to the “Unit Cost per Cumulative Production”
chart.
=101280*B4^-0.3057
Figure 7.42 displays these forecasted cost values and the “Error” calculated between the
forecasted and actual data. The error values seem to be fairly stable, therefore implying
a reliable fit.
Figure 7.42 Creating the “Forecast” and “Error” columns with the Power trendline
equation.
We would now like to make a note about using data with dates (for example the “Year” in
the above example). If dates are employed as an independent variable, we must convert
them into a simple numerical list. For example, if we had chosen to assign the “Year”
column in the above example as an independent variable for predicting the “Unit Cost,”
we would have had to renumber the years from 1 to 7, 1 being the first year, 2 the
second, etc., in which the data was collected. Using actual dates may yield inaccurate
calculations.
Trend Curves
Linear Curve y = a*x - b
Exponential Curve y = a*e^(b*x) or y = a*EXP(b*x)
Summary
Power Curve y = a*x^b
7.3.2 Regression
Another more accurate way to ensure that the relationships we have chosen for our data
are reliable fits is by using regression analysis parameters. These parameters include
the R-Squared value, standard error, slope and intercept.
The R-Squared value measures the amount of influence that the independent variable
has on the dependent variable. The closer the R-Squared value is to 1, the stronger the
linear relationship between the independent and dependent variables is. If the R-
Squared value is closer to 0, then there may not be a relationship between them. We
can then draw on multiple regression and other tools to determine a better independent
variable to predict the dependent variable.
To determine the R-Squared value of a regression, or a trendline, we can use the Add
Trendline dialog box on a chart of data and specify to Display R-Squared Value on Chart
in the Options tab (see Figure 7.43).
Let’s review the previous three examples to discover their R-Squared values. We have
gone back to our charts and added the R-Squared display option by right-clicking on the
trendline previously created. We then Format Trendline to revisit the Options tab and
specify this new option.
For the first example, we fit a Linear trendline to the “Monthly Plant Cost per Units
Produced” chart (see Figure 7.44). The R-Squared value is 0.8137, which is fairly close
to 1. We could try other trend curves and compare the R-Squared values to determine
which fit is the best.
In the following example, we fit an Exponential trendline to the “Sales per Year” chart
(see Figure 7.45). The R-Squared value for this data is 0.9828. This value is very close
to 1 and therefore a sound fit. Again, it is wise to compare the R-Squared values for
Exponential and Power curves on a set of data with an increasing slope.
In the last example, we fit a Power trendline to the “Unit Cost per Cumulative Production”
chart (see Figure 7.46). The R-Squared value is 0.9485, which is also very close to 1
and therefore an indication of a good fit.
Excel’s RSQ function can calculate an R-squared value from a set of data using the
Linear trendline. The format of the RSQ function is:
=RSQ(y_range, x_range)
Note that this function only works with Linear trend curves. We must also make sure that
we have entered the y_range, or the dependent variable data, before the x_range, or the
independent variable data. In Figure 7.47, we have employed the RSQ function with the
first example from above to measure the accuracy of a Linear trendline as applied to the
“Monthly Plant Cost per Units Produced” data. We can verify that the result of this
function is the same as the one attained with the R-Squared value.
Figure 7.47 Using the RSQ function to calculate the R-Squared value of the Linear
trendline.
The standard error measures the accuracy of any predictions made. In other words, it
measures the “spread” around the least squares line, or the trendline. We have learned
previously that this value can be found using Descriptive Statistics. It can also be
calculated in Excel with the STEYX function. The format of this function is:
=STEYX(y_range, x_range)
Again, this function can only be used for Linear trend curves. In the example above, we
have calculated the standard error using the STEYX function (see Figure 7.48). We can
now use this value to check for outliers as we did using the standard deviation value in
the previous sections. These outliers reveal how accurate our fit is with a Linear
trendline.
Figure 7.48 Using the STEYX function to calculate the standard error.
Two other Excel functions that can be applied to a linear regression line of a collection of
data are SLOPE and INTERCEPT. The SLOPE function’s format is:
=SLOPE(y_range, x_range)
Similarly, the intercept of the linear regression line of the data can be determined with
the INTERCEPT function. The format of this function is:
=INTERCEPT(y_range, x_range)
In Figure 7.49, we are finding the slope and intercept of the linear regression line of the
“Monthly Plant Cost per Units Produced” data.
Figure 7.49 Finding the slope and intercept with the SLOPE and INTERCEPT
functions.
Regression
Measures the amount of influence that the
R-Squared Value independent variable has on the dependent
Summary variable.
Standard Error Measures the accuracy of any predictions made.
7.4 Distributions
We will now discuss some of the more common distributions that can be recognized
when performing a statistical analysis of data. These are the Normal, Exponential,
Uniform, Binomial, Poisson, Beta, and Weibull distributions. The Normal, Exponential
and Uniform distributions are those most often used in practice. The Binomial and
Poisson are also common distributions.
Most of these distributions have Excel functions associated with them. These functions
are basically equivalent to using distribution tables. In other words, given certain
parameters of a set of data for a particular distribution, we can look at a distribution table
to find the corresponding area from the distribution curve. These Excel functions perform
this task for us.
Let’s begin with the Normal distribution. The parameters for this distribution are simply
the value that we are interested in finding the probability for, and the mean and standard
deviation of the set of data. The function that we apply with the Normal distribution is
NORMDIST, and with these parameters, the format for this function is:
We will use the cumulative parameter in many Excel distribution functions. This
parameter takes the values True and False to determine if we want the value returned
from the cumulative distribution function or the probability density function,
respectively. A general difference between these two functions is that the cumulative
distribution function (cdf) determines the probability that a value in the data set is less
than or equal to x, while the probability density function (pdf) determines the probability
that a value is exactly equal to x. We will employ this general definition to understand the
cumulative parameter of other distribution functions as well.
For example, suppose annual drug sales at a local drugstore are distributed Normally
with a mean of 40,000 and standard deviation of 10,000. What is the probability that the
actual sales for the year are 42,000? To answer this, we use the NORMDIST function:
This function returns a 0.58 probability, or 58% chance, that given this mean and
standard deviation for the Normal distribution, annual drug sales will be 42,000 (see
Figure 7.50).
Figure 7.50 Using the NORMDIST with the cumulative distribution function.
The cumulative distribution can also determine the probability that a value will lie in a
given interval. Using the same example data, what is the probability that annual sales
will be between 35,000 and 49,000? To find this value, we subtract the cdf values for
these two bounds:
This function returns a 0.51 probability, or 51% chance, that annual sales will be
between 35,000 and 49,000 (see Figure 7.51).
Related to the Normal distribution is the Standard Normal distribution. If the mean of our
data is 0 and the standard deviation is 1, then placing these values in the NORMDIST
function with the cumulative parameter as True determines the resulting value from the
Standard Normal distribution. There are also two other functions that determine the
Standard Normal distribution value: STANDARDIZE and NORMSDIST.
STANDARDIZE converts the x value from a data set of a mean not equal to 0 and a
standard deviation not equal to 1 into a value that does assume a mean of 0 and a
standard deviation of 1. The format of this function is:
The resulting standardized value is then used as the main parameter in the
NORMSDIST function:
=NORMSDIST(standardized_x)
This function then finds the corresponding value from the Standard Normal distribution.
These functions are valuable as they relieve much manual work in converting a Normal x
value into a Standard Normal x value.
Let’s now consider the same example as above to determine the probability that a
drugstore’s annual sales are 42,000. We standardize this using the following function:
The result of this function is 0.2. We can then use this value in the NORMSDIST function
to compute the probability:
=NORMSDIST(0.2)
This function again returns a probability of 0.58 that the sales will reach 42,000 (see
Figure 7.52).
The Uniform distribution does not actually have a corresponding Excel function;
however, there is a simple formula that models the Uniform distribution. This formula, or
pdf, is:
1 / (b – a)
Given that a value x is Uniformly distributed between a and b, we can use this formula to
determine the probability that x will have an integer value in this interval. To apply this
formula in Excel, we recommend creating three columns: one for possible a values, one
for possible b values, and one for the result of the Uniform formula (see Figure 7.50).
Then, we just enter the Uniform formula by referencing the a and b value cells.
Figure 7.53 Using the Uniform distribution formula for various values of a and b.
The Poisson distribution has only the mean as its parameter. The function we use for
this distribution is POISSON and the format is:
(Note that for the Poisson distribution, the mean may be in terms of lambda*time.) The
Poisson distribution value is the probability that the number of events that occur is either
between 0 and x (cdf) or equal to x (pdf).
For example, consider a bakery that serves an average of 20 customers per hour. Find
the probability that, at the most, 35 customers will be served in the next two hours. To do
so, we use the POISSON function with a mean value of lambda*time = 20*2.
This function returns a 0.24 probability value that 35 customers will be served in the next
two hours (see Figure 7.54).
Figure 7.54 Using the POISSON function with the service time.
The Exponential distribution has only one parameter: lambda. The function we use for
this distribution is EXPONDIST and its format is:
(Note that the lambda value is equivalent to 1/mean.) The cumulative parameter is the
same as described above. The x value is what we are interested in finding the
distribution value for, and lambda is the distribution parameter.
This function returns a probability value of 0.96 that a customer will arrive within 10
minutes (see Figure 7.55).
Figure 7.55 Using the EXPONDIST function with the interarrival time.
The Binomial distribution has the following parameters: the number of trials and the
probability of success. We are trying to determine the probability that the number of
successes is less than (using cdf) or equal to (pdf) some x value. The function for this
distribution is BINOMDIST and its format is:
(Note that the values of x and trials should be integers.) For example, suppose a
marketing group is conducting a survey to find out if people are more influenced by
newspaper or television ads. Assuming, from historical data, that 40 percent of people
pay more attention to ads in the newspaper, and 60 percent pay more attention to ads
on television, what is the probability that out of 100 people surveyed, 50 of them respond
more to ads on television? To determine this, we use the BINOMDIST function with the
prob_success value equal to 0.60.
This function returns a value of 0.03 that 50 out of 100 people will report that they
respond more to television ads than newspaper ads (see Figure 7.56).
Figure 7.56 Using the BINOMDIST function with the survey data.
The Beta distribution has the following parameters: alpha, beta, A, and B. Alpha and
beta are determined from the data set; A and B are optional bounds on the x value for
which we want the Beta distribution value. The function for this distribution is BETADIST
and its format is:
If A and B are omitted, then a standard cumulative distribution is assumed and they are
assigned the values 0 and 1, respectively.
The result reveals that there is a 0.28 probability that they can finish the project in 10
days (see Figure 7.57).
Figure 7.57 Using BETADIST and calculating the alpha and beta values.
The Weibull distribution has the parameters alpha and beta. The function we use for this
distribution is WEIBULL and its format is:
(Note that if alpha is equal to 1, then this distribution becomes equivalent to the
Exponential distribution with lambda equal to 1/beta.) The Weibull distribution is most
commonly employed to determine reliability functions. Consider the inspection of 50 light
bulbs. Past data reveals that on average, a light bulb lasts 1200 hours, with a standard
deviation of 100 hours. We can use these values to calculate alpha and beta to be 14.71
and 1243.44, respectively. (Note that usually alpha and beta can be located in a
resource table for the Beta distribution.) We can now use the WEIBULL distribution to
determine the probability that a light bulb will be reliable for 55 days = 1320 hours.
The result is a 0.91 probability that a light bulb will last up to 1320 hours, or 55 days (see
Figure 7.58).
Figure 7.58 Using the WEIBULL function to determine the reliability of a light bulb.
7.5 Summary
¾ Some of Excel’s basic statistical functions are: AVERAGE to find the mean, MEDIAN
to find the median, and STDEV to find the standard deviation of a set of data.
¾ The Analysis Toolpack is an Excel Add-In that includes statistical analysis
techniques such as Descriptive Statistics, Histograms, Exponential Smoothing,
Correlation, Covariance, Moving Average, and others.
¾ The Descriptive Statistics option provides a list of statistical information about a data
set, including the mean, median, standard deviation, and variance.
¾ The Mean is the average of all values in a data set, or all observations in a sample.
The Median is the “middle” observation when data is sorted in ascending order. The
Mode is the most frequently occurring value.
¾ The Sample Variance is the average squared distance from the mean to each data
point. The Standard Deviation, s, is the square root of the Sample Variance. Any
values in the data set that lie more than +/-2s from the mean are called outliers.
Excel functions such as IF, ABS, and OR can identify outliers. Conditional Formatting
can also be used.
¾ Kurtosis is a measure of data peakedness. Skewness is a measure of how
symmetric or asymmetric data is.
¾ The Confidence Level for Mean constrains the mean calculation to a specified
confidence level. The Kth Largest and Kth Smallest options provide the respectively
ranked data value for a specified value of k.
¾ Similar to the Kth Largest and Kth Smallest options with Descriptive Statistics are the
two Excel functions PERCENTILE and PERCENTRANK.
¾ Histograms calculate the number of occurrences, or frequency, which values in a
data set fall into various intervals. Bins are the intervals into which values can fall;
they can be defined by a user or can be evenly distributed among the data by Excel.
The bin values are specified by their upper bounds.
¾ There are four basic shapes to a histogram: symmetric, positively skewed, negatively
skewed, and multiple peaks.
¾ Relationships in data are usually identified by comparing the dependent variable and
the independent variable. The dependent variable is a variable that the user tries to
predict values for; the independent variable is the variable that the user employs as
the comparison in order to make the prediction.
¾ You can graph this data (with the XY Scatter chart type) by placing the independent
variable on the x-axis and the dependent variable on the y-axis and then using a
trend curve to determine if any relationship exists between these variables. There
are five basic trend curves that Excel can model: Linear, Exponential, Power, Moving
Average, and Logarithmic.
¾ With Linear curves, there are two values that measure the accuracy of the
relationship between the dependent and independent variables. The R-Squared
value measures the amount of influence that the independent variable has on the
dependent variable. It can be calculated from the trendline chart or with the RSQ
function. The standard error also measures the accuracy of any predictions made
from this relationship. This value can be determined using the STEYX function.
¾ The SLOPE and INTERCEPT functions also analyze a Linear trend curve.
¾ Some of the more common distributions that can be recognized when performing a
statistical analysis of data are the Normal, Exponential, Uniform, Binomial, Poisson,
Beta, and Weibull distributions. Most of these distributions have Excel functions
associated directly with them and are basically equivalent to using distribution tables.
7.6 Exercises
7.6.1 Review Questions
1. The table below provides a sample of the starting salaries of all geography
graduates from a state university this year. What is your best estimate of a
“typical” starting salary for a geography graduate?
Student Salary
1 $22,259.00
2 $20,931.00
3 $26,271.00
4 $26,034.00
5 $26,632.00
6 $25,038.00
7 $26,338.00
8 $25,489.00
9 $26,670.00
10 $29,952.00
11 $21,237.00
12 $27,469.00
13 $27,568.00
14 $22,341.00
15 $26,967.00
16 $25,381.00
17 $22,075.00
18 $28,211.00
19 $20,748.00
20 $27,978.00
2. A quality assurance expert at a soft drink bottling plant has been assigned to
develop a plan to reduce the number of defective bottles that the plant produces.
To find the cause of the defects, she plans to analyze factors associated with the
bottling lines and the types of bottles being produced. The expert has randomly
sampled sets of bottles from different bottling lines and counted the number of
defective bottles in the sample. She records the bottling line, the size of the
sample, and the number of nonconforming bottles. She then computes the
fraction of nonconforming bottles. The table below contains her results. Create
this table in an Excel spreadsheet and make the following modifications:
a. Fill in the values for the “Fraction Nonconforming” column by dividing the
number of nonconforming bottles in the sample by the sample size.
Display the results as a percentage.
b. Compute the mean and standard deviation of the fraction of
nonconforming bottles found in the samples and record the results in the
bottom right-hand corner of the spreadsheet.
Fraction
Sample No. Line Number Sample Size No. Nonconforming
Nonconforming
1 1 100 9
2 1 100 5
3 1 90 7
4 2 75 4
5 2 100 11
6 2 90 8
7 2 80 8
8 3 80 6
9 3 100 10
10 3 90 7
Mean=
Standard Deviation=
3. In New York, Electro produces voltage that regulates equipment and then ships
the equipment to Chicago. The voltage held is measured in NY before each unit
is shipped to Chicago. The voltage held by each unit is also measured when the
unit arrives in Chicago. A sample of voltage measurements at each city is
provided below.
4. Use the data below to create a histogram for annual returns on stocks, bills, and
bonds. Which investment has the highest average return?
5. Using the above data, describe the type of histogram for each investment option:
symmetric, positively skewed, negatively skewed, and multiple peaks.
6. The table below records monthly returns on the S and P stock index and Dell
stock.
7. The following table lists the square footage and sales price for several houses.
2436 $ 272,652.00
3894 $ 384,248.00
4093 $ 435,207.00
1548 $ 182,280.00
a. If you build a 400 square foot addition to your house, by how much do
you feel you will increase its value?
b. What percentage of the variation in home values is explained by variation
in house size?
c. A 2500 square foot house is selling for $470,000. Is this price out of line
with typical home values? Explain.
8. Given additional information on the number of bedrooms and bathrooms for the
above house data, which factor (“Square Footage,” “Bedrooms,” or “Bathrooms”)
has the strongest relationship with the sales price?
9. Given the yearly revenues (in millions) of the companies in the table below,
determine the following:
a. Which company’s revenues best fit an Exponential trend curve.
b. The annual percentage growth rate for revenues.
c. Predicted 2003 revenues.
10. A marketing manager estimates total sales as a function of price, as seen below.
a. Estimate the relationship between price and demand.
b. Predict the demand for the $69 price.
c. By how much will a 1 percent increase in price reduce the demand?
Price Demand
$45.00 350
$55.00 310
$65.00 276
$75.00 203
$85.00 140
$95.00 98
11. The manager of the sales department of a leading magazine publication has
recorded the number of subscriptions sold for various numbers of sales calls.
a. If he were to make 75,000 sales calls next month, how many
subscriptions could he estimate selling?
b. If he wanted to sell 80,000 subscriptions, how many sales calls would he
have to make?
12. A human resources manager wants to examine the relationship between annual
salaries and the number of years that employees have worked at the company. A
sample of collected data is below:
20 $70,000
30 $75,000
13. Consider the relationship between the size of the population and the average
household income level for several small towns. A sample of this data is below:
14. A bank is trying to prove that they do not practice gender discrimination. They
have a record of the education level, age, gender, and salary of each employee
(provided below).
Determine which factor has the strongest relationship with the salary of the
employees.
a. Which trend curve fits the data better, a Linear, Exponential, or Power
curve?
b. What are the R-Squared values of each curve?
c. How much cost can they expect if they produce 800 units?
16. A new industrial production company wants to analyze their production time to
determine if they have improved productivity after gaining a few months of
experience. The table below records the times to produce each batch of
products.
Batch Time
1 125
2 110
3 105
4 103
5 99
6 98
7 94
8 93
9 92
10 87
11 89
12 85
13 84
14 78
15 81
16 79
17 76
18 75
19 76
20 74
17. Suppose that car sales follow a Normal distribution with a mean of 50,000 cars
and a standard deviation of 14,000 cars.
a. There is a 1 percent chance that the car sales will be how many cars next
year?
b. What is the probability that they will sell less than or equal to 2.7 million
cars during the next year?
18. Given that the weight of a typical American male follows a Normal distribution
with a mean of 180 lb and standard deviation of 30 lbs, what fraction of American
males weigh more than 225 lbs?
20. Assume that the monthly sales of a toys store follow an Exponential distribution
with mean 560. What is the probability that sales will be over 600 in January?
22. Using the Binomial distribution, assume that on average 95 percent of airline
passengers show up for a flight. If a plane can seat 200 passengers, how many
tickets should be sold to make the change of an overbooked flight less than or
equal to 5 percent?
23. A professor gives his students a 20-question True or False exam. Each correct
answer is worth 5 points. Consider a student who randomly guesses on each
question.
a. If no points are deducted for incorrect answers, what is the probability that
the student will score at least 60 points?
b. If 5 points are deducted for each incorrect answer what is the probability
that the student will score at least 60 points?
24. Suppose that the interarrival time between customers at a bank are Exponentially
distributed with a mean of 45 seconds. If you just observed an arrival, what is the
probably that you will need to wait more than a minute before observing the next
arrival? What is the probability that you will need to wait at least 2 minutes?
25. The following table presents the weekly sales of floppy disk drives in a local
computer dealer.
a. Find the trendline that fits the data best (linear, exponential, etc).
b. Present the R-square for each trendline considered in part a.
c. What are the expected sales for weeks 13 and 14?
26. The length of an injection-molded plastic case that holds magnetic tape is
normally distributed with mean 80.3 millimeters and standard deviation 0.2
millimeters.
a. What is the probability that the bottle weights more than 13 ounces?
b. What is the probability that the bottle weights no more than 13 ounces
and no less than 11 ounces
c. What must the standard deviation of weight be in order for the company
to state 99.9% of its bottles weight less than 13 ounces?
d. If the standard deviation remains 0.5 ounce, what must the mean be in
order for the company to state that 99.9% of the bottles produced are
less than 13 ounces?
28. The length of time (in seconds) that a user views a page on a Web site before
moving to another page is lognormal random variable with parameters θ = 0.5
and ω2 = 1.
a. What is the probability that a page is viewed for more than 10 seconds?
b. What is the length of time that 50% of users view the page?
c. Plot the density function of this distribution. Change the value of θ to 1
and plot the density function again.
Chapter Overview
8.1 Introduction
8.2 Formulating Mathematical Programs
8.2.1 Parts of the Mathematical Program
8.2.2 Linear, Integer, and Nonlinear Programming
8.3 The Excel Solver
8.3.1 The Solver Steps
8.3.1.1 Standard Solver
8.3.1.2 Premium Solver
8.3.2 A Solver Example
8.3.2.1 Product Mix
8.3.2.2 Infeasibility
8.3.2.3 Unboundedness
8.3.3 Understanding Solver Reports
8.4 Applications of the Solver
8.4.1 Transportation Problem
8.4.2 Workforce Scheduling
8.4.3 Capital Budgeting
8.4.4 Warehouse Location
8.5 Limitations and Manipulations of the Solver
8.6 Summary
8.7 Exercises
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
8.1 Introduction
This chapter illustrates how to use the Excel Solver as a tool to solve mathematical
programs. We review the basic parts of formulating a mathematical program and present
several examples of how the Solver interprets these parts of the program from the
spreadsheet. We give examples of linear, integer, and non-linear programming problems
to show how the Solver can be used to solve a variety of mathematical programs. We
also give an overview of the Premium Solver and its benefits. This chapter is important
for the reader to understand as many DSS applications involve solving optimization
problems, which are mathematical programs. The reader should be comfortable with
preparing the spreadsheet for use with the Solver. In Chapter 19, we revisit the Solver
using VBA commands. We have several examples of DSS applications which use the
Solver to solve optimization problems, such as Portfolio Management and Optimization.
A mathematical program consists of three main parts. The first is the decision
variables. Decision variables are assigned to a quantity or response that we must
determine in a problem. For example, if a toy manufacturer wants to determine how
many toy boats and toy cars to produce, we assign a variable to represent the quantity
of toy boats produced, x1, and the quantity of toy cars produced, x2. Decision variables
are defined as negative, non-negative, or unrestricted. An unrestricted variable can be
either negative or non-negative. These variables represent all other relationships in a
mathematical program, including the objective, the limitations, and the requirements.
The second part of the math program, called the objective function, is an equation that
states the goal, or objective, of the model. In the same example of the toy manufacturer,
we want to know the quantities of toy boats and toy cars to produce. However, the goal
of the manufacturing plant’s production may be to increase profit. If we know that we can
profit $5 for every toy boat and $4 for every toy car, then our objective function is:
In other words, we want profit to drive us in determining the quantity of boats and cars to
produce. Objective functions are either maximized or minimized; most applications
involve maximizing profit or minimizing cost.
The third part of the math progam, the constraints, are the limitations of the problem.
That is, if we want to maximize our profit, as in the toy manufacturer example, we could
produce as many toys as possible if we did not have any limits. However, in most
2
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
realistic situations, there are certain limitations, or constraints, that we must consider.
Constraints can be a limited amount of resources, labor, or requirements for a particular
demand. These constraints are also written as equations, or inequalities in terms of the
decision variables. That is, if we can only use 20 hours of labor in a week and we need
0.5 hours to produce each toy boat and 0.3 hours to produce each toy car, then we write
our constraint as follows:
0.5x1 + 0.3x2 ≤ 20
There are three main categories of problems for which we can use the above
mathematical program parts: Linear Programming (LP), Integer Programming (IP),
and Nonlinear Programming (NLP).
Linear programming problems have a linear objective function and linear constraints.
That is, there are no variables of multiple powers such as x2 and x3, and no terms
involving two variables such as x1x2. In addition, LP problems consist of decision
variables with any range or interval of values, x ≥ 0 or x ≤ 0. An example of an LP would
be a production problem in which we want to maximize profit by determining how many
of several different product types we want to produce. The objective function could
therefore be expressed as:
n
z= ∑ px
i =1
i i
Where i = product number for n products, pi = profit per product i, xi = amount produced
of product i. This is therefore a linear objective function. If we assume that the
constraints are also linear, then this is a linear programming problem. We will revisit this
example in more detail in section 8.2.2.
Integer Programming is related to linear programming in that both the objective function
and constraints are linear; however, the decision variables can have only integer values
in a given range. Integer programming is also applied when decision variables are
Boolean, which means that they only take the values true or false, yes or no, go or no
go − all of which are mathematically represented as 0 or 1, respectively. An example of
an IP would be a capital budgeting problem in which we want to decide which projects to
invest in and which not to invest in. This decision is a yes/no decision that can be
represented by the following objective function:
n
z= ∑ yx
i =1
i i
3
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Where i = project number for n projects, yi = NPV per project i, xi = decision to invest per
project i. This is therefore a linear objective function. What makes it an integer
programming problem is that we limit the values of xi to 1 or 0 to reflect whether or not
we have or have not invested in a project, respectively. We will revisit this example as
well in more detail in section 8.3.2.
n
z= ∑
i =1
( xi − xw) 2 + ( yi − yw) 2
Where i = facility number for n facilities, xi and yi = coordinates of each facility i, xw and
yw = coordinates of the warehouse. This is therefore a nonlinear objective function. Even
if the constraints are all linear, it is still a nonlinear programming problem. We will also
revisit this example in more detail in section 8.3.3.
Linear Programming Both the objective function and the constraints are
linear. Decision variables can have any range or
interval of values.
Summary Integer Programming An LP in which decision variables can only take
integer values in a given range or Boolean values.
(Note: To find the Solver, go to Tools > Solver from the menu options. If you do not see
Solver in the Tools menu, you must first choose the Solver Add-In. To do so, select the
Add-In option from the Tools menu. A small dialog box will appear; from there, select
Solver Add-In from the list. If you do not see Solver Add-In in the Add-In list, click
Browse and look for the Solver.xla file from the following directory: C Drive > Program
Files > Microsoft Office > Office (or Office10) > Library > Solver. Double-click this file.
Now you should find Solver Add-In in the list; check the box next to it. Restart Excel. If
you do not find the Solver.xla file, go to the Add-Ins window as explained above; select
Solver Add-in and press OK. Insert the MS Office CD in CD-ROM drive when asked.)
To operate the Solver, we must follow a short sequence of steps: 1) read and interpret
the problem; 2) prepare the spreadsheet; and 3) solve the model and review the results.
We will now describe these steps in detail for both the Standard Solver and the Premium
Solver.
5
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Another important consideration when laying out the constraints in preparation for the
Solver is that there must be individual cells for the right-hand side (RHS) values as well.
We should also place all inequality signs in their own cells. This organization will become
clear once we explain how the Solver interprets our model.
Another advantageous way to keep our constraints organized as we use the Solver is to
name cells. We can also group constraints that have the same inequality signs. The
benefit of this habit will become apparent once we input the model parts for the Solver.
6
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.3 The Solver Parameters window reads the decision variables, constraints,
and objective function as parameters of the model.
Figure 8.4 Adding Constraints involves selecting the cell with the equation formula,
choosing the inequality or equality sign, and selecting the cell with the RHS value.
Comment and Report are optional.
Excel also allows us to define more than one constraint at a time. By grouping
constraints that have the same inequality signs, we can select an entire range of
constraint formulas and RHS values and choose the common inequality sign. Naming
7
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
constraints with the same inequality can also clarify what we add to the Solver and
prevent us from making any mistakes. If multiple ranges are not adjacent, we can select
them by holding down the CTRL key or by separating them with commas in the
Constraint window.
We have now added all of our constraints, so we press OK and return to the Solver
window. We can observe all of our constraints in the Constraint textbox. Note that we
can press the Change button to edit any of the constraints and the Delete button to
erase them.
Figure 8.5 The Options window allows a user to specify how Solver will work with a
model.
For now, we leave the Estimates, Derivatives, and Search options at their default values.
Assuming a Linear Model and Assuming Non-Negative decision variables are essential
options to consider. It is important to always check the Assume a Linear Model option
when solving a Linear programming problem in order to guarantee an optimal solution.
8
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
solution”; or “Target Cell values (or Set Cell values) could not converge.” The
spreadsheet cells we set when formulating the model now have values for the decision
variable cells. Therefore, they also have values in the constraint and objective function
cells since they contain formulas referencing the decision variable cells. We can confirm
that all constraints have been satisfied by noting that the values in the constraint cells
with the Solver solution are all less than or equal to, or greater than or equal to the RHS
values, respectively.
Figure 8.6 Excel displays this window after we run Solver and find a solution.
After reviewing this solution, we can choose to Keep the Solver Solution or Restore our
Original Values. We can also opt to have some extra reports made from the Solver
solution: the Answer report, the Sensitivity report, and the Limits report. We will discuss
these reports in more detail later.
Premium Solver
Steps 1 and 2 are exactly the same when using the Premium Solver. Even the sub-steps
3.1, 3.2, and 3.3 are no different. However, depending on which algorithm we choose to
use, there is a different set of Options for sub-step 3.4.
To use the Premium Solver, we simply press the Premium button on the Solver window.
(Note that the Premium button now becomes a Standard button to allow us to switch
back to the standard Solver.) The main Solver window has a drop down list to select
9
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
which Solver version, or which set of algorithms, we want to use to solve our problem
(see Figure 8.7). The Standard GRG Nonlinear and the Standard Simplex LP
versions are basically the same as the Standard Solver for LP, IP, and NLP models. We
use the Standard Evolutionary version to solve our more complex NLP models; we will
refer to it as the Premium Solver.
Figure 8.7 The Premium Solver offers three different Solver methods.
The Evolutionary Solver employs the Genetic Algorithm to solve Nonlinear problems.
(We will discuss this in more detail in a later section.) Once we select this version, we
click on the Options button to see the window shown in Figure 8.8. We can observe
several similar options as in the Standard Solver such as Max Time, Iterations,
Precision, and Assume Non-Negative.
The Population Size and Mutation Rate are parameters associated with the Genetic
Algorithm. The Population Size basically sets the initial number of feasible solutions. The
Mutation Rate determines how often the “Mutation” part of the Genetic Algorithm is
performed. We recommend setting the Population Size to at least 50; if there are several
variables and constraints, it may be wiser to have a Population Size of 100 or 1,000. The
Mutation Rate can be small, but it usually performs better at 0.25 than at 0.075. The
Local Search options can be specified to further determine the behavior of the Genetic
Algorithm. At this point, we will not further discuss this algorithm.
10
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
After we have specified these options, we can press Solve to run the Solver. The Solver
Results window as shown in Figure 8.9 then appears. We may see “Solver found
Solution” or “Stop chosen when…” as possible messages in this window. We also have
a different list of available Solver Reports, including the Population Report. This report
provides the best, mean, maximum, and minimum values along with the standard
deviation of the Changing Cells, or decision variable, values across all iterations.
Figure 8.9 The Solver Reports for the Evolutionary Solver include a Population
Report.
Now that we are familiar with the three steps of using the Solver to determine the
solution of a mathematical model, let’s work through a more complicated example.
11
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Problem Description:
A company produces six different types of products. They want to schedule their
production to determine how much of each product type should be produced in order to
maximize their profits. This situation is known as the “Product Mix” problem.
Production of each product type requires labor and raw materials, but the company is
limited by the amount of resources available. There is also a limited demand for each
product, and no more than this demand per product type should be produced. Input
tables for the necessary resources and the demand are provided.
This is a Linear programming problem, as the constraints and objective function are
linear with respect to the decision variables, as we will see below. We therefore decide
to use the Standard Solver. Let’s now outline the model parts.
Decision Variables:
For the amount produced of each product type, we use the following variable
representation:
Objective Function:
Maximize Profit. Profit is calculated as the sum of the array multiplication of the unit
profit, p, and the amount produced of each product type. We write this equation as
follows:
z = ∑i pixi for i = 1 to 6
Here, p1 is the amount of profit gained per unit of product 1. Therefore, p1*x1 is the
amount of profit per unit of product 1 times the number of units produced of product 1,
thus yielding the total profit from product 1. The same follows for the other products, 2
through 6.
Constraints:
There are two resource constraints: labor, l, and raw material, r. Available amounts are
provided for each resource, and required amounts are provided for the production of
each product type. We therefore say that the sum of the array multiplication of the
resource requirements and the amount produced of each product type must be less than
or equal to the amounts available of each resource. These equations are written:
Labor Constraint:
= ∑i lixi <= available labor = 4500
12
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Here l1 is the amount of labor required per unit produced of product 1. Similarly, r1 is the
amount of raw material required per unit produced of product 1. Therefore, the equations
represent the total labor and raw material needed for all products.
There is also a constraint that all demand, D, should be met (but is not enforced), and no
extra amount should be produced. Therefore, the amount produced of each product type
must be less than or equal to the given demand quantities. This constraint can be written
as follows:
Demand Constraint:
xi <= Di for i = 1 to 6
Next, we set up the spreadsheet to list these parts of the model in such a way that the
Solver can read them clearly.
The Spreadsheet:
In Figure 8.10, we observe the overall spreadsheet layout. We have organized our cells
by Input, Decision Variables, Constraints, and Objective Function. The Input table was
given. We calculate the Unit Profit row by subtracting the Variable Cost from the Unit
Price.
We have also labeled some ranges on the spreadsheet. We have named the Decision
Variable range “PMDecVar,” the Labor resource requirement row “PMLabor,” the Raw
Material resource requirement row “PMRawMat,” and the Unit Profit row “PMUnitProfit.”
These names will be helpful for writing the constraint and objective function formulas as
well as for inserting cell references in the Solver.
To prepare the constraint and objective function formulas, we use the SUMPRODUCT
function. Remember from Chapter 4 that this function takes two arrays, or ranges, as
13
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
parameters for which it will multiply and sum all values. Referring to the equations
written above and the range names created, we write the constraint formulas as follows:
Labor Constraint:
=SUMPRODUCT(PMDecVar, PMLabor)
The right-hand side values are equal to the “Available” amounts from the Input table (see
Figure 8.11).
Figure 8.11 The Labor and Raw Material constraint formulas use the SUMPRODUCT
function.
For the demand constraint, we simply need to ensure that the values in our decision
variable range are less than each of the corresponding values in the “Demand” range.
We do not require a formula for this constraint (see Figure 8.12).
The objective function formula is also written with the SUMPRODUCT function (see
Figure 8.13). Referring to the equation and range names above, we type the following
formula:
=SUMPRODUCT(PMUnitProfit, PMDecVar)
14
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.13 The objective function formula employs the SUMPRODUCT function.
After appropriately preparing the cells, we can now use the Solver to find our solution. In
Figure 8.14, the Solver dialog box appears. We have set the Target Cell to the location
of the objective function formula, the Changing Cells are set to the empty decision
variable cells, which we named “PMDecVar,” and the “Constraints” box shows the left
and right sides of the constraint equations with the corresponding inequalities. Here, the
labor and raw material constraints are listed first, then the demand constraint is listed
with the decision variable cells. We have also selected the two options Assume Linear
Model and Assume Non-Negative.
The Solver Results window reveals that a solution was successfully found (see Figure
8.15). We can view the final results in Figure 8.16. Notice that all constraints are met.
The company now knows how much to produce of each product type and what their
maximum profit will be. (There may be multiple solutions, but we cannot find them or
determine if there are more solutions with the Standard Solver.)
15
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Infeasibility
An infeasible problem is one in which at least one of the constraints cannot be met. For
this example, we consider infeasibility based on the “Demand” Constraint. Note in the
solution above that some of the product types did not meet their demand since the
“Demand” Constraint inequalities were <= in order to avoid the cost of over-production.
Now let’s assume that the company insists that demand is always met, even if some
waste is produced. We now need to change the “Demand” Constraint inequality from <=
to >=. To do so, we select the “Demand” Constraint in the Solver window from the list of
constraints and use the Change button (see Figure 8.17).
16
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
However, now when we press Solve, the Solver Results window conveys that the Solver
could not find a feasible solution with this modified constraint (see Figure 8.18). If there
are not enough resources available to meet the demand, then the solution is infeasible.
The result of this infeasible solution is shown in Figure 8.19.
17
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Unboundedness
An unbounded problem is one in which the objective function can reach an unreasonably
large number (if we are maximizing) or small number (if we are minimizing). Such a
situation implies that the constraints are not inclusive enough. We consider
unboundedness for this example in the case where we are able to maximize the profit to
an unreasonably huge value. Now, let’s suppose that there are no “Demand”
Constraints. We select the “Demand” Constraint from the constraint list in the Solver
window and press Delete. Let’s also assume at this point that negative decision variable
values are allowed. To do so, we re-visit the Options window and uncheck the Assume
Non-Negative option. The updated model now appears in the Solver window, displayed
in Figure 8.20.
Figure 8.20 The updated Solver model does not have the “Demand” Constraints.
This time, after pressing Solve, the Solver Results window tells us that a solution did not
converge (see Figure 8.21). That means that without the “Demand” Constraints or non-
negativity assumption we are able to achieve extremely large profit values.
Figure 8.21 The Solver solution did not converge without the “Demand” Constraints
and non-negativity assumption.
18
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
The three main reports available when using the Standard Solver are the Answer report,
the Sensitivity report, and the Limits report. We will now briefly review what information
is contained in these reports. (Note that there are some limitations to the Solver Reports
that we will discuss in the “Limitations and Manipulations” section.)
The Answer Report provides the original and final values of the Target Cell, Changing
Cells, or Adjustable Cells, and the Constraints (see Figure 8.22). It also gives the
reference of all of these cells on the spreadsheet. The names for each cell are based on
the row and column labels next to the tables on our spreadsheet. The formulas for the
constraints are provided only as references for where they formulas are held; in other
words, any functions used are not reported here. The Status of the Constraint part of the
report conveys whether or not a constraint is binding. A constraint is binding when its
slack value is zero.
19
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
The Sensitivity Report provides information about the Changing Cells and the
Constraints (see Figure 8.23) as well as their final values. The reduced cost or shadow
price and the allowable increase and decrease indicate how much flexibility can be
allowed with any of these values in order to achieve the desired objective function value.
The Limits Report provides information about the Target Cell and the Changing Cells
(see Figure 8.24); it also includes the value of each cell. The lower and upper limits of
the Changing Cells are listed next to the corresponding Target Cell value that would
result if the Changing Cell had the limit value.
20
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
The Input for this problem is in the first table in Figure 8.25. It contains the transportation
cost between each city and each region. It also displays the capacity per plant and the
demand per region.
The decision variables are the amount to ship from each plant to each region. We have
created a table with empty cells for these decision variables.
There are two constraints for this problem: “Demand” and “Capacity.” We need to ensure
that the total number of products shipped from a plant (to each region) is less than or
equal to its capacity, and we also need to ensure that the total number of products
received by a region (from each plant) is greater than or equal to its demand. We have
used the SUM function to create a column and row for these respective constraints. We
have then copied the capacity and demand from the Input table as the RHS value.
The objective function is to minimize the total transportation costs. We need to sum the
array multiplication between the given costs between each plant and region with the
amount shipped between each plant and region. To do so, we use the SUMPRODUCT
function. We have actually also named the range of decision variables as
“TransShipped” and the range of input costs as “TransCosts,” so the formula for the
objective function is simply:
=SUMPRODUCT(TransShipped, TransCosts)
21
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
We are now ready to use the Solver (see Figure 8.26). We set the Target Cell and
choose Min for our objective function. We then set the Changing Cells (notice that the
name of this range appears in the Solver window). We add both the capacity and
demand constraints to the constraint list. It is also very important that we specify two
Options for this Linear programming problem as well: Assume Linear Model and Assume
Non-Negative.
Figure 8.26 Completing the Solver window and specifying the Options.
The Solver solution appears in Figure 8.27. We have found the number of products to be
shipped from each plant to each region and the value of the resulting minimal
transportation cost. We can also check that all constraints have been met.
22
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Shown in the first table of Figure 8.28, the main Input for this problem is the number of
workers needed for each day of the week.. We also know that each employee works 5
consecutive days. We have represented this schedule in the second table by recording a
sequence of 1’s beginning on the day listed in each row. So, the Monday row has a 1 in
the Monday, Tuesday, Wednesday, Thursday, and Friday columns. The Tuesday row
has a 1 in the Tuesday, Wednesday, Thursday, Friday, and Saturday columns, and so
on. This table of consecutive 1’s will be used for the constraint formula.
The decision variables for this problem are the number of employees who will begin
working (for 5 consecutive days) on each day of the week. The column next to the
second table with empty cells is for the decision variables. We have also named this
range “SchedDecVar.”
There is only one constraint for this problem, which is to ensure that the total number of
employees working on a given day (regardless of which day they started working) is
greater than or equal to the number of employees needed on that particular day. To
achieve this, we again use the SUMPRODUCT function. We sum the array multiplication
of the decision variable column with the column of 1’s for each day. Since we have
named our decision variable range, this formula is:
23
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
=SUMPRODUCT(SchedDecVar, DayColumn9:DayColumn15)
The DayColumn letter value would change from D to J for Monday through Sunday
respectively. This formula appears in Figure 8.28.
=SUM(SchedDecVar)
Figure 8.28 The spreadsheet preparation for the Workforce Scheduling problem.
We are now ready to use the Solver (see Figure 8.29), so we specify the Target Cell and
choose Min for the objective function. We then set the Changing Cells. (Notice that the
name of this range appears in the Solver window.) We add the one constraint to the
constraint list. It is also very important that we specify two Options for this Linear
programming problem: Assume Linear Model and Assume Non-Negative.
24
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.29 Completing the Solver window and specifying the Options.
The Solver’s solution, shown in Figure 8.30 reveals the number of employees who will
start work on each day of the week. We can also check that the constraint is met.
However, we notice that some of the results of the decision variables and objective
function are non-integer. Technically, this solution is correct for the way we
communicated with the Solver, but it does not make much realistic sense to hire a total
of 19.33 employees.
Therefore, we need to enforce integer decision variables, thus making this an integer
programming problem. So, we actually need to add a constraint in the Solver window.
For this new constraint, we highlight the range of decision variables as the constraint
formula and choose int from the list of inequalities (see Figure 8.31). The RHS value
then automatically becomes “integer.”
25
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.31 The additional constraint enforces the decision variables to be integers.
Note that an extra constraint has been added to the constraint list in the Solver window
(see Figure 8.32). Since we named our decision variable range, this new constraint is
displayed in the constraint list simply as:
SchedDecVar = integer
The updated solution now has integer values for the decision variables and an objective
function that is more realistic (see Figure 8.33).
26
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.33 The updated Solver solution for the Scheduling problem.
The Input table lists the NPV and yearly costs for each project (see Figure 8.34).
27
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.34 The Input table for the Capital Budgeting problem.
The decision variables for this problem are the projects that we do and do not invest in.
These will have yes,no or go,no go values. We represent these binary options using 1’s
and 0’s. We have to ensure that the decision variables are only given binary values
when we add constraints to the Solver. We name this range “CBDecVar.”
There is only one constraint for this problem, which is that no more than the yearly
available funds can be spent annually. Since each project has associated yearly costs,
we must sum the costs of all of the projects that we have invested in each year to
determine if this constraint is met. To do so, we again use the SUMPRODUCT function.
The arrays for this function are the decision variables and the column of yearly costs
from the input table. Since the decision variable values are binary, only the costs for the
projects in which we will invest will be summed. Applying the range name given to the
decision variables, the formula is:
=SUMPRODUCT(CBDecVar, CostColumn4:CostColumn24)
The objective function is to maximize the total NPV. To determine this, we sum the array
multiplication of the decision variables and the column of NPV values for each project.
We have named this NPV column “CB_NPV.” Using this range name and the name of
the decision variable range, this formulas is:
=SUMPRODUCT(CBDecVar, CB_NPV)
See Figure 8.35 for the location and formulation of these model parts.
28
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Now we are ready to use the Solver. After specifying the Target Cell and Max for the
objective function, the Changing Cells, and the one constraint, we must also include the
additional binary variable constraint. To do so, we list the decision variables as a
constraint formula and choose “bin” from the list of inequalities. The word “binary”
automatically appears as the RHS value (see Figure 8.36). Now, when we return to the
Solver window (see Figure 8.37), we can see that this additional constraint has been
added as:
CBDecVar = binary
We also still specify the two Options: Assume Linear Model and Assume Non-Negative.
29
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.37 The completed Solver window for the Capital Budgeting problem with
binary decision variables.
Note that it takes a slightly longer time for the Solver to ascertain a solution, which is
provided as 1’s and 0’s in the column of decision variables (see Figure 8.38). This result
can be interpreted as: invest in the projects with 1’s; do not invest in the projects with
0’s. Therefore, in order to maximize NPV, we should only invest in 12 of the projects.
Figure 8.38 The Solver solution for the Capital Budgeting problem.
30
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
The Input for this problem is the location of each city identified by its latitude and
longitude. We are also provided with the number of shipments made to each city. This
input is illustrated in the first table of Figure 8.39. We have named the column of
shipments “WHShipments.”
The decision variables are the latitude and longitude values of the warehouse location.
We have created two empty cells for these and named each one “WHLat” and
“WHLong,” respectively.
There is only one constraint for this problem, which is that the latitude and longitude for
the warehouse location must be between the values of 0 and 120. We only need to add
the constraint that they be less than or equal to 120 since non-negativity is a Solver
option.
31
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
We now need to keep track of the distances between each city and the possible
warehouse location. These distances are calculated using the following Nonlinear
equation:
The CityLatitude and CityLongitude are calculated from the columns of the input table for
each city row. The SQRT function calculates the square root, which is a Nonlinear
manipulation of the decision variables. This column of distances appears in Figure 8.40.
We have named this column “WHDist.” (Note: The value 69 is based on the earth’s
curvature and is only used when computing latitude and longitude distances for US
cities.)
Figure 8.40 Calculating the distance between each city and the possible warehouse
location.
The objective function is to minimize the total distance traveled from the warehouse to
each city. It is the sum of the array multiplication between this column of distances and
the column of shipments made to each city from the input table. Since we have named
both of these ranges, the formula for the objective function is:
=SUMPRODUCT(WHDist, WHShipments)
32
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.41 The objective function formula for the Warehouse Location problem.
Let’s now use the Premium Solver, or Evolutionary Solver, to solve this nonlinear
programming problem. We begin by setting the Target Cell and choosing Min for the
objective function. Then, we set the Changing Cells and the Constraints. We next
choose Standard Evolutionary from the list of Solver versions (see Figure 8.42).
We now click on Options to view the Evolutionary Solver Options shown in Figure 8.43.
Here, we want to check Assume Non-Negative. (Note that Assume Linear Model is not
an option here.) We set the Population Size and Mutation Rate as shown.
33
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Now, when we operate the Solver, we find that several iterations of the Genetic
Algorithm are being run. After several seconds, the message in Figure 8.44 appears.
One of our option parameters has been reached, in this case the Maximum Iteration,
and we are given the choice to Continue or Stop. In this case, we stop here, but we
could continue for a more accurate solution.
Figure 8.44 The Show Trial Solution message offers the choice to stop or continue.
Since we chose to stop, the Solver Results window indicates when and why we stopped
(see Figure 8.45).
34
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Figure 8.45 The Solver Results window explains when and why the Solver stopped.
The solution appears in Figure 8.46. The latitude and longitude for the warehouse
location are displayed with the corresponding total minimal distance to be traveled to
each city for all shipments.
Figure 8.46 The Solver solution for the Warehouse Location problem.
35
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
Let’s first discuss problem size limitations. We are referring to the summarized table in
Figure 8.47 (from www.solver.com). The Standard Solver has a limit of 200 variables
and 200 constraints for LP problems, and 200 variables and 100 constraints for NLP
problems. We have also noted that it takes a long time for IP problems to be solved with
the Standard Solver.
The Premium Solver, on the other hand, has a limit of 1000 variables and 8000
constraints for LP problems, and 400 variables and 200 constraints for NLP problems.
Therefore, all large problems should be solved with the Premium Solver. The time for
solving IP problems with the Premium Solver is 10 to 20 times faster than the Standard
Solver.
The Standard Solver faces other problems in addition to size limitations. One error that
can occur involves scaling the related model parts. If there is a mixture of small and
large coefficient values in the constraints or the objective function and the possible
values that the decision variable can take, rescaling is necessary. For example, if we are
solving a binary IP problem whose decision variable values can only be 0 or 1 and
whose constraint coefficients are in the hundreds of thousands, the Solver will not be
able to recognize the problem as an LP model if we choose Assume Linear Model from
the Solver Options. To correct this error, we must first re-scale the problem by dividing
the constraint coefficients by 1000 and adjusting the costs to maintain proportionality.
The Standard Solver is then able to recognize the linearity of the problem and find a
solution, which should then be re-scaled again to find a solution proportional to the initial
coefficient values.
Another error that can occur with the Standard Solver involves the Sensitivity Report. If
the decision variables or Changing Cells have additional bound constraints and the
solution value of one of these variables is at its bound, then the reduced cost on the
Sensitivity Report becomes equal to the dual values of these variables. The additional
36
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
bound constraints will not be listed and no shadow price for these constraints will appear
either. For example, if we consider the Product Mix problem discussed in the Solver
Example section above, the “Demand” Constraints are additional bound constraints on
the decision variables. Therefore, if the solution to this problem has one of the decision
variable values at its demand bound, its reduced cost will become its dual value in the
Sensitivity Report and the “Demand” Constraint will not be listed. This is not correct, but
it happens.
The Premium Solver has corrected some of these and other problems of the Standard
Solver. There are additionally technical benefits to using the Premium Solver, including
the algorithms. The main algorithmic difference between the Standard Solver and the
Premium Solver is that the Standard Solver is limited by a slope-based algorithm for
solving NLP problems. This algorithm works well for convex maximization or concave
minimization problems; however, when neither of these is the structure of the problem,
there can be multiple optima that the Standard Solver will not recognize. Such examples
include problems with fixed charges or absolute values. Therefore, the Genetic
Algorithm of the Premium Solver is much better for solving NLP problems.
Aside from the Excel Solver, there are other optimization software packages available to
use with Excel. Frontline Systems, the developers of the Excel Solver, have developed
improved software for solving IP problems, called the XPress Solver. They have also
developed several large-scale solvers. Lindo Systems has additionally developed some
solvers, including What’s Best. What’s Best is a popular alternative to the Excel Solver;
however, it requires more optimization knowledge from the user in order to model the
problem directly in the spreadsheet. We have chosen to work with the Excel Solver for
this book since it is easier to use and learn in both the Excel spreadsheet and in VBA for
Excel.
8.6 Summary
¾ The three parts of a mathematical model are decision variables, objective
function, and constraints.
¾ The three primary types of mathematical models are linear, integer, and
nonlinear programming problems.
¾ Using Solver involves three main steps: reading and interpreting the problem to
determine the three parts of the model; preparing the spreadsheet so that Solver
can read the data; and running the Solver.
¾ Several applications of mathematical modeling exist for which Solver can be a
useful tool. Some LP examples are Transportation and Workforce Scheduling.
An IP example is Capital Budgeting, and an NLP example is the Warehouse
Location problem.
¾ We use the Premium Solver to solve NLP problems since the Genetic Algorithm
is more accurate.
37
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
8.7 Exercises
8.7.1 Review Questions
1. Use the Excel Solver to determine the solution to the following LP model:
Maximize Q = 3X + 4Y – 5Z
Subject to: 5X + Z ≤ 150
2X + 4Y ≤ 100
10Z – 2X – 3Y ≥ 20
X,Y,Z ≥ 0
2. A distribution center for a department store has four trucks available to deliver
products to retail stores. The company accrues shipping costs for all boxes that it
ships and losses for all boxes that cannot fit on one of the four trucks and must
be shipped later. Use the information below to construct a model formulation that
minimizes the total cost by determining the optimal number of boxes of each
product to be delivered by each truck. Each truck has a trailer volume of 1000 ft3
and a weight limit of 50,000 lbs.
38
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
3. Referring to the model formulated in the previous problem, use the Solver to find
the optimal number of boxes of each product to ship in each truck. Adjust the
values for amount, size, weight, cost of shipping, and loss if shipped late, and
use the Solver again to find the optimal solution.
4. A toy company is expanding its toy vehicle product line. The company formerly
produced only toy trains but now is expanding the line to include toy cars, trucks,
and airplanes. The amount of each type of vehicle to produce must now be
determined. The table below displays the expected production cost, sales price,
required machine hours, and required labor hours to produce a single unit of
each type of toy vehicle. It costs $200 an hour to run the machine that produces
cars, trucks, and trains and $250 an hour to run the machine that produces
airplanes. All toy assembly workers are paid a wage of $7.25 an hour. Based on
historical data, the product line manager forecasts that the demand for trains,
cars and trucks will be at least 500 units, while the demand for airplanes will be
at least 250 units. The production cost of all toy vehicles cannot exceed $10,000,
and no more than 1,000 labor hours can be spent on production. Formulate this
problem as an integer programming model that will maximize the profit earned by
the company’s toy vehicle product line.
Now use this formulation to create an Excel spreadsheet of your model. Then,
use the Solver to find the optimal number of each type of toy vehicle to produce.
39
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
6. Using the Solver model that you developed in the previous exercise, perform the
following:
a. Trace the dependents of each decision variable.
b. Trace the precedents of the target cell and the constraint cells.
7. A hardware manufacturer uses four workstations to produce nuts and bolts. The
following table provides the number of minutes required to create a batch of nuts
or bolts at each workstation:
Workstation
1 2 3 4
Nuts 2 8 7 8
Bolts 3 9 7 5
The following table lists the cost of machining a batch of nuts or bolts:
The machines at each workstation run for 16 hours a day, 5 days a week. A
minimum of 700 batches of nuts and 1000 batches of bolts must be produced
each week. Use the Solver to determine the optimal number of batches of nuts
and bolts to produce at each workstation in order to minimize the cost of
machining.
8. Suppose you have $0.97 worth of coins in your pocket. You know that you have
three times as many nickels as there are dimes. You also know that you have at
least five pennies and no more than two quarters. Use the solver to determine
the optimal number of batches of nuts and bolts to be produced per week at each
workstation in order to minimize production costs.
40
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
binary programming model, and use the solver to find the optimal combination of
projects for the capitalist to finance.
11. During each 4-hour period, a small town’s police force requires the following
number of on-duty police officers: 8 from midnight to 4 AM, 7 from 4 AM to 8 AM,
6 from 8 AM to noon, 6 from noon to 4 PM, 5 from 4 PM to 8 PM, and 4 from 8
PM to midnight. Each police officer works two consecutive 4-hour shifts.
Formulate and solve an LP that can be used to minimize the number of police
officers needed to meet the daily requirements.
12. A retailer store accepts orders made by telephone 7 days a week, from 8 a.m. to
5 p.m. The management has estimated that the following number of people is
needed daily in the call center to cover incoming orders:
The employees work 5 consecutive days per week. The salary is $100/day to
work on Monday through Friday and $150/day to work on the weekend.
Formulate the problem as an integer programming problem that minimizes the
cost of staffing the call center. Use Excel solver to optimize this problem.
13. Read hands-on exercise 12. Suppose part-time staff working 3 consecutive days
during Monday to Friday can be hired at a cost of $110/day. The higher cost
reflects the higher training and turnover costs associated with part-time
employees. The number of such staff cannot exceed 5. Extend the integer
programming model to incorporate this option. Use Excel solver to optimize the
problem.
41
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
14. A production company blends silicon and nitrogen to produce two types of
fertilizers. Fertilizer 1 contains 40% nitrogen and 60% silicon. Fertilizer 2 contains
30% nitrogen and 70% silicon. The selling price for fertilizer 1 is $70/lb and for
fertilizer 2 is $40/lb. The company can purchase up to 80lbs of nitrogen at $15/lb
and up to 100lbs of silicon at $10/lb. The company should produce at least 80lbs
of fertilizer 1 and at least 30lbs of fertilizer 2.
Identify the amount of fertilizer 1 and 2 that maximize the profits. Formulate this
problem as a linear programming problem and use Excel solver to find the
solution
15. A local bakery sells blueberry and chocolate muffins in packs of four. In a week
the bakery bakes at most 65 packs of muffins. The cost and demands per pack
are presented in the table below. It costs $.50 to hold a pack of blueberry muffins
and $.40 to hold a pack of chocolate muffins in inventory for a week. Formulate
and solve an LP to minimize total cost of meeting next three weeks’ demands.
16. A company supplies goods to three customers, each of whom require 30 units.
The company has two warehouses. Warehouse 1 has 40 units available and
warehouse 2 has 30 units available. The costs of shipping 1 unit from the
warehouse to a customer are shown below. There is a penalty for each unmet
customer unit of demand. With customer 1, a penalty cost of $90 is incurred, with
customer 2, $80, and with customer 3, $110. Formulate and solve a
transportation problem to minimize the sum of shortage and shipping costs.
17. Referring to the above problem, suppose that extra units could be purchased and
shipped to either warehouse for a total cost of $100 per unit and that all customer
demand must be met. Formulate and solve this transportation problem to
minimize the sum of purchasing and shipping costs.
18. Currently, the exchange rate is 100 yen per dollar. In Japan, you sell a product
that costs $5 to produce at a price of 700 yen. The product has an elasticity of 3.
With exchange rates varying from 70 to 130 yen per dollar, determine the optimal
product price in Japan and the profit in US dollars. Assume a Linear demand
curve. The current demand is 100.
A currency trader faces the following 1-day currency exchange problem that
involves U.S. dollars, English pounds and Japanese yen. In the beginning of the
day, he has an inventory of 40,000 dollars, 90,000 pounds and 100,000 yen. By
the end of the day, the he must have an inventory of at least 50,000 dollars,
42
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
95,000 pounds and 90,000 yen. The exchange rates are given below (for
example, one can exchange 1 U.S. dollars for 0.61 English pounds).
During the day, the currency trader exchanges the starting inventory for different
currencies to create the required ending inventory while maximizing the surplus
inventory of U.S. dollars. Formulate this problem as a linear program and use the
Excel solver to find the solution.
19. A computer company must purchase 700 customized hard drivers for the new
model that is planning to launch next year. The following table presents the
quotes received from three different vendors:
For example, Company C will require a fixed cost of $9,000 to set-up the
machines to produce the drivers. In addition, it will charge $270 per unit sold.
20. Read hands-on exercise 8.19. Reformulate the problem and re-optimize using
Excel solver under the additional constraint that no vendor is allowed to supply
more than 65% of the total number of units required.
21. A manufacturing company uses trucks to ship products from the production plant
to the warehouse. The following network represents the available routes between
the plant and the warehouse. The numbers in brackets (d, t) present the length of
the route d and the time t it takes to cross the road segment.
2 (1, 5)
(1,3)
4 (7, 12)
1 (2, 7) (6, 8)
(3, 20) 6
(3, 4)
(5, 15)
(4, 10)
3 (1, 2) 5
Find the shortest path (in terms of distance) from the plant (node 1) to the
warehouse (node 6). Formulate the problem as an integer program and use
Excel solver to find the solution.
43
Chapter 8: Using The Excel Solver To Solve Mathematical Programs
22. Read hands-on exercise 8.22. Find the longest path (in terms of time) that
connects the plant (node 1) with the warehouse (node 6). Formulate the problem
as an integer programming problem and use Excel solver to find the solution.
23. The Markowitz problem provides the foundations for single-period investment
theory. The problem is stated as follow: “Given that an investor has n assets. The
r , r ,..., r
n and the co-variances are ij σ
corresponding mean rates of return are: 1 2
for i, j = 1,…,n. The problem is to find a minimum-variance portfolio for a given
r w
fixed mean value returns ( ).” A portfolio is defined by a set of weights i , i =
1,…, n, that sum to 1 (Luenberger 1998). The following is a NLP formulation of
the problem:
1 n
Min : ∑ wi w jσ ij
2 i , j =1
Subject to :
n
∑ wi ri = r
i =1
n
∑ wi = 1
i =1
Given the covariance matrix and the rates of return for three assets, find the
minimum variance portfolio that gives an expected return equal to 0.5.
2 1 0
V = 1 2 1
0 1 2
44
Chapter 9
Simulation
Chapter Overview
9.1 Introduction
9.2 Defining Simulation
9.2.1 Data Tables
9.2.2 Scenario Manager
9.2.3 Generating Random Numbers within Distributions
9.3 Applications
9.3.1 News Vendor Problem
9.3.2 Game of Craps
9.3.3 Bidding
9.4 Summary
9.5 Exercises
Chapter 9: Simulation
9.1 Introduction
This chapter defines simulation and describes how simulation can be performed in
Excel. We illustrate some simple tools available in Excel and then discuss how to use
some new functions to create a simulation in Excel. The reader should be aware that
there are several simulation software packages available that have more advanced
features than what is available in Excel; since Excel is primarily a spreadsheet software,
simulation features are not highlighted. However, we want to demonstrate, that when
developing a spreadsheet-based DSS, simulation can be performed with the basic tools
available in Excel. We revisit the simulation topic in Chapter 20 with VBA. We have
several DSS applications which use simulation, such as Poker Simulation, Birthday
Simulation, Queuing Simulation, and Reliability Analysis.
For example, if the supervisor of a paper mill wants to determine how production will be
affected by a change in the rate of raw material input, it would be very costly to observe
the actual affects in the mill. He would have to instruct employees to change the rate of
placing raw materials in the first machine, possibly change the processing times of some
following machines, react to any malfunctions caused by this change, and possibly get
an undesirable result, thus having to change everything back to how it originally was. By
using simulation, he could see what would happen numerically without actually
modifying the actual process. If the simulation model showed an increase in production
by changing the rate of incoming raw materials, the supervisor could implement the
change; otherwise, he would not need to waste time if production did not increase.
We use Data Tables to determine how some outputs vary in response to changes in
input. Data Tables use a spreadsheet to refer to cells that may contain formulas or
functions for some output and input of a problem. For example, you may have input as
sales price and output as profit. The input cell would just contain a numerical value, but
the output cell would contain a formula that calculates profit based on quantity sold and
2
Chapter 9: Simulation
profit per unit. You could then use a Data Table to vary the price values and observe the
change in profit.
There are two types of Data Tables: one-way data tables and two-way data tables. A
one-way data table enables you to determine how changing one input will change any
number of outputs. In the above example of price and profit, price is the only input value
that needs changing, so you would create a one-way data table. It is also possible to
observe the number of customers as price values while observing profit. Since this
would only require changing one input (price) you would use the one-way data table.
Two-way data tables allow you to determine how changing two inputs affect a single
output. For example, if you varied price and location values as input, you could observe
profit. Since two inputs are changing, you would use a two-way data table.
To use Data Tables, you must first prepare your spreadsheet. To do this, create a list of
inputs and outputs. These cells may contain values or formulas. You should label them
for easy reference. Next, create a list of the various input values that you want to
experiment with. If you are creating a one-way data table, you should put these values in
a single column. If you are creating a two-way data table, you should create one column
and one row of varying input values for the two inputs of interest. Then, enter the output
formulas that you want the Data Table to calculate for observation. For one-way data
tables, these output cells (again, more than one output can be observed using one-way
data tables) should be in the columns adjacent to the input column. For two-way data
tables, this output cell (again, only one output value is observed with two-way data
tables) should be placed in the upper corner of the data table (in the cell above the
column of input value and next to the row of input values).
Once you’ve prepared your spreadsheet, highlight the entire table area that you just
created (without column or row titles). Now, select Data > Table from the Excel menu. A
small dialog box will appear that asks you for the row and/or column input cell (see
Figure 9.1).
Figure 9.1 The Row Input and Column Input cells refer to the initial list of inputs
and outputs.
If you are creating a one-way data table, the column input cell will be the only reference
you give. This reference is to the cell in your original list of inputs and outputs that
contains the current value of the input that you wish to vary. The formula you copied for
the output cell(s) should be in terms of this input cell location. If you are creating a two-
way data table, you will reference both a row and column input since you are varying two
inputs. Again, these references should be to the input cells in the initial list of inputs and
outputs. Once you have specified the input cell references, click OK and observe the
values calculated in the data table.
3
Chapter 9: Simulation
Let’s consider an example to illustrate both one-way and two-way data tables. In Figure
9.2, we provide a list of inputs and outputs for ticket sales. The Total Profit is calculated
by finding the unit profit (price minus cost per ticket) and multiplying this value by the
number of salespersons and the average number of tickets sold per person. Using the
input cells shown, the formula for the total profit would be:
=(B6-B7)*B4*B5
Figure 9.2 The initial list of inputs and outputs contains values and formulas.
The first data table we want to create will show the different profit values as we vary the
price per ticket. Since we are only varying one input, this will be a one-way data table.
Let’s begin by creating a column of various prices per ticket. We will vary these prices
from $16.00 to $24.00 per ticket. Then, we copy the formula used in cell B8 to a cell in
the first row of the data table (above the first price value in the input columns). Note that
the output formula can either be copied directly or we can just refer to the original
formula cell in the list of inputs and outputs; that is, we could again type “=(B6-
B7)*B4*B5” orsimply refer to this formula in the initial table by typing “=B8”. Figure 9.3
shows the preparation for this data table.
Figure 9.3 Preparation for the one-way data table comparing price per ticket and
total profit.
We now select Data > Table from the menu. Since we are varying price as the input, and
we have these varying values in the first column of the data table, we should specify the
location of the price value in our initial list of inputs and outputs as the Column Input; this
is, cell B6 (see Figure 9.4).
4
Chapter 9: Simulation
Figure 9.4 Using the price per ticket initial cell reference as the Column Input.
Once we press OK, Excel completes the data table. In Figure 9.5, we find that various
profit values have been calculated for each possible price per ticket listed.
Figure 9.5 The final data table shows various profit values as the price per ticket
changes.
Now suppose that we are curious to see how the combination of price per ticket and
number of salespersons affects the total profit. Since we are varying two inputs, we
should now create a two-way data table. We again begin by creating a column of varying
prices per ticket. Then, we create a row for varying numbers of salespersons (above the
first price value in the input column). Now, in the cell above the input column of prices
and next to the input row of number of salespersons, we again copy the formula for total
profit. Figure 9.6 illustrates the preparation for this data table.
5
Chapter 9: Simulation
Figure 9.6 The preparation for the two-way data table that varies both price per
ticket and number of salespersons.
We now select Data > Table from the menu. This time, we will reference both the Row
Input and Column Input. The Row Input will be the number of salespersons, which is cell
B4, in the initial list of inputs and outputs. The Column Input will again be cell B6 for
price per ticket (see Figure 9.7).
Figure 9.7 The number of salespersons is the Row Input and the price per ticket is
the Column Input.
We press OK to see the completed data table. For any combination of price per ticket
and number of salespersons (among the varying values specified), we can find the
calculated total profit (see Figure 9.8).
6
Chapter 9: Simulation
Figure 9.8 The final data table shows various profit values for combinations of
number of salespersons and prices per ticket.
The Scenario Manager allows you to vary up to 32 input cells for various values, or
scenarios, and observe the results of several output cells. The Scenario Manager will
create a Scenario Report, which shows the resulting output values for each scenario of
input values.
The spreadsheet preparation for using the Scenario Manager is simpler than that for
Data Tables. All we need is the initial list of inputs or outputs. Appropriate values and
formulas should be filled in in these cells. Now, we go to Tools > Scenarios to view the
Scenario Manager dialog box shown below in Figure 9.9.
To create a scenario, simply press the Add button. Another dialog box, in which the
inputs are selected for the scenario, will appear (see Figure 9.10). These cell references
7
Chapter 9: Simulation
should be to the list of inputs created in the spreadsheet preparation. We also name the
scenario at this time.
Figure 9.10 Creating the scenario name and selecting the input cells to change.
Next we specify the values that these inputs should take for the scenario that we are
creating (see Figure 9.11).
The scenario will now be listed in the original Scenario Manager dialog box. At this point,
we can Add more scenarios, Edit a scenario we have already created, or Delete any of
the listed scenarios. When we are done specifying the various scenarios of input values,
we can click Summary to create the Scenario Report. The Scenario Summary dialog box
asks us to select the outputs that we want to observe for the various scenarios of inputs
(see Figure 9.12). We can then choose to create a Scenario Summary, or Scenario
Report, or a Scenario Pivot Table report. These will be created as new sheets in the
current workbook.
8
Chapter 9: Simulation
Figure 9.12 Selecting the output cells to put in the Scenario Report.
Let’s consider an example to illustrate the benefits of using the Scenario Manager. In the
table shown in Figure 9.13, there is a list of inputs for a company’s sales. These inputs
are: tax rate; Year 1 sales; sales growth; year 1 price; year 1 cost; interest rate; cost
growth; and price growth. Then, there is a table of outputs for five years. The outputs
are: unit sales; unit price; unit cost; revenue; costs; before tax profits; tax, after tax
profits; and total NPV. The unit sales, unit price, and unit cost are actually inputs
calculated using the growth rates from the input table.
For this example, let’s focus on the company’s after tax profits for each of the five years
as well as their total NPV. We want to consider three different scenarios for year 1 sales,
sales growth, and year 1 price and choose various values of these inputs for Best,
Worst, and Most Likely scenarios. We begin by adding a new scenario (see Figure 9.14).
9
Chapter 9: Simulation
Figure 9.14 Naming the Best Case scenario and selecting the changing input cells.
We select the input cells, or changing cells, for the year 1 sales, sales growth, and year
1 price: C5:C7. We name this scenario the Best Case scenario and give the values
20,000, 0.2, and 10 for these inputs, respectively (see Figure 9.15). Then, we press Add
on this dialog box to directly create the next scenario.
Figure 9.15 Specifying the values for the Best Case inputs.
Let’s name this the Worst Case scenario and select the same input cells. We give them
the values 5,000, 0.02, and 5, respectively.
Finally, we create the Most Likely scenario using the same input cells with the values
10,000, 0.1, and 7.5. Figure 9.16 illustrates the list of scenarios that we have created in
the Scenario Manager dialog box.
10
Chapter 9: Simulation
We now choose Summary and select the “after tax profit row” (for all five years), which is
in cells C21 and G21, and the NPV cell, which is cell C23, as the outputs (see Figure
9.17). We then press OK to create the Scenario Report.
Figure 9.17 Selecting the output cells to create the Scenario Report.
The Scenario Report (see Figure 9.18) exhibits the output cells, or result cells, for each
scenario as well as the current values from the initial tables. Now, we can easily observe
the NPV of the Best Case, Worst Case, and Most Likely scenarios by looking at the last
row of the Scenario Report. This is especially helpful if output and input cells are spread
out on the spreadsheet, since results are grouped together in the report.
11
Chapter 9: Simulation
Figure 9.18 The Scenario Report shows the outputs for the various input values in
each scenario.
Note that we have named our input and output cells in order to see the names shown in
the first column of the Scenario Report. If you do not name your cells, the cell reference
will be shown instead (i.e. C23 instead of “NPV”). Also note that some icons are
provided next to the row numbers and column letters to hide some parts of the report
and to show others.
Despite its benefits, creating many scenarios of possible input values with Data Tables
or the Scenario Manager can be tedious. We will therefore explain how to use some
important Excel functions to generate various input data values for several scenarios, or
runs of a simulation.
Simulation is useful because it can handle variability of parameters in a model. That is,
you may not know many settings in a process with full certainty; you may be aware of a
range of numbers into which values fall, but not know the exact figures. For example,
assume that there is a shipment company that is dependent upon a variety of suppliers
before its workers can organize and ship their products. The company’s staff knows
which suppliers will be delivering goods within a few days, but they do not know the
exact day or time when each delivery will arrive. This variability affects their business,
since they cannot satisfy their customers’ demands until they receive products from their
suppliers. To put this situation into a model, we would not be able to assign a constant
rate to the arrival of goods from suppliers. Therefore, we would want to study the arrival
rates over a time and match the distribution to the arrival rate that closely matches the
suppliers’ patterns. Once we know the distribution of a certain parameter, we can
generate random numbers within this distribution to observe the effect on satisfying
customer demand. Therefore, it is now appropriate to introduce the concepts of
distributions and random numbers.
In Chapter 4, we mentioned that the RAND and RANDBETWEEN functions are used to
generate random numbers. The RAND function does not have any parameters; it returns
a randomly chosen fractional number between 0 and 1. The RAND formula is:
12
Chapter 9: Simulation
=RAND()
RAND is useful for generating test data or any data with random patterns. You can
manipulate this RAND value if you want to generate values outside the interval between
0 and 1. If you want to generate numbers between 1 and n, multiply this RAND value by
(n-1) and add 1.
=RAND()*(n-1) + 1
To generate a random number between some lower and upper bound, replace 1 with the
lower bound and n with the upper bound in the above formula. We demonstrate this
particular use of RAND in in Figure 9.19 with n = 10. Here we want to generate heights,
widths, and depths to calculate some probable packaging volumes. We type the
following function to create random numbers between 1 and 10:
=RAND()*9 + 1
Figure 9.19 The RAND function can be manipulated to change the range of the
random numbers produced. Here the range is changed from the default of 0 to 1, to 1 to
10.
We can convert these random numbers into random integers by changing the format of
the cells in which we entered this function. Simply use the Number tab of the Formatting
dialog box and set the number of decimal places to 0.
We can also generate a random integer by using the INT function with the RAND
function. The INT function rounds a number down to the nearest integer.
=INT(number)
=INT(range_name)
=INT(cell_referenced)
To generate random integers, we can apply the INT function to the RAND function by
typing:
=INT(RAND())
13
Chapter 9: Simulation
=INT(RAND()*(n-1) + 1)
For example, the following formula would generate the numbers 1, 2, 3, 4 (random
integers between 1 and 5):
=INT(RAND()*4 + 1)
Note that the integer 5 would never be generated using this formula since the INT
function always rounds down to the nearest integer. That is, the random number 1.6
would be transformed to the integer 1. You could also use the TRUNC function to create
an integer by removing the decimal part of a number without rounding. The TRUNC
function is used similarly to the INT function.
An easier way to manipulate the RAND function is to use another function called
RANDBETWEEN. This function takes two parameters, which are the lower and upper
limits of the range. The format of this function is the following:
=RANDBETWEEN(lower_limit, upper_limit)
Note: to use the RANDBETWEEN function, you will need to ensure that the Analysis
Toolpack has been selected as an Add-In. Go to Tools > Add-Ins to check.
Using the same example, if we wanted to create random numbers between 1 and 10,
we would type the following:
=RANDBETWEEN(1,10)
Note: When using either RAND or RANDBETWEEN, the numbers generated will
change with every operation performed in the worksheet. That is, after creating the table
in Figure 9.19, if we were to then type new text or data into another cell on the same
worksheet, the initial random numbers shown would automatically recalculate new
random numbers in our range. To prevent this automatic recalculation from occurring,
go to Tools > Options from the menu and, on the Calculation tab, check Manual. We
can now use F9 to recalculate the random numbers if necessary. (Note, however, that
some other functions may not recalculate automatically when copied after changing the
Calculation option to Manual.)
The RAND and RANDBETWEEN functions can also be used with the distribution
functions discussed in Chapter 7. The general distribution function has the following
format:
For these parameters, the x-value is the number for the distribution. The cumulative
value is TRUE if we use the cumulative distribution function (cdf), or FALSE if we use
the probability mass function (pmf). However, to generate a random number within a
given distribution, we must use the inverse functions of these distribution functions.
These inverse functions return the inverse of the cumulative probability functions. That
is, if probability = DIST(x,...), then DISTINV(probability,...) = x.
14
Chapter 9: Simulation
Some of the inverse functions of the more common distributions are BETAINV,
BINOMINV, LN or LOGINV (the Exponential inverse), and NORMINV. We can also find
these functions in the Statistical category of the list of functions when we choose Insert >
Function from the menu. The format of these inverse functions is:
=DISTINV(probability, distribution_parameters)
The probability parameter is a number between 0 and 1 associated with the given
distribution. We will use the RAND function as the value for this parameter to generate a
number between 0 and 1. For example, to generate random numbers from theNormal
distribution, we would follow the next format:
In Figure 9.20, we have entered this function in a column of cells for a Normal
distribution with a mean of 50 and a standard deviation of 15. The function is entered as
the following:
Note that most of the numbers generated are in the range between 35 (that is, 50 – 15)
and 65 (that is, 50 + 15), with the majority being closer to 50.
Figure 9.20 To generate a random number within a distribution, we use the RAND
function as the first parameter of the inverse distribution functions.
15
Chapter 9: Simulation
Summary
Scenario Analysis: Performs all possible alternative actions and notes the
varying results from these different situations.
9.3 Applications
We will now explain three common applications of simulation in Excel. To illustrate
these examples, we will use the ideas of scenario analysis and random number
generations in distributions. Please note that there are many other areas and models for
which simulation is a beneficial tool besides the following.
A bookstore must determine how many 2006 comic calendars to order in September of
2005. It costs $2.30 to order each calendar, and the store sells each one for $4.70. After
January 1, 2006, any unsold calendars can be returned to the supplier for a salvage
value of $0.75 each. Our best guess is that the number of calendars demanded is
governed by the following probabilities:
Demand Probability
150 0.3
200 0.3
250 0.4
So, how many calendars should the company order? Let’s set up our spreadsheet by
listing the inputs and outputs. For inputs, you know the ordering cost, sales price, and
salvage value. You are also provided with a table of possible demands with their
corresponding probabilities (see Figure 9.21).
16
Chapter 9: Simulation
Figure 9.21 Preparing the spreadsheet using formulas to determine unknown values.
The expected number of magazines sold will be based on these demands and
probabilities. First, we compute the cumulative probabilities of each demand (since
demands are increasing). We then create a cell that will generate a random probability;
we do this using the RAND function. To determine the expected number of magazines
sold, we use the IF function to compare this random probability to the ranges of
cumulative probabilities. We have named the cell ranges to make our formulas easier to
read (see Figure 9.21).
We will then use the NORMINV function, combined with the RAND function, to
determine how many calendars to order (see Figure 9.22). We set the mean to 200
(since this is the average demand) and the standard deviation to 15 (even though the
standard deviation of the demand value is 50, we want to keep the order number closer
to the mean).
Next, we list the outputs; these are costs, sales revenue, salvage revenue, and profit.
The costs are equal to the number ordered times the ordering cost. The sales revenue is
the expected number sold times the sales price. The salvage revenue is the number
returned times the salvage value. The number returned is considered to be the
difference between the number ordered and the expected number sold; however, if the
number sold is greater than the number ordered, this value is 0. The profit is then the
sales revenue plus the salvage revenue minus the costs (see Figure 9.22).
17
Chapter 9: Simulation
Figure 9.22 Using the NORMINV function with the RAND function to generate a
random number from the Normal distribution.
18
Chapter 9: Simulation
Figure 9.23 Making runs to compare profit values for several random input values.
This table displays 20 runs, or 20 rows of various input generated from changing random
values. From the values stored in each run, we can easily find the average profit and
average number of calendars ordered.
In the game of Craps, a player rolls two dice. If the first roll yields a sum of 2, 3, or 12,
the player loses. If the first roll yields a sum of 7 or 11, the player wins. Otherwise, the
player continues rolling the dice until she matches the value thrown on the first roll or
rolls a sum of 7. Rolling a match for the first roll wins the game, rolling a value of 7
before a match loses the game. How many times will a player win on average in 1, 2, 3,
4, or 5 rolls?
To answer this question, we begin by listing the known inputs in a spreadsheet (see
Figure 9.24). We already understand the conditions for winning and losing on one roll
and on multiple rolls. Knowing that the player rolls two dice, we can calculate the
probability of winning or losing on the first roll. However, if the player makes multiple rolls
in the game, we do not know exactly how many rolls will be needed.
19
Chapter 9: Simulation
Figure 9.24 The conditions for winning and losing on one or multiple rolls.
Let’s determine possible outcomes for 1, 2, 3, 4, and 5 rolls in a game. To do so, we will
generate random results for rolling two dice for each roll for 20 games, or runs. To
generate the result of a roll, we simply use the RANDBETWEEN function with the
interval (2, 12). Then, we use the IF function to check the conditions for winning and
losing and output the player’s status after the roll. For the first roll, we can use the
following IF function (these cell references are for the first row in the simulation table):
=IF(OR(C26=2,C26=3,C26=12),"Lose",IF(OR(C26=7,C26=11),"Win",
"Continue Play"))
The IF function, along with the OR function, checks if any of the losing conditions or any
of the winning conditions are met for the first roll. If the player neither wins nor loses,
then she continues to play (see Figure 9.25). We should also use Conditional Formatting
to identify cells with “Win” values, since this will helpanswer the question stated in the
problem description.
20
Chapter 9: Simulation
Figure 9.25 Recording the outcome of rolling two dice for 20 different games.
For all sequential rolls, we again use the IF function, but this time, we are checking the
current roll with the first roll. Therefore, we will need to create a new column for the
second roll values (again using the RANDBETWEEN function) and compare these
results with the results of the first roll value (see Figure 9.26). The comparative function
with new winning and losing conditions can then be written as follows (these cell
references are for the first row in the simulation table):
=IF(D26="Continue Play",IF(E26=$C26,"Win",IF(E26=7,"Lose",
"Continue Play")),"")
Now, we can create two more columns (one for the roll value and one for the
comparative function) for the third, fourth, and fifth rolls (see Figure 9.26). Notice that we
have used column absolute referencing to refer to the result of the first roll; this way we
can copy and paste the two columns for each roll. We have also used Conditional
Formatting on the sequential columns with comparative functions to identify the “Win”
values more easily.
21
Chapter 9: Simulation
Figure 9.26 Recording the number of wins when there are 1, 2, 3, 4, or 5 rolls in a
game.
Now, we create a summary table below the simulation table. We want to count the
number of wins for each roll among 20 games, or runs. To perform this conditional
counting, we use the COUNTIF function. We will discuss this function in more detail in
Chapter 10 with Database functions. The structure for this function is as follows:
=COUNTIF(table_range, condition)
For counting the number of wins in the first roll, we use this function:
=COUNTIF(D26:D45, “Win”)
We copy this formula to calculate the number of wins in each roll for all 20 games. What
we have done is simply taken each of these resulting numbers and divided them by 20
to find the percentage of games won for each roll. (We can also press F9 to generate
new rolls and simulate another 20 games, or runs, of data.)
9.3.3 Bidding
A contractor is planning to make a bid on a construction project. She believes that it will
cost $12,000 to complete the project. Three competitors are going to bid against her.
Based on past history, she believes that each competitor’s bid is equally likely to be any
value between her cost and triple her cost of completing the project. She also believes
22
Chapter 9: Simulation
that each competitor’s bid is independent of the other bids. Which bid will maximize her
expected profit?
We begin by listing the known inputs. We know that the cost estimate is $12,000.
Suppose we also know that the competitors will bid somewhere between this cost and
three times the cost. To estimate these values, we use the RAND function and create
the interval (cost, 3*cost). As described in the previous section, we can use these lower
and upper bound values to generate random numbers as follows:
Note that we could have also used the RANDBETWEEN function with this interval;
however, we want non-integer values. Once we have generated possible bids for the
competitors, we must determine possible bids for the contractor. She assumes that she
will bid anywhere between $12,000 and $20,000 for the job. In this case, we have used
the RANDBETWEEN function since it is just an estimate.
Now we can determine the output, which is the profit, by comparing the contractor’s bid
to the competitors’ bids. If the contractor’s bid is the minimum value, then her profit is her
bid minus her cost; however, if her bid is not the minimum, then she will lose the bid and
not gain any profit (see Figure 9.27).
Figure 9.27 Calculating the inputs and output using several formulas.
If we limit these formulas to determine which bid will generate the maximum profit, we
will have to generate several random numbers (by pressing F9) and keep track of the
profit and bid values for comparison. Several simulation runs would accomplish this in a
more efficient manner. In Figure 9.28, we have repeated the above formulas for 20 runs
of the simulation. We then have several instances of the contractor’s bid and the
competitors’ bids, and for each instance we can calculate the profit.
23
Chapter 9: Simulation
When the 20 runs are complete, we can determine the max profit value by using the
MAX function and the corresponding bid value. To find this bid value, we use the
VLOOKUP function; however, since we are looking for the bid value based on the
location of the max profit value, we must have the profit column in the first column of the
lookup table. Therefore, we simply copy the bid column to the right of the profits and only
select the data in columns F, G, and H as the lookup table. We can repeat this 20-run
simulation by generating a new set of random numbers (by pressing F9) to compare the
max profit and bid values.
9.4 Summary
¾ Simulation is a modeling tool used for analyzing a process running under
different parameters.
¾ Simulation is used not to find the best result given certain inputs, but rather to
find the best inputs given a desired result.
¾ Scenario Analysis performs all possible alternative actions and notes the varying
results from these different situations.
¾ To perform random number generation in distributions, use the RAND() function
as the first parameter in the inverse distribution functions.
¾ Inverse Distribution functions are the following: NORMINV, LN (the Exponential
inverse), BETAINV, BINOMINV.
¾ Applications of simulation include the News Vendor Problem, Cash Flows, the
Game of Craps, and Bidding.
24
Chapter 9: Simulation
9.5 Exercises
9.5.1 Review Questions
1. What is a simulation?
2. How do simulation models differ from optimization models?
3. How do you activate the Scenarios function?
4. How is the Scenarios function a useful tool?
5. Name four of the distribution functions under the Statistical category of functions
in Excel.
6. Name four of the inverse distribution functions under the Statistical category of
functions in Excel.
7. How can you generate a random number within a distribution?
8. Which inverse distribution function is appropriate to use for generating a random
number within an Exponential distribution?
9. If given a gamma distribution with an alpha value of 5 and a beta value of 8, what
is the appropriate functional expression to generate a random number?
10. Give an example of an application of Excel simulation.
3. In chapter 4, problem 3, you completed the following table, which determines the
height of a cylinder given its volume and radius. Use scenario analysis to
complete the table again for each of the following cylinder volumes: 10, 15, 20,
25, and 30 ft^3. Recall that the height of a cylinder can be computed with the
following formula: Height = Volume/(Radius^2).
Volume (ft^3): 10
Radius (ft) Height (ft)
5
2
4
3
25
Chapter 9: Simulation
4. A bakery receives its ingredients from one of three vendors. Each vendor under
consideration charges different prices for each different ingredient. The tax rates
vary by the state in which the vendors are located, and the shipping costs vary by
the distances the supply must travel. The table below displays the prices, tax
rates, and shipping costs per pound for each vendor. The management has to
choose a vendor for each ingredient. It has been estimated that 200 lbs of
ingredients will be needed per month. Use scenario analysis to help the
management to choose a vendor for each ingredient.
26
Chapter 9: Simulation
7. Suppose that you have just been hired by Sturdy Boxes, Inc. to manage the
production floor. On your first day, you realize that the garbage compactor is full
of cardboard boxes. After questioning the workers, you find out that those boxes
were not cut and shaped correctly in the Flex-O machine. The problem is that
the Flex-O machine’s blades are not replaced quickly enough after it has been
discovered that misshapen boxes are being produced. After studying daily
production reports, you determine that the time for the first failure to occur follows
a normal distribution with an average of four hours and a standard deviation of 15
minutes. If the time to process each box has a normal distribution with an
average of 30 seconds and a standard deviation of three seconds, how many
boxes should be processed before the blades on the Flex-O machine should be
replaced? Use ten scenarios with thirty runs each and find the minimum value to
approximate the number of boxes to produce before replacing the blade. Your
objective is to use each blade as long as possible but change it before failures
start to occur.
8. A PVC pipe manufacturer has recently approximated the effect of certain factors
on the amount of time it takes to produce a certain length of PVC pipe. These
factors include the pressure and temperature of the liquid PVC and the radius
and thickness of the pipe. The time that it takes to produce PVC pipe is shortest
when the liquid PVC pressure is at 20 psi, the liquid PVC temperature is 240
degrees Fahrenheit, the radius is at its minimum setting of one-half inch, and the
thickness of the pipe is at its minimum setting of one-eighth inch. The time to
produce a 12-foot pipe at these settings follows a normal distribution with a mean
of 85 seconds and a standard deviation of six seconds. Below are the
approximated effects of each factor on the mean time necessary to produce a
12-foot PVC pipe. Note that P is the liquid PVC pressure setting, T is the liquid
PVC temperature setting, R is the radius of the pipe, and TH is the thickness of
the pipe.
Use scenario analysis to find the approximate time to produce a 12-foot PVC
pipe for each of the following settings:
27
Chapter 9: Simulation
manager would like to order all of the products at an appropriate time so that they
will arrive in time to meet demand without accumulating too much. After studying
past trends, the distribution manager approximated the distributions of the
demand rate and supply time for one of the products as shown in the following
table. Assume that the product is perishable and only good for the month in
which it is ordered. Also assume that the product is ordered in batches of 100.
The supply time follows a normal distribution with the following parameters:
Month Demand
January ~Norm (20000, 3000)
February ~Norm (17500, 4500)
March ~Norm (13000, 1900)
April ~Norm (9500, 1100)
May ~Norm (7800, 900)
June ~Norm (7000, 600)
July ~Norm (7400, 700)
August ~Norm (8500, 950)
September ~Norm (9000, 1250)
October ~Norm (10000, 2500)
November ~Norm (15000, 4000)
December ~Norm (25000, 1500)
a. Use ten runs to find the approximate number of batches to order to meet the
demand and the approximate number of days to allow for delivery so that the
delivery will arrive in time for the next month.
b. Perform scenario analysis to find the results for each of the following
distributions if the time of supply arrival has a Normal distribution with the
following parameters:
Standard deviation = S
i. A = 15, B = 160, S = 5
ii. A = 25, B = 220, S = 2
iii. A = 12, B = 210, S = 4
10. A local company currently sells 40,000 units of a product for $45 each. The unit
variable cost of production for the product is $5. The company is thinking about
cutting the product price by 30%. They are sure this will increase sales by an
amount between 10% and 50%. Perform a sensitivity analysis to show how profit
will change as a function of the percentage increase in sales (ignore fixed costs):
28
Chapter 9: Simulation
11. A GMC dealer believes that demand for 2005 trucks will be normally distributed
with a mean of 200 and a standard deviation of 30. His cost of receiving a truck is
$25,000, and he sells each truck for $40,000. Half of all leftover trucks can be
sold for $30,000. He is considering ordering 200, 220, 240, 260, 280, or 300
trucks. How many should he order?
12. The game of keno is played as follows: 80 balls (numbered 1 to 80) are mixed up
and 20 balls are randomly drawn. Before the 20 balls are drawn, the player
chooses 10 different numbers. If at least 5 of the numbers are drawn, the player
wins. What is the probability that the player will win?
13. Suppose you are bidding for an oil well that you believe will yield $40 million
(including the cost of developing and mining the oil) in profits. Three competitors
are bidding against you and each competitor’s bid is assumed to follow a normal
random variable with a mean of $30 million and a standard deviation of $4
million. What should you bid (within $1 million)?
14. Two basketball teams are ready for the best-of-seven NBA finals. The two teams
are evenly matched, but the home team wins 60% of the games between the two
teams. The sequence of home and away games is to be chosen by Team A.
Team A has the home edge and will be the home team for four of the seven
games. They have the following sequence choices for the home games:
a. Sequence 1: Team A, A, B, B, A, B, A
b. Sequence 2: Team A, A, B, B, B, A, A
Use simulation to prove that each sequence gives Team A the same chance of
winning.
15. The game of Chuck-a-Luck is played as follows: You pick a number between 1
and 6 and toss three dice. If your number does not appear on any of the dice,
you lose $1. If your number appears x times you win $x. On the average, how
much money will you win or lose on each play of the game?
16. In August 2004, a car dealer is trying to determine how many 2005 cars should
be ordered. Each car ordered in August 2004 costs $10,000. The demand for the
dealer’s 2005 models has the probability distribution shown in the table below:
Each car sells for $15,000. If the demand for 2005 cars exceeds the number of
cars ordered in August 2004, the dealer must reorder at a cost of $12,000 per
car. Excess cars may be disposed of at $9,000 per car. Use simulation to
29
Chapter 9: Simulation
determine how many cars should be ordered in August 2004. For your optimal
order quantity, find the expected profit.
17. In the above problem, suppose now that the demand for cars is normally
distributed with a mean of 40 and a standard deviation of 7. Use simulation to
determine an optimal order quantity. For this optimal order quantity, determine
the expected profit.
18. A ticket from Miami to Orlando sells for $150. The plain can hold 100 people. It
costs $8,000 to fly an empty plain. The airline incurs variable costs of $30 per
person on the plane. If the flight is overbooked, anyone who cannot get a seat
receives $300 in compensation. On the average, 95% of all people who have a
reservation show up for the flight. To maximize the expected profit, how many
reservations for the flight should be taken?
20. A person plays a game in which 2 fair dices are thrown. If the total outcome is 7
or 11, the person wins the game. Use simulation to find the probability of a win.
21. Jane is buying a house that costs $210,000. A financial institution is willing to
lend her the money. The bank provides different rates depending on the amount
of down payment and the duration of the loan. The table below presents the
interest rate for different loan durations and amount of down payment. Use two-
way Data Tables to calculate the monthly payments that Jane will be making to
pay-off the loan under each scenario.
22. A drive-through only, fast food restaurant is considering hiring new employees. It
has been estimate that each additional employee will reduce the service time by
10%. The following data have been collected (the times are in minutes):
30
Chapter 9: Simulation
a. Simulate the arrival and service time of 100 customers using the information
provided above.
b. Estimate the expected waiting time per customer, expected total time in the
system, the longest waiting time, and the percentage of time the server (drive
through window) is idle.
c. Assume that one additional employee was hired. Recalculate the expected
waiting time per customer, expected total time in the system, the longest
waiting time, and the percentage of time the server is idle.
23. The coffee shop in a university town faces the problem of many bagels to order
per day from a nearby bakery. For planning purposes, the bakery requires that a
fixed amount of bagels be ordered every day in a given month. At the end of the
month, the order size may be changed.
Based on past observations, the coffee shop manager has identified the following
distribution of daily demand for bagels for the current month:
Sales Frequency
20 30
25 55
40 15
It costs $0.1 to buy a bagel from the bakery, regardless of the quantity ordered.
The bagels are sold for $1.05. Left over bagels are disposed at the end of the
day. The manager is interested to identify an order size for the current month that
would maximize the profits.
Randomly generate the demand for the 30 days of the current month using the
distribution given above. Consider order sizes from 20 to 40. For each order size,
31
Chapter 9: Simulation
calculates the corresponding profits. Based on the results of the simulation what
should the order size for the current month be?
24. The tables below present the distribution of the inter-arrival time and service time
of the jobs in a service center.
Estimate the expected waiting time per job, expected total time in the service
center, and the longest waiting time for 100 jobs under the following two different
service rules:
First In First Out (FIFO) – jobs are served in the order they arrive.
Last In First Out (LIFO) – the last job to arrive
32
Chapter 10
Chapter Overview
10.1 Introduction
10.2 Importing Data
10.2.1 Text Files
10.2.2 Web Addresses
10.2.3 Databases
10.3 Exporting Data
10.4 Creating Pivot Tables from External Data
10.5 Using Excel as a Database
10.5.1 Sorting
10.5.2 Filtering
10.5.3 DFunctions
10.5.4 Data Validation
10.5.5 Data Consolidation
10.6 Summary
10.7 Exercises
Chapter 10: Working with Large Data
10.1 Introduction
This chapter explains how to work with external data or large data in Excel. We illustrate
how to import data from text files, webpages, and databases. For the database
discussion, we assume the reader has some previous knowledge about database
software, such as Microsoft Access. Knowing how to import data to the spreadsheet
environment is an important attribute for developing a DSS. In many real-life
applications, large amounts of data will be stored in a separate database; this data has
to be imported as input for the model in the DSS. We revisit this topic in Chapter 21 to
illustrate how this can be done using VBA. We have developed several DSS applications
which use these features, such as the University Information System application.
To import data from a text file, use the Text Import Wizard. If you try to open a text file in
Excel, this Wizard will appear. There are three simple steps to follow to use the Wizard.
The first step is to specify how you want to organize your data into columns for Excel.
There are two main options: Fixed Width and Delimited. If you choose Fixed Width,
Excel will guess how to separate your data into columns. The second step of the Wizard
will then give you a chance to modify this guess. If you choose the Delimited option, you
specify what character will be used as a separator, for example: commas, spaces, tabs,
etc. This character choice would be done in the second step of the Wizard with the
Delimited option. The third step of the Wizard in both cases is to specify any particular
numerical formatting to be applied to the data as it is imported into Excel. Let’s take a
closer look at the Text Import Wizard through an example:
Figure 10.1 presents a text file that records the number of hours ten employees worked
for three consecutive weeks. The file records each employee’s start date and hours
worked for three weeks. This file has been saved as a “.txt” file.
2
Chapter 10: Working with Large Data
The supervisor of these employees may be interested in calculating the average hours
worked per employee and the average hours worked every Monday. To take advantage
of Excel tools and functions that will help her accomplish this, she must import this text
file into Excel. After ensuring the above data is saved as a text file, we open Excel and
choose File > Open from the menu; then select the above text file. The Text Import
Wizard will appear (see Figure 10.2).
For the first step of the Wizard, we will choose the Delimited option. We notice that tabs
separate the text file data. Excel may have guessed this if we had selected the Fixed
Width option, but it is still important to ensure that tabs are used (notice that “/” marks
are also used in the dates which might be considered as a separator in Excel’s guess).
In the second step of the Wizard, we select “Tab” as the delimiter (see Figure 10.3). We
could try other options to preview the columns that would appear. For example, we could
check “Other” and enter the “/” mark in the adjacent space to preview an incorrect
separator for the data.
3
Chapter 10: Working with Large Data
Next, we go to the third step of the Wizard and specify any numerical formatting for the
data (see Figure 10.4). For this text, the second, forth, and sixth columns should be
entered as dates. So, we select each of these columns in the preview area and choose
“Date: MDY” as the format option. The other columns can be kept in “General”
formatting.
4
Chapter 10: Working with Large Data
We are now done with the Text Import Wizard; the text data should now be imported into
Excel columns, as shown in Figure 10.5.
Figure 10.5 The text file data imported into Excel columns.
The employer can now use the Excel AVERAGE function to compute the averages that
she was looking for (per employee and per date). She can also organize her data by
adding column headings and other formatting (see Figure 10.6).
5
Chapter 10: Working with Large Data
To import data from a webpage, use the Data > Import External Data > New Web Query
option from the menu. In the dialog box that appears, you can enter the web address
that contains the data you want to import. There will then be a preview of this website in
the window. You can then select what sections of data you want to import; that is, you
may not want to import everything on the webpage. The possible data sections will be
marked by small arrows. Select the arrows for all the data you are interested in and then
click Import. Excel will prompt you to select a cell in the worksheet for where you want to
place this imported data. Excel will import the data and separate it into columns, as done
on the webpage. There is no prompt for how to separate this data, but Excel usually
makes a good guess. You can always reorganize your data in the spreadsheet, if
desired.
Let’s consider an example. The following web address contains current stock quotes and
information for MSFT stock (see Figure 10.7), reported by Quicken:
http://www.quicken.com/investments/quotes/?p=MSFT
6
Chapter 10: Working with Large Data
Suppose we are interested in maintaining a worksheet with this data in order to compare
the company values or to analyze how a possible portfolio of this stock is doing. To
import this data, we first copy the web address of this page. Then, we select Data >
Import External Data > New Web Query from the menu and simply paste the web
address into the “Address” window of the dialog box (see Figure 10.8).
7
Chapter 10: Working with Large Data
In the above webpage preview, we can see the small arrows in yellow boxes that Excel
has placed on different sections of data. Since we are only interested in the actual stock
data, we select the arrow next to “Last Trade.” Notice as you place your cursor on this
arrow that a square appears around the entire section of data associated with that arrow.
After we click the arrow, it will become a check mark, indicating that we wish to import
this data. Now that we are done selecting data, we click Import. Finally, we must specify
where in the worksheet we would like to paste the data (see Figure 10.9).
8
Chapter 10: Working with Large Data
The data has now been transferred to the worksheet, as seen in Figure 10.10. Excel has
also formed appropriate columns.
We can now further format this data for future analysis. We have added a title to the
table and left a place to enter the date and time when this query was made (see Figure
10.11). We may then keep a record of “Last Trade” values, or others, as we continue to
repeat this query in the future. To repeat a query, use the Refresh Data option from the
Data menu. Simply select the range of queried data in the worksheet and select Data >
Refresh Data from the menu. Any changes on the webpage will be reflected on your
worksheet.
10.2.3 Databases
To import data from a database, choose Data > Import External Data from the menu.
From here you can browse to find a database file with which you want to work. These
files do not need to be other Excel files; they can be any Data Access Object or ActiveX
Data Object. The options available to you at this point will all involve an understanding of
databases.
To illustrate the concepts in this section, we will use a previously created database in
Microsoft Access. This database is titled Books and has a short record of a number of
books. The fields are: ISN, Title, Author, and Copyright Year. To work with this data in
9
Chapter 10: Working with Large Data
Excel, we prepare a new worksheet. First, we choose Data > Import External Data from
the menu; the window in Figure 10.12 will appear.
Figure 10.12 To import data, choose Data > Import External Data from the menu and
specify where to place the data.
We then specify where we want the imported data to be located. In this case, we
choose a cell in the worksheet, say B4, and press OK. The imported data will then be
copied to the worksheet, as illustrated in Figure 10.13. Notice that an External Data
toolbar automatically appears next to the data. The first two options on the toolbar are
Edit Query and Data Range Properties. Edit Query allows us to modify what part of the
database we have imported. A query is a search for a particular set of data from the
database; it is similar to filtering. The Data Range Properties option gives us the
opportunity to fine-tune the data before us. We can also update the data by using the
icon. The icon will ensure that any changes that have been made to the original
database are reflected in our Excel worksheet.
Figure 10.13 The data has now been imported into the worksheet in Excel.
A more detailed way to run a query on this database using help from Excel is to run the
query before we import the data. To do this, from Excel, choose Data > Import External
Data > New Database Query. You will then see the window in Figure 10.14, which
configures the data source for the query. Since we are using a database in Microsoft
Access, the only change we need to make here is to select MS Access Database from
the list of Databases; then we press OK.
10
Chapter 10: Working with Large Data
Figure 10.14 To choose a data source, we need to specify the type of database we are
using.
The next window will provide a list of files that match this data source type (see Figure
10.15). In the list, we should find the file, books.mdb; we select it and press OK. Now,
we begin to define our query.
Figure 10.15 Selecting books.mdb from the list of files that match our data source.
The first step in defining our query is to specify which columns of data that we want to
appear in our final table. We click the + sign next to the table name, tblbooks, to see a
list of all the column titles. In figure 10.16 we selected the Title, Author, and Copyright
Year columns. We click Next to arrive at the next step.
11
Chapter 10: Working with Large Data
Figure 10.16 Selecting which columns of data we want to appear in our final table.
Now, we specify how we want to filter this data. This is the query definition (see Figure
10.17). First, we select the column title, or field, by which we want to filter our data.
Then, we select an equality type and a value from that field. In this case, we decide to
filter the data by selecting all entries whose Copyright Year is equal to 2000 and click
Next. Note that we can have multiple filters among each available field.
Figure 10.17 Filtering data is how to define a query. We can select one or more
fields, equality and a value from the field(s) by which we want to filter the data.
Then, it is necessary that we choose how to sort the data in the final table (see Figure
10.18). We have decided to sort by the Author in Ascending Order. Again, note that we
can have multiple sorts; Section 10.4.1 presents an example of multiple sorting.
12
Chapter 10: Working with Large Data
Figure 10.18 Specifying how to sort the data in the final table.
After pressing Next, we arrive at the last step in creating our query. This step requires
us to decide whether we want to place the data from this query in Excel, view it in its
original data source, or create an OLAP cube. The first two options are the most
commonly used; we will view our data in Excel (see Figure 10.19). To view the final
table, we click Finish.
After we pick a place in our worksheet to situate the table, the result of the query is
transferred to Excel. We can format this data in a table and use it in our worksheet as
13
Chapter 10: Working with Large Data
needed (refer to Figure 10.20). Notice here that only the books with Copyright Year of
2000 are shown in the table and that all of the books are sorted alphabetically by Author.
Figure 10.21 The three exporting options available with the AccessLinks Add-In appear
at the bottom of the Data menu.
14
Chapter 10: Working with Large Data
Figure 10.22 Choosing external data as the source of the pivot table.
We will then be prompted to select the actual data (see Figure 10.23). To specify the
data type and location, we click Get Data.
Figure 10.23 Getting the actual data to use for the pivot table.
We will use some example data from a Microsoft Access database file. We select MS
Access Database from the list of options (see Figure 10.24).
15
Chapter 10: Working with Large Data
We are now prompted to choose the data within our data source to use for the pivot
table (see Figure 10.25). In this example database, we have several tables and queries
of data for a University System Database. Let’s look at the data in the Student Table to
make an analysis of the number of students of each nationality and gender by using a
pivot table. We select the Student Table from the left window and click the + icon. Then,
we select the field names, or column titles, that we are interested in and click the > arrow
to move them to the right window. All of the fields listed in the right window will be
imported to the pivot table.
Figure 10.25 Selecting the table fields to be imported to the pivot table.
We are now ready to return to the Pivot Table Wizard. Just as we did in Chapter 6, we
specify the layout and location of the pivot table along with any options (see Figure
10.26). Figure 10.27 presents the final pivot table. We can see the number of students
per nationality and per gender for each department.
16
Chapter 10: Working with Large Data
Figure 10.27 The final pivot table with the external data.
10.5.1 Sorting
17
Chapter 10: Working with Large Data
The first database function we will discuss is sorting. To sort means to order all entries
in a database by a particular field; a field is a category name. (In Figure 10.28, each
column heading of the table is a database field name.) In the example shown in Figure
10.28, we see that the names of the students are in no particular order. To place the
names in alphabetical order, We can sort the entire database by the field of Student
Name.
Figure 10.28 This database of student records for a course contains eight fields.
To do so, we highlight the entire database, including the field names; this is the range
B4:I21. Next,we choose Data > Sort from the menu. We will then see a window similar
to Figure 10.29.
(a) (b)
18
Chapter 10: Working with Large Data
Figure 10.29 (a) The Sort window allows us to choose a field name by which to sort
and select Ascending or Descending Order for the sort. (b) A list of all of the field
names.
We can select any field name to sort by; we have chosen Student Name in Ascending
Order. Ascending Order will list last names from A to Z; Descending Order would list
them in the opposite order. We can see the results of this sort in Figure 10.30. Note
that the and icons at the top of the window will automatically sort the
highlighted database by the first field in Ascending and Descending Order, respectively.
Figure 10.30 The database is now sorted in alphabetical order by Student Names.
Note also that in the Sort window (Figure 10.29), we can sort by more than one field at a
time. For example, in Figure 10.31, we have sorted by Class Average in Descending
Order and then by Student Name in Ascending Order. Observe that the values for Class
Average in cells I8 and I9 are the same (88.6) and the student names are listed
alphabetically by last name (Lexington and Richards). Please note, however, that there
is a limit of three fields that you can simultaneously sort by.
19
Chapter 10: Working with Large Data
(a)
(b)
Figure 10.31 First sorting by Class Average in Descending Order and then by Student
Name in Ascending Order. (b) The database is now sorted.
There is one more option to specify in the Sort window; this is the Header Row option. It
simply informs Excel whether or not the field names are already specified by column
names in a header row. If there is a header row, which we have marked in the above
example, then the field names appear in a list as column titles. If, however, we had
selected No header row, there would only be column names in the field list (see Figure
10.32).
20
Chapter 10: Working with Large Data
Figure 10.32 If we select No header row, the Excel column names are used as field
names instead of as column titles in the database.
We can also sort within a row. To do this, we click the Options button on the Sort
window. The window shown in Figure 10.33 appears. There is an option to determine
how the first field we sort by should be sorted; the default value is Normal; other options
refer to date values. We can also select whether our data is Case Sensitive or not. To
sort within a row, we will need to change the Orientation option from “top to bottom” to
“left to right.”
Figure 10.33 Sort Options include sorting from top to bottom or left to right.
Once we press OK and return to the main Sort window, wel notice that the field lists
show row numbers instead of column numbers (see Figure 10.34). These numbers refer
to the row numbering on the spreadsheet; they are not relevant to our table.
21
Chapter 10: Working with Large Data
Using the student data, let’s choose row 4 to sort by since it has the titles of the different
grades. If we sort by Row 4 in Ascending order, we will see the table shown in Figure
10.35.
10.5.2 Filtering
The next database function we will discuss is filtering. Filtering differs from sorting in
that it selects a specified set of data from the database instead of ordering the entire
database. Filtering allows you to select a group of entries in a database that is equal to
a particular data entry within a field. For example, referring to Figure 10.28, instead of
ordering all of the data by sorting by Class Average, we could select to view only the row
items that have a Class Average equal to 88.6. To do so, we begin by highlighting the
entire database, including column titles, cells B4:I21. Then,we choose Data > Filter >
Auto Filter from the menu. This selection will transform the database by adding drop-
down arrows to each field (see Figure 10.36).
Figure 10.36 The database is auto-filtered with selection options for each field.
We then select the arrow attached to the Class Average field. Here, we determine a
particular data value, say 88.6, that will reconstruct the database so that it only displays
the rows whose Class Average entries match the value chosen (see Figure 10.37). We
can again display the entire database by clicking on the option arrow of Class Average
and selecting (All).
23
Chapter 10: Working with Large Data
(a)
(b)
Figure 10.37 (a) Selecting the drop-down list of values in the Class Average field.
(b)The database is filtered to only show data entries with a Class Average equal to 88.6.
There are two more options on the filtered drop-down list in each field that we would like
to discuss. One is Top 10 and the other is Custom. Selecting Top 10 allows us to
display 10 entries (or another specified amount) or 10 percent (or another specified
percent) of the entries on the top or bottom of the database. In Figure 10.38, we chose
to show the top 10 entries.
(a)
24
Chapter 10: Working with Large Data
(b)
Figure 10.38 Choosing the Top 10 option from the drop-down list. (a) Specifying
Top or Bottom, a certain number, and items or percent. (b) The database is updated to
show only the number of entries specified.
The Custom option gives you a set of inequality specifications. You can choose to view
entries whose field value is less than, greater than, or equal to a certain value. There is
also an And/Or option which allows you to narrow your search to a particular range of
values. For example, in Figure 10.39, we have specified to list only the entries whose
Class Average is between the values 81.1 and 82.4.
(a)
(b)
Figure 10.39 Selecting the Custom option. (a) Specifying to show only entries with
Class Average values greater than or equal to 81.1 and less than or equal to 82.4. (b)
The database is updated to show only these entries.
25
Chapter 10: Working with Large Data
Filtering is useful for locating specific data correlations in a database. You can define a
select group of data by choosing a field entry that they have in common. There are
many applications of databases for which filtering will be a necessary tool.
10.5.3 DFunctions
There is a group of Excel functions that are meant specifically for working with Excel as
a database; we call these functions Dfunctions. These are specific functions designed
for use with databases. They include DSUM, DAVERAGE, DMIN, and DMAX, which
are essentially the same functions as SUM, AVERAGE, MIN and MAX, which we
discussed in Chapter 4. Dfunctions differ from the previously described functions
because they specify certain criteria before performing the function. That is, the formats
of these functions have extra parameters (we use DSUM to illustrate the general format):
The criteria parameter must include a field name and a criteria cell. In preparation for
using Dfunctions, we must add a few rows to our database (see Figure 10.40). These
rows contain our criteria and must repeat our field names. We will define the Dfunction
for this example in a new cell below these criteria. Using the example from Figure 10.28
one more time, let’s find the minimum Exam 2 score for students with a Class Average
above 85.0.
Figure 10.40 Adding criteria rows to the bottom of the database with the repeated
field names. The criterion specified in I22 is associated with the DMIN function that finds
the minimum Exam 2 score of students with a Class Average above 85.
26
Chapter 10: Working with Large Data
To do this, we choose Insert > Function from the menu, or select the fx icon from the
standard toolbar and find the function category labeled Database. We can now view all
of the Dfunctions and choose DMIN from the list (see Figure 10.41). After selecting
DMIN, a new window will appear that guides us through defining the function.
Figure 10.41 In the function window, selecting the Database category to view all
Dfunctions.
Here we begin by defining the database parameter, which is the entire database
excluding the new criteria rows, cells B3:I20. The field parameter is the name of the field
in which we are searching for a minimum value, in this case, the Exam 2 Score. We
specify the minimum value by entering the cell with this field name, which is G3. (We
could also just enter the title of the field here.) Lastly, the criteria parameter includes the
field name and criteria value cells. In this scenario, we want to find the minimum Exam 2
score for only the students with a Class Average above 85. Therefore, in the criteria
cells, >85 is the criterion in the Class Average field. So for this parameter, we enter the
cells I21:I22. The final DMIN function is then as follows (also see Figure 10.42):
27
Chapter 10: Working with Large Data
Figure 10.42 Each argument is defined here for the DMIN function. Explanations of
each argument are provided below the entry fields.
After entering this Dfunction, we will find the minimum Exam 2 Score to be 91 (see
Figure 10.43(a)).
We have illustrated another scenario that searches for the minimum Exam 2 Score for
the students with exactly 1 absence. In this case, the number 1 is the criterion for the
Attendance field. Notice that the DMIN function in this case has the same database and
field parameters but different criteria since we are now reviewing a condition in the
Attendance field instead of the Class Average field (see Figure 10.43(b)).
28
Chapter 10: Working with Large Data
(a)
(b)
Figure 10.43 The DMIN function uses the criteria in the Class Average field. (b) The
DMIN function uses the criteria in the Attendance field.
29
Chapter 10: Working with Large Data
The other Dfunctions can be defined in a similar fashion. After clicking the fx icon and
choosing a Dfunction from the Database category, the Function Argument window will
guide you through defining your database and criteria for which you want to perform the
selected function. Dfunctions are useful for databases as they allow you to consider
many options in your search for a particular value.
Some other functions that are not Dfunctions are also useful to manage database data in
Excel. One group of these functions, COUNT functions, has four main sub-functions:
COUNT, COUNTA, COUNTBLANK, and COUNTIF. The COUNT function simply counts
the number of cells with numerical values in a given range. The format of the function is
as follows:
=COUNT(range)
The COUNTA function counts all of the cells with data of any kind in a given range of
cells. That is, it will count the number of cells with numbers or text, or any other non-
blank cell. The format for this function is:
=COUNTA(range)
The COUNTBLANK function counts the number of blank cells in a given range. It can be
helpful for finding empty entries in a data table. The format for this function is:
=COUNTBLANK(range)
Let’s use these three functions on the student data in the previous example. Suppose
we want to determine the number of assignments in the record. To use the COUNT
function, we must highlight a row of grades, excluding the row of assignment titles. We
must exclude the row of assignment titles because the COUNT function only counts cells
with numerical values. If we use the COUNT function with the cells C4:G4, we find that 5
assignments have been recorded. To count the number of students in the class, we can
use the COUNTA function with the first column of data (B4:B20); the result is 17
students.
Now suppose that we want to determine the number of students who never had an
absence. We can remove the “0”s from the column of absences so that there are blanks
instead. Now we can use the COUNTBLANK function with the attendance column
(H4:H20); the result is that 9 students have perfect attendance records. See Figure
10.44 for these results.
30
Chapter 10: Working with Large Data
The COUNTIF function is probably the most useful COUNT function. It counts the
number of cells in a given range that meet a specified criterion. The format for this
function is:
=COUNTIF(range, criterion)
The criterion can contain some helpful characters such as * for a sequence of unknown
values or ? as a single wild card value.
Let’s use the COUNTIF function to determine the number of students who have an A (≥
90), a B+ (≥ 85), or a B (≥ 80) as their final grades. First, we need to find the number of
students with an A by using the COUNTIF function as follows:
=COUNTIF(I4:I20, “>=90”)
Now, to determine the number of students with a B+, we want to count the grades that
are < 90 and ≥ 85. However, since we cannot have multiple criteria with this function, we
will first just count the number of students with final averages ≥ 85. This will result in the
number of students with an A or a B+. Then, we subtract the previous result from this
result to find the number of students with B+’s only. We repeat this process to find
students with a B. See Figure 10.45 for the results.
31
Chapter 10: Working with Large Data
Figure 10.45 Using The COUNTIF function with the student data.
Similar to the COUNTIF function is the SUMIF function. The SUMIF function will
calculate the values in a given range that meet a specified criterion. This function is also
similar to the DSUM function that we discussed earlier in this section. The format of the
SUMIF function is:
Even though we use the SUMIF function in the same manner as the DSUM function, it
only allows for a single criterion. Therefore, when using Excel as a database, we
recommend using the DSUM function instead of the SUMIF function to handle multiple
criteria.
We would also like to note that a Conditional Sum Wizard Add-In accomplishes the
same task as the SUMIF function. After ensuring that it is checked as an Add-In, you can
find the Conditional Sum Wizard in the Tools menu. The Wizard has four steps in which
you need to specify the data range, the criteria, how the formula is to be copied, and
where the formula will be copied to.
32
Chapter 10: Working with Large Data
Most database applications have a data validation tool. It enforces a certain format or
type of data to be entered by the user for particular input. Excel offers this tool when
working with large data. To use data validation, select the cell(s) you want to validate
and then click on Data > Validation from the menu. The window shown below will appear
(see Figure 10.46).
There are three main tabs in this window: Settings, Input Message, and Error Alert. In
the Settings tab, you specify the criteria for the data that will be entered into the selected
cell. As shown in Figure 10.47, there is a list of criteria including Whole Numbers, Dates,
Text Lengths, and others.
33
Chapter 10: Working with Large Data
Depending on which criteria are selected from the list, you will have a few more options
to further specify these criteria. For example, if we choose Whole Number from the list,
we then choose from a list of inequalities (greater than, less than or equal to, between,
etc) and provide some numerical bounds (see Figure 10.48).
Figure 10.48 Specifying inequality and bounds for the Whole Number criterion.
34
Chapter 10: Working with Large Data
The Input Message tab allows you to create a comment that will appear next to the cell
after it is selected. This message is intended to guide the user to enter the correct data.
For example, in Figure 10.49, we have created the message “Please enter a whole
number between 0 and 5 only.” This message matches the criterion we specified.
The Error Alert allows you to display a message to the user after he or she has entered
any data incorrectly. You can choose to have an Error, Warning, or Information symbol
on this message box also; each symbol has a different set of options for the user in the
message box. In Figure 10.50, we have chosen the Warning symbol and have entered
the message, “You must enter a whole number between 0 and 5!”
35
Chapter 10: Working with Large Data
Figure 10.50 Creating an Error message to warn users if they enter any incorrect data.
Let’s consider an example. Suppose we have a record of customer orders. For each
order, the date, quantity, and price of the order are noted. If a maximum of 5 products
can be sold in one order, we can apply the above Whole Number validation to the
Quantity column. The Input Message for this validation appears when a cell in the
Quantity column is selected (see Figure 10.51). If we enter a number greater than 5 (or
less than 0), then the Warning message created in the Error Alert tab will appear (see
Figure 10.52).
Figure 10.51 When selecting the validated cell, the Input Message appears.
36
Chapter 10: Working with Large Data
Figure 10.52 If a user enters incorrect data, the Warning message appears.
We could also validate the Date column in this example by choosing the Date criterion.
We may, for example, specify that this record is for dates before February 1st only (see
Figure 10.53).
This time, let’s choose the Information alert type. The Input Message and alert are
shown in Figure 10.54 for a selected cell in the Date column.
37
Chapter 10: Working with Large Data
Figure 10.55 The Custom criterion uses a formula to check for errors.
One last unique criterion is List. It allows you to create a list box, or drop-down box, of
options for users to choose from as their entry value. We must create a source for this
list somewhere in the spreadsheet. For example, if we have an additional column in the
38
Chapter 10: Working with Large Data
Customer Order table for the Salesperson who took the order, we may want to limit this
entry to the list of valid salespersons working for the company. In the spreadsheet
below, there is a list of salesperson names. We can then select List from the criteria
options and highlight this range of names as the source (see Figure 10.56).
Figure 10.56 The list of names becomes the source for the List criterion.
Then, we can set the Input Message and Error Alert so that the users know they can
only enter a salesperson name from the provided list. When they select a cell from the
Salesperson column, a drop-down arrow will appear providing them with this list of valid
names (see Figure 10.57).
39
Chapter 10: Working with Large Data
Figure 10.57 The List criterion provides a list of valid entry values.
Another database tool available in Excel, data consolidation, allows you to compare and
combine multiple sources of data into a new spreadsheet. Consolidation can also be
accomplished with Pivot Tables’ “multiple consolidation ranges” option; however, the
Data Consolidation tool can sometimes be easier to use. Let’s consider an example.
Suppose that we have monthly sales recorded for various products in two different
marketing regions (see Figure 10.58). If we want to know the total sales per month
(among all regions) for each product, we would want to use data consolidation.
(a)
40
Chapter 10: Working with Large Data
(b)
Figure 10.58 A record of monthly sales per product for two different regions
We begin by creating a new spreadsheet (right-click on a sheet name and choose Insert
> Worksheet from the list of options). In this new sheet, we select Data > Consolidate
from the menu. The window shown below will appear (Figure 10.59).
The data consolidation tool provides a list of functions that can be applied to the data as
it is consolidated (see Figure 10.60). These functions include Sum, Count, Max, Min, etc.
For this example, let’s use the Sum function since we want to find the total number of
sales in both regions.
41
Chapter 10: Working with Large Data
Figure 10.60 Multiple functions are available in the data consolidation tool.
After selecting the function, we must choose the references of the data that we want to
consolidate. In this case, we select the entire table from both the Region 1 Sales and
Region 2 sales spreadsheets. Next, we select the Top row and Left column check
boxes for the location of data labels (since we have selected these labels in the
reference as well). We also make sure to check the box “Create Links to source data.”
This option ensures that any change made to the source data (that is, the original tables)
will be reflected automatically in the consolidated table. The completed data
consolidation window appears in Figure 10.61.
42
Chapter 10: Working with Large Data
Figure 10.62 displays the resulting consolidated table. Excel has calculated the sum of
the sales of each product per month. Notice that some small + icons appear on the left
side of the table. We can click these to expand the hidden rows that show the sources
(or the references) of the consolidated data.
10.6 Summary
¾ You can import text files, webpage information, and database fields into Excel using
the Import External Data wizard.
¾ Queries can be made to data being imported from a database.
¾ Using the Access Links Add-In, data can be exported to a database.
¾ Pivot Tables can use external data, such as from databases, their source,.
¾ Sorting and Filtering are helpful tools for organizing large data.
¾ There are several functions, called Dfunctions, that can be used on large data in
Excel. There are also some other functions, such as COUNT functions and SUMIF,
that can be used with large data.
¾ Data Validation and Data Consolidation are database tools available in Excel.
10.7 Exercises
10.7.1 Review Questions
1. Define the terms sorting and field and discuss how they are related.
2. What are the two ways to sort a set of data?
3. How does filtering differ from sorting?
4. How does selecting Auto Filter affect the display of the spreadsheet?
5. What does the Top 10 option from a filtered drop-down list allow you to do?
6. What does the Custom option from a filtered drop-down list allow you to do?
7. Give an example of an instance when filtering data in an Excel database may be
necessary.
8. How do Dfunctions differ from ordinary functions?
43
Chapter 10: Working with Large Data
44
Chapter 10: Working with Large Data
45
Chapter 10: Working with Large Data
8. Read hands-on exercise 10.7. Present the name, enrollment date, credit limit and the
number of late payments for the cardholders that are eligible for the platinum-level
membership.
46
Chapter 10: Working with Large Data
10. The table below presents sales information of a company that sells beauty products
in different regions of US. Answer the following questions:
47
Chapter 10: Working with Large Data
11. The Text option in Data Validation enables you to generate an error message when
the number of characters in a cell is not of a desired length. Use the Text option to
ensure that each cell in the range A1:A10 will contain at most 5 characters (including
blanks).
12. You are entering employee names in the cell range B1:B10. Use Data Validation to
ensure that no employee’s name is entered more than twice in this column. (Hint:
use the COUNTIF function.)
13. Suppose you have asked an assistant to enter values in a debt database. There is a
column each for names, phone numbers, and price owed. Use Data Validation to
ensure that only text is entered as a name, phone numbers have 10 digits, and price
owed is never negative.
15. Find a website on foreign exchange rates. Import this data to Excel and save the
query. Then run this query again to find the updated exchange rates.
16. Use the following information to create a text file. Import the file to Excel using the
appropriate delimiter.
17. Place the following data in a text file and import it into Excel. Do not import the
address or zip code columns.
18. Using the information recorded in the table below for the project teams of a class,
perform the following actions:
48
Chapter 10: Working with Large Data
Project
Teams Topics Team Members Presentation Report
1 Capital Budgeting Problem John Amy Ruth Steve 77 83
2 Reliability Problem Tom Joan Rob Randy 81 76
3 Quality Control Sam Tim Vic Sally 80 85
4 Simulation Susan Melissa Mac Eric 97 93
5 Production Problem Mary Jorge Maggy Paul 95 100
19. Consider the database in hands-on exercise 10.10. Answer the following questions:
20. The database given below presents the sales transactions for different products in
two different regions. Perform the following data consolidations:
a. Identify the Max sales (over both regions) for each product in each month.
b. Count the number of sales made (over both regions) for each product during
the three months period.
49
Chapter 10: Working with Large Data
21. Consider the data presented in hands-on exercise 10.20. Use Dfunctions to find the
following:
h. The average amount sold during the first half of March in region 1.
i. The average amount of product A sold during the second half of
January in region 1.
j. The minimum amount of product B sold during the month of February in
region 1.
k. Total amount sold in January 1st in region 1.
22. A professor saved the results from three different exams he gave in his class, in
three tables. Use the Data Consolidation tools in Excel to create the following:
a. A table that presents for each student the identification number, as well
as the grades of the three exams.
b. A table that presents the average exam grade for each student.
c. A table that presents the maximum grade received in an exam for each
student.
d. A table that presents the minimum grade received in an exam for each
student.
50
Chapter 10: Working with Large Data
23. A retail store keeps the information about the products carried in the inventory in an
excel spreadsheet. This information is presented in the table below. Use Data
Validations tools to insure that:
a. The identification number for each item is a number that has exactly five
digits.
b. Inventory level for each item cannot be negative.
c. The unit price for each item is at least $150.
d. A product condition should be either fair, or good, or very good.
e. The purchase date for the items in the inventory should not be less that
1/1/2000.
24. A University carries the information about the current students in table tblStudents in
the School.dbm file in MS Access. The following figure presents the table
tblStudents.
51
Chapter 10: Working with Large Data
25. A University carries the information about the instructors in table tblInstructors in the
School.dbm file in MS Access. The following figure presents the table tblInstructors.
52
Part II: VBA for Excel
EXCEL BASICS
Visual Basic for Applications (VBA) for Excel can be a powerful tool to manipulate Excel
objects. The chapters in this part of the book give an overview of how to program in the
VBA language and then revisit the extended functionality topics of Excel to show the
improvement that can be added using VBA.
VBA Overview
Chapter 12: Recording Macros
Chapter 13: More on Objects
Chapter 14: Variables
Chapter 15: Sub Procedures and Function Procedures
Chapter 16: Programming Structures
Chapter 17: Arrays
Chapter 18: User Interface
Chapter Overview
11.1 Introduction
11.2 The Visual Basic Editor
11.2.1 Project Explorer
11.2.2 Properties Window
11.2.3 Code Window
11.2.4 Immediate and Watch Windows
11.2.5 VBE Toolbars
11.4 Summary
11.5 Exercises
11.1 Introduction
Chapter 11: Introduction to the Visual Basic Environment
This chapter introduces the Visual Basic environment. We expect the reader to have
read the chapters in Part I of the book or to have an equivalent knowledge of Excel. It is
important for the reader to be comfortable in the spreadsheet environment as Visual
Basic for Applications (VBA) will be used to perform the same spreadsheet functions that
are done in Excel along with some other advanced manipulations. VBA will bring a
dynamic element to the spreadsheet DSS applications. With VBA, we will modify the
spreadsheet, create a user interface, and perform simulation models and solve
optimization problems. In this chapter, we begin by understanding the environment in
which will be using VBA and the basics of this programming language.
VBE icon from Excel and the Excel icon from VBE. You can also toggle between
the windows just by pressing Alt+F11 in either window. To view the execution of the
code, write VBA code in the VBE first and then go to Excel. Or, you can record a macro
in Excel and then check the code automatically created in the VBE. We will discuss this
more in Chapter 12, “Recording Macros.”
There are three main windows in VBE: the Code Window, the Project Explorer, and the
Properties Window. The Project Explorer lists all projects in any open workbook. Each
workbook has a project, and each project can have several parts, namely its objects,
modules, and forms. Each of these project parts can have VBA code associated with it.
Excel automatically lists the objects; they are the worksheets and the workbook itself. In
Figure 11.1, you will notice that there are no modules or forms in this project, only the
worksheet and workbook objects.
The modules contain the VBA code for any macro, which does not have to be
associated with a particular worksheet; that is, the modules have macros that can be
executed in any worksheet at any time. There are two types of modules: standard and
class. Standard modules are used for writing general procedures, or sections of code, to
perform different tasks with Excel objects. We will perform these tasks by using
properties and methods to manipulate Excel objects. There are sets, or libraries, of
properties and methods available in VBA for all Excel objects. Class modules are used
to create new properties and methods for more advanced coding. For now, we will focus
on standard modules.
Forms are the user interface tools in Excel. Forms have two design areas: the user
display and any VBA code associated with user display items. We will discuss these
user forms in more detail in Chapter 18.
2
Chapter 11: Introduction to the Visual Basic Environment
To add a user form or module to your project, simply click Insert > User Form or Insert >
Module, respectively. You can also use the icon to see a drop down list of
insert options. [Note: You also have the option to insert a Procedure, which is either a
macro (subroutine) or a function, or a Class Module, which defines the properties and
methods of an object.] You can insert objects, modules, or forms from other projects by
using Insert > File or File > Import File. Every time a new part of a project is inserted,
including when a new worksheet is added from the Excel workbook, you will see it
bookmarked in the Project Explorer for easy reference.
The Properties window contains detailed information about any selected project part
from the Project Explorer. For example, in Figure 11.1, you can see the properties for
the workbook object since it has been selected in the Project Explorer.
This window displays basic properties and more advanced ones that are adjustable,
such as font size of all the text in a worksheet or user form items. The user form
properties tend to be the most commonly used. Each item in a user form has its own list
of properties; that is, a text box can have a different font type than a label on a user
form. Again, we will discuss user forms in more detail in Chapter 18. We will not discuss
the various properties here, but recommend that you go ahead and experiment with
changing some of them.
3
Chapter 11: Introduction to the Visual Basic Environment
Project Explorer
Properties Window
Figure 11.1 Two of the main VBE windows: the Project Explorer and the Properties
Window.
The main window is the Code window, which is in the central section of the VBE. This
window displays the VBA code for the highlighted project in the Project Explorer. Macros
are recorded as procedures in VBA. When we record macros, we simultaneously create
VBA code in the Visual Basic Editor.
There are two main types of procedures: sub and function. We will discuss both in more
detail in Chapter 15. Sub procedures are always created when a user records a macro
in Excel. The statement Sub and the macro name introduce the code . The code ends
with the command End Sub. The VBE automatically generates these two statements
when a new sub procedure is inserted.
Two other VBE windows are the Immediate window and the Watch window. These
windows are useful for debugging purposes. Whatever you type into the Immediate
window is executed as soon as you enter it. In the Watch window, variable names,
when typed, are “watched” by VBE and displayed whenever their values change. We will
use these windows when discussing debugging in Chapter 22.
4
Chapter 11: Introduction to the Visual Basic Environment
There are four toolbars in VBE: Standard, Edit, Debug, and Userform. We will primarily
use the Standard toolbar for now and we will discuss the Debug toolbar in Chapter 22.
The Standard toolbar allows you to switch to the Excel window, insert a user form,
module, or procedure, and save your current code modifications (see Figure 11.2). It
also includes several standard editing options, such as cut, copy, paste, search, undo,
and redo. The Standard toolbar allows you to run, pause, or stop your code. You can
also view the design mode of your form design (which we will discuss in more detail in
Chapter 18) and view or hide the Project Explorer, Properties Window, Object Browser,
and User Form Toolbox. The final icon is for help options.
Before understanding the purpose of the Object Browser, let’s review from Chapter 2 the
idea of an object and the object model. A few examples of Excel objects are the
workbook, the worksheet, a range of cells, and a cell itself. Excel objects are grouped
according to their order in what is known as the object hierarchy. That is, the workbook
object includes the worksheet object, which includes the range object, which includes
the cell object. This entire system of objects is known as the object model.
The Object Browser provides a list of properties and methods of all Excel objects that
may be manipulated in VBA (see Figure 11.3). Properties are the physical descriptions
of all Excel objects. For example, a range object can have a length property and a cell
object may have a font size property. The description of the particular property is called
the value of the property. To continue the example, a length property could have a value
of two and a font size property could have a value equal to “Arial.”
Methods are the actions that can be performed on an object. For example, a user can
select a range or copy a cell. The elements of a method statement are called the
arguments of the method. For example, a cell can be copied to a certain destination;
5
Chapter 11: Introduction to the Visual Basic Environment
the specification of that copy destination would be an argument of the copy method for
the cell object.
There are a few more terms to be familiar with when using the Object Browser. Groups
of related objects are called classes; collections of VBA and Excel object classes are
called libraries; and the properties, methods, and constants of a selected class of
objects are known as the members of the class of objects. In the list of members,
properties are identified by the icon and methods are identified by the icon.
You can also find related sub procedures that have been predefined by Excel; the
icon identifies them.
Use the Object Browser to search different libraries of objects for a list of all classes of a
specified object. This allows you to research the class members to find out which
methods and properties can be applied.
Figure 11.3 The Object Browser lists all libraries of properties and methods in VBA
and Excel.
6
Chapter 11: Introduction to the Visual Basic Environment
To find all of the properties and methods associated with a particular object, first select a
library from the library drop-down list at the top of the window; then type the object name
in the search field. For example, if we select the Excel library and type “worksheet” in the
search field, we see the results shown in Figure 11.4. Two windows appear; the first is
the Search Results window, which displays a list of classes to which the object belongs
to, and the second shows all classes and members related to the selected search result.
For example, in Figure 11.4, we have selected the first worksheet result from the Search
Results window; we can see that the worksheet object has been located under class in
the window below. We can now view all of the members of this class and view a more
detailed description of the property, method, or constant selected in the bottom window.
Here, we have selected the Activate method from the member list, which is described as
an Event and a Member of Excel worksheet.
The Object Browser is a very useful tool for determining the best way to manipulate an
object of interest.
Figure 11.4 Search results for a worksheet as an Excel object in the Object Browser.
11.4 Summary
¾ The Visual Basic Editor (VBE) is the environment in which you work with VBA
programming code.
7
Chapter 11: Introduction to the Visual Basic Environment
¾ There are three main windows in VBE: the Code Window, the Project Explorer,
and the Properties Window.
¾ The Project Explorer lists all projects in any open workbook. Each workbook has a
project, and each project can have several parts: its objects, modules, and forms.
Each of these project parts can have VBA code associated with it.
¾ The Properties window contains detailed information about any selected project
part from the Project Explorer. You can adjust the basic properties and more
advanced ones in this window. these properties include font size of all the text in a
worksheet and user form items. The user form properties tend to be the most
commonly used.
¾ The main window is the Code window, which is in the central section of the VBE.
This window displays the VBA code for the highlighted project in the Project
Explorer. When we record macros, we simultaneously create VBA code in the
Visual Basic Editor.
¾ The Immediate window and the Watch window are more useful for debugging
purposes. Whatever you type in the Immediate window is executed as soon as
you enter it. In the Watch window, variable names, when typed, are “watched” by
VBE and displayed whenever their values change.
¾ There are three main toolbars in VBE: Standard, Edit, and Debug.
¾ The Object Browser provides a list of properties and methods of all Excel objects
that may be manipulated in VBA. Properties are the physical descriptions of all
Excel objects, and the description of the particular property is called the value of
the property.
¾ Methods are the actions that can be performed on the object. The elements of a
method statement are called the arguments of the method.
¾ Groups of related objects are called classes; collections of VBA and Excel object
classes are called libraries; and the properties, methods, and constants of a
selected class of objects are known as the members of the class of objects.
11.5 Exercises
11.5.1 Review Questions
1. List and define the three parts of an Excel project.
2. What is the difference between a property and a method?
3. Using the Object Browser, give three examples of properties within the Chart
class.
4. Using the Object Browser, label the items listed as a function, a property, or an
event, and name a class of which each is a member.
a. WindowActivate
b. Cells
c. SumIf
d. Protect
e. ColorIndex
5. What is a procedure?
6. What are the two main procedure types?
7. What are the options on the Edit toolbar?
8. Using the Object Browser, find the arguments for the Sort method.
9. Do the Project Explorer and Properties windows have to be open at all times?
10. How do you insert a module into a project?
8
Chapter 12
Recording Macros
Chapter Overview
12.1 Introduction
12.2 Macros
12.2.1 Recording Macros
12.2.2 The VBA Code
12.2.3 Event Procedures
12.4 Summary
12.5 Exercises
Chapter 12: Recording Macros
12.1 Introduction
This chapter discusses macros as an introduction to working with VBA code. Recording
macros will automatically create VBA code which we can review in order to understand
how VBA performs basic Excel functions. It is very important that the user understand
the material in this chapter as this is the basics for reading, understanding, and writing
VBA code. We also introduce some basic user interface in this chapter. This user
interface creates a connection between the VBA code and the Excel spreadsheet so that
the user can easily perform the actions written in VBA.
12.2 Macros
Macros are technically defined as units of VBA code. In Excel, macros can be
considered a way to automate a series of actions in a spreadsheet application. Macros
can either be created directly with VBA code in the Visual Basic Editor, or they can be
recorded in Excel. To record a macro, you must know exactly the actions that you wish
to perform and then use the Macro Recorder. You can then translate standard Excel
functionality into VBA code.
Let’s consider the simple example of combining the actions of copying and pasting data.
Figure 12.1 presents a table of used car information that a dealer has in his database.
He wants to place the data highlighted in yellow in a newspaper advertisement.
To prepare for recording the macro, we should rehearse the steps needed to copy these
four columns and paste them into the cells below. We begin by selecting the first three
columns (Make, Model, and Year) of the Used Cars table and then, while pressing the
CTL key, select the last column (Price). We then either right-click and choose Copy or
use the shortcut key CTL-C to copy the selected data. Then, we place our cursor in the
first cell of the Newspaper Information table (cell C19) and paste the data either by right-
clicking and choosing Paste or by using the shortcut key CTL-V.
2
Chapter 12: Recording Macros
Now that we know the exact steps we need to perform when recording the macro, we
are ready to initiate the Macro Recorder. There are two ways to do so; we can select
Tools > Macro > Record New Macro from the menu, or simply press the circular Record
button, ., in the VBE toolbar.
At this point, the window in Figure 12.2 appears. Here, we name the macro and define
its location as well any shortcut keys that we wish to associate with it. For this example,
we simply name our macro “CarCopyPaste” and apply it to this workbook without any
associated shortcut key. After pressing OK, the Record button from the icon bar
transforms into a Stop button . We can either select this icon from the VBA toolbar or
choose Tools > Macro > Stop Recording from the menu.
3
Chapter 12: Recording Macros
Figure 12.2 The Record Macro window prompts us to create a name for the macro we
are about to record. We can also define the location of the macro and associate a
shortcut key with it.
Once we start recording, every action we perform on the spreadsheet will be written into
the macro code. Since we have prepared for the copy and paste task, we can just
repeat the steps mentioned above and copy and paste the yellow columns from the
Used Cars table into the Newspaper Information table. Once we have finished pasting
the cells, we can either press the Stop button in the icon bar or choose Tools > Macro >
Stop Recording Macro. We are now finished recording the macro.
Figure 12.3 To play a recorded macro, we choose the macro name desired and press
Run.
To ensure that the macro was correctly recorded, we can erase the data we just pasted
into the Newspaper Information table and press the Play button, , in the icon bar. We
can also select Tools > Macro > Macros to play the macro. We then select the macro
from the list in the Macro window that appears (see Figure 12.3) and click Run. All of
the copy and paste actions we recorded should then be performed; this results in the
complete tables in Figure 12.4.
4
Chapter 12: Recording Macros
Figure 12.4 After running the copy and paste macro, both tables are complete.
Macros can be very useful when repetitive and tedious tasks need to be performed.
You can create a macro for each task using the Macro Recorder and then play the
macro for the associated task you need to perform.
Let’s now determine how the Macro Recorder actually creates VBA code while we are
performing standard Excel functionality. In Chapter 10, we learned how to sort and filter.
In this example, suppose there is a database for the Miami Airport that lists Flight
Destinations, Number of Stops, Class (Economy or Business), and Price (see Figure
12.5). We want to be able to filter this information so that we can do the following: view
flights to Beijing, view flights to Hong Kong, and view all flights. We also want to be able
to sort the data by Number of Stops and by Price.
First, let’s review what needs to be done to filter a list. We need to highlight the entire
table (cells B3:E15), then select Data > Filter > AutoFilter from the menu. Next, to filter
for specific flight destinations, we simply select Beijing, Hong Kong, or All from the filter
drop down list near Destination. Now we just need to record these steps as three
separate macros.
5
Chapter 12: Recording Macros
For the first macro, we press the Record button in the VBE toolbar and name the macro
“ViewBeijingFlights.” Then, we highlight the data, select Data > Filter > AutoFilter, select
Beijing from the Destination filter options, select cell A1, and finally press the Stop
button.
For the second macro, we press the Record button in the VBE toolbar and name the
macro “ViewHongKongFlights.” Then, we highlight the data, select Data > Filter >
AutoFilter, select Hong Kong from the Destination filter options, select cell A1, and finally
press the Stop button.
For the third macro, we press the Record button in the VBE toolbar and name the macro
“ViewAllFlights.” Then, we highlight the data, select Data > Filter > AutoFilter, select (All)
from the Destination filter options, select cell A1, and finally press the Stop button.
After testing the macros to make sure that they perform the correct actions, we can now
view the created VBA code . To do this, we simply go to VBE (by pressing Alt+F11) and
click on Module1 in the Project Explorer. Then, we find the three macros recorded as
separate sub procedures with the names that we assigned (see Figure 12.6).
6
Chapter 12: Recording Macros
Figure 12.6 The VBA code created from recording the three filtering macros.
The first line of code for all three macros, Range(“B3:E15”).Select is an example of the
Select Method for the Range Object. To code this directly, we simply type the word
Range and specify its range (there are other ways to identify a range, which we will
discuss in Chapter 13). Then, we type a period and an automatic drop-down list of
methods and properties will appear. We can either choose Select from the list or just
type it directly (notice that as soon as you type an S, the VBE will automatically try to
guess which method or property you want to use and type it for you).
The next line of code, Selection.AutoFilter, uses the Selection as the object and
AutoFilter as the method. Again, we can view a list of methods and properties
associated with the Selection object by typing Selection and a period.
Notice that we are actually typing the AutoFilter method twice in our code. We will often
find that some coding redundancies will happen with the Macro Recorder. We can
actually modify these macros directly in the VBA code by deleting the first
Selection.AutoFilter statement from each sub procedure (see Figure 12.7). To test the
macros again, we return to Excel (press Alt+F11).
The last line of code, Range(“A1”).Select, selects a cell so that the filtered data is not still
highlighted after performing the macro.
7
Chapter 12: Recording Macros
Now we want to create the sorting macros. Again, we should first review the steps
needed to filter the data. We highlight the same range of data and select Data > Sort
from the menu. Finally, we specify our sorting criteria and order.
To record the first sorting macro, we press the Record button from the VBE toolbar and
name the macro “SortByStops”, then we highlight the data, select Data > Sort, select
“Stops” from the “Sort by” list, click “Descending” order, press OK, select cell A1, and
then press the Stop button.
To record the second sorting macro, we press the Record button in the VBE toolbar and
name the macro “SortByPrice.” We then highlight the data, select Data > Sort, choose
“Price” from the “Sort by” list, click “Descending” order, press OK, select cell A1, and
finally press the Stop button.
We can now test these macros and return to VBE to view the code (see Figure 12.8).
This code will also appear in Module1. The first line of code for these macros is the
same as the filtering macros; it just selects the range we are interested in sorting. The
last line of code is also the same; it just selects a cell to de-highlight the data.
The second line of code is the most important in this case. The object is again Selection
and this time the method is Sort; however, there are many more arguments than we
specified when recording the macro. The only arguments we specified were: Key1,
which was the column selected from the “Sort by” list; and Order1, which was the
Ascending or Descending option. The other arguments listed are actually from the
Options section of the Sort window. These may be useful for future applications, but for
this example we can delete all arguments aside from Key and Order. And it is always a
good idea to retest your macros after modifying the code.
8
Chapter 12: Recording Macros
Figure 12.8 Adding the code for the two sorting macros to the module.
In our updated code (see Figure 12.9), we see that the Key is specified by the range
name of the first cell in the column that we are sorting by. For the first macro,
Range(“C4”), is the first cell in the Stops column. Similarly, for the second macro,
Range(“E4”) is the first cell in the Price column. The Order is specified by “xlAscending”
or “xlDescending.” This “xl” notation appears several times and notates an Excel-specific
option.
Again, VBE is helpful while typing this code. If we type Selection and a period and then
select Sort from the list of methods and properties, a small dialog box comes into view; it
lists the possible arguments that we can use with the Sort method.
Figure 12.9 Updated code with only the necessary arguments listed.
9
Chapter 12: Recording Macros
Now, let’s try creating the macro originally from the VBA code and then we can view its
performance in Excel. Suppose that we also want to filter the Business or Economy
Class flights. We know from recording the above filtering macros what code is required;
however, some of the argument values will be different.
First, we need to make sure that we are in the VBE Code Window. So, we move down to
a new line under our last sorting macro and type the beginning of this new filtering macro
as a sub procedure:
Sub ViewEconomyFlights()
After typing this line and pressing enter, the line End Sub automatically appears. These
statements are the first and last line of any sub procedure:
Sub SubName()
End Sub
Now, referring to the filtering code from before, we need to select the range we want to
filter. In this case, we will be using the same range as in the previous filtering macros, so
we can type the same line of code:
Range(“B3: E15”).Select
We can now use the AutoFilter method for this selection. We can either type AutoFilter
directly or choose it from the list of methods and properties that appear after typing the
period after Selection.
Selection.AutoFilter
When we are typing the arguments for the AutoFilter method, we will encounter some
differences from our previous recorded macros Previously, the Field argument was
equal to “1,” which is the first column of the range. However, this time instead of filtering
by Destination, we are filtering by Class, which is the third column. Therefore, the Field
argument should be “3.” For the first of these two new macros, the Criteria argument
needs to be “Economy,” since that is the Class value we will be filtering. So, the
complete AutoFilter statement should be:
Note that we have typed Criteria1 and not just Criteria, because there can be more than
one criterion when filtering. All we have left is the last line of code, which will select a cell
in order to de-highlight the filtered range.
Range(“A1”).Select
For the next macro, which will filter Business Class flights, the code will be the same,
except the Criteria argument will be “Business” instead of “Economy.” Therefore, we can
just copy and paste what we have already typed a few lines below. We must also
remember to change the name of the sub procedure to “ViewBusinessFlights.”
10
Chapter 12: Recording Macros
The final code for these two macros is displayed in Figure 12.10. We can now save our
work and return to the Excel window in order to test our macros.
Figure 12.10 The two new filter macros typed directly as VBA code below the
previously recorded macros.
We recommend that you experiment with typing other macros directly with VBA code. If
you are unsure of how to perform a function, just record the macro first from Excel.
Then, you can simply view the code created in VBE and use it to create a new macro.
Events are actions that take place in the Excel window and are linked directly to a set of
actions in VBA code. We connect events to code by using Event Procedures.
Examples of event procedures include Click, Change, and Activate. For each Excel
11
Chapter 12: Recording Macros
object selected in the Project Explorer, there is a list of associated objects in a drop-
down menu in the upper-left hand corner of the Code Window. After selecting an object
from this list, you can view corresponding events in the drop-down menu in the upper-
right hand corner of the Code Window. Each object in Excel has its own corresponding
list of event procedures. In Figure 12.11, we see a list of event procedures for the
worksheet object that we selected from the Project Explorer. We will see many objects
and events when we discuss user forms in Chapter 18.
Figure 12.11 Event procedures for the worksheet object listed in the drop-down menu.
An event procedure is just like a sub procedure, except that it is associated with a
particular action of an Excel object. The name structure of event procedures is also
different from that of sub procedures; that is, sub procedures can have almost any name
(as we saw when giving names to macros we recorded) while event procedures have
the name structure object_event. Event procedures are necessary when building user
interfaces. For example, instead of asking the user to run a macro to perform the filtering
discussed above, we may want to have some buttons on the screen that will perform
corresponding macros when clicked. To do this, we first create a button and then use a
Click event procedure.
To create a button in Excel, we can use the Control Toolbox. Find the toolbox at View >
Toolbars > Control Toolbox or click the icon on the Visual Basic toolbar. The
window shown in Figure 12.12 then appears. There are several objects that can be
placed on the spreadsheet: command buttons, scroll bars, list boxes, etc. For now, let’s
choose the Command Button object (we will discuss all of these objects in more detail in
Chapter 18). To choose the Command Button, click on it in the Control Toolbox and then
click somewhere on the spreadsheet (see Figure 12.13).
12
Chapter 12: Recording Macros
Command Button
Figure 12.12 The Control Toolbox window appears after the icon is selected.
Figure 12.13 Selecting the Command Button icon from the Control Toolbox and
placing the button on the spreadsheet.
We must now view the Properties window of the Command Button to specify its name
and caption values. To view the Properties window, we can simply right-click on the
Command Button and then select Properties from the drop-down menu (see Figure
12.14). These properties are important for manipulating any object in the Control
Toolbox. For example, aside from the name and caption of an object, we can change its
font, size, and color from the list of properties. There are also some other specific
properties associated with different objects. For example, a scroll bar object has a
minimum value, maximum value, and change amount properties that can be set. Again,
we will discuss these properties in more detail for each object in Chapter 18.
13
Chapter 12: Recording Macros
For the command button we just created, we enter the name cmdHongKong and give
the caption “View Hong Kong Flights” for the Name and Caption properties, respectively.
Let us clarify that the name property of an object becomes the name of the actual object
which we may refer to in our code (such as cmdHongKong) whereas the caption
property of an object is the text that is displayed on the object (“View Hong Kong
Flights”). (We start the button name with the letters “cmd” since it is a command button.
This is not required by Excel or VBA, but it is a good programming practice in order to
keep track of the type of objects created. We list other abbreviations for controls in
Chapter 18.) We see that our button is updated as we enter the caption information in
the Properties window. We can also modify the font or button colors at this time;
however, we will not detail all of these options here.
Figure 12.14 The Properties window for the command button. Modifying the Name
and Caption properties.
Now that the Command Button has been created, we are ready to write the event
procedure that associates a set of actions with the Click event for this button. To create
event procedures for a Control Toolbox object, we must write the code in the appropriate
worksheet object in VBE, not in a module. That is, this control becomes an object
14
Chapter 12: Recording Macros
associated with the worksheet object in which it is placed. For this example, we will
create the event procedure in Sheet1 from the Project Explorer. We can use exactly the
same code as we recorded above to filter the Hong Kong flights. However, we must re-
title that sub procedure as a new event procedure by typing instead:
Sub cmdHongKong_Click()
Here, we have entered the name of the Command Button (as we specified in the
Properties window in Excel) and then an underscore followed by the type of event with
which we want to associate the code. We could also have selected the Command Button
cmdHongKong from the drop-down list at the top of the Code window and then selected
Click in the list of events (see Figure 12.15). The Command Button now appears in the
list of objects for the selected worksheet, since it has been placed in this worksheet.
Figure 12.15 The event procedure code for the Click event of the cmdHongKong
Command Button.
Notice that before the procedure, VBE automatically inserted the word Private. This
implies that the procedure can only be run from the worksheet in which the code was
written. In other words, the code is associated with a button that can only be clicked from
this worksheet. However, we may not always want to limit our coding in this way. Private
and public procedures will be discussed in more detail in Chapter 15.
Now, we should return to Excel to test the functionality of our button. We must first make
sure that we are no longer in Design View. Design View is active when we are creating
or modifying any Control Toolbox object on the sheet. To activate or deactivate the
Design View, just click the icon on the Visual Basic toolbar. Now we can click “View
Hong Kong Flights” and see that the filtering works correctly (see Figure 12.16).
15
Chapter 12: Recording Macros
Figure 12.16 Clicking the Command Button runs the associated event procedure to
filter the data.
We could now create similar Command Buttons for the other macros that we previously
recorded. However, there is another way to build a user interface for macros in Excel
without using the Control Toolbox. It is actually a much simpler method because it
avoids setting properties and using event procedures. All we need to do is draw a shape
by using the Drawing toolbar and then assign the appropriate macro to it (see Figure
12.17).
After creating the shape, we right-click on it and choose Add Text from the list of options.
We enter the words “View Hong Kong Flights” to our shape, which is now a button. If we
want, we can modify the shape to make it look more like a button (see Figure 12.18).
Figure 12.17 From the Drawing Toolbar, we select any Auto Shape and Add Text to
create a button.
16
Chapter 12: Recording Macros
Figure 12.18 Creating a text box that will serve as a button to run the associated macro
with the Drawing toolbar.
Now, we just right click on the box and select Assign Macro from the drop-down list.
From here, we see a list of all macros in the workbook. For our first button, we select
“ViewHongKongFlights” (see Figure 12.19).
This procedure demonstrates another advantage of creating buttons since any public
macro from any module can be assigned to any object on any sheet. This way we are
not limited to a private event procedure. We can repeat this method until we have
created a button for each of our macros for this example (see Figure 12.20).
17
Chapter 12: Recording Macros
Figure 12.20 Creating a button to assign to each macro using the Drawing Toolbar.
To review, there are two options for creating buttons with which to associate macros.
These are: selecting a control from the Control Toolbox or creating a drawing shape.
Using a control requires you to set various properties to manipulate formatting. You must
also create a name for the control with which you write an event procedure. This event
procedure code must be located in the worksheet object (selected from the Project
Explorer) in which it is placed. With shapes, you can add captions and format them
easily. You can also assign any macro or any public procedure from any module to this
type of created button. Some controls are very unique and can be used often with user
forms (as described in Chapter 18); however, for creating buttons, drawing shapes is
probably the simpler choice.
Creating buttons as a user interface can greatly aid a user unfamiliar with macros in
performing desired functions. Another useful way to help a user with macros is to create
a toolbar with icons designated for each macro. This technique is called customizing a
toolbar.
18
Chapter 12: Recording Macros
To customize a toolbar, we first go to View > Toolbars > Customize from the menu. The
window shown in Figure 12.21 appears. There are three tabs for this window: Toolbars,
Commands, and Options. Let’s begin with the Toolbar tab.
We can either add buttons to an existing toolbar associated with our macros, or we can
create a new toolbar. To create a new toolbar, we press the New button and title the
toolbar “Filter” (see Figure 12.22).
Now we move to the Commands tab to add the buttons for our macros to our new
toolbar. In the Commands tab, we scroll down the Categories list until we find Macros
(see Figure 12.23). There are two command options for Macros: Custom Menu Item,
which we will use later, and Custom Button.
19
Chapter 12: Recording Macros
Figure 12.23 The Macros category on the Commands tab offers two command options.
To add a button to our new toolbar,we click and drag the Custom Button to the toolbar.
The new button is automatically selected from the toolbar and the Modify Selection
option then becomes available on the Customize window. We now select Modify
Selection to specify the name of the toolbar button, edit the button icon, and assign a
macro to the button action. Let’s first create a button for viewing the Hong Kong flights
on this Filter toolbar. We select Modify Selection and type the name “HongKong” (see
Figure 12.24).
20
Chapter 12: Recording Macros
Figure 12.24 The Modify Selection option allows us to edit the name and icon as well as
assign a macro function.
We then use the Change Button Image option to transform the icon into an arrow.
Finally, we select Assign Macro in order to see a list of macros from this workbook (see
Figure 12.25).
Figure 12.25 The Assign Macro window provides a list of all available macros.
From this list, we select “ViewHongKongFlights.” We now have a new button on our new
toolbar that can filter the airline data for flights to Hong Kong (see Figure 12.26).
21
Chapter 12: Recording Macros
We repeat these steps to add buttons for viewing Beijing flights, viewing all flights,
viewing economy flights, and viewing business flights. Our final Filter toolbar appears in
Figure 12.27.
Figure 12.27 Adding four more buttons to the Filter toolbar. The Economy button is
currently selected.
We can also assign our macros to a new menu option. To do this, we begin again by
choosing View > Toolbars > Customize from the menu. This time we can go directly to
the Commands tab and scroll down to the New Menu category (see Figure 12.28).
22
Chapter 12: Recording Macros
Figure 12.28 Using the New Menu category and command to create a new menu
option.
All we do is click and drag the New Menu command to the menu at the top of the Excel
window. We can again choose the Modify Selection option to enter the name that will
appear in the main menu, in this case, “Sorting” (see Figure 12.29). We will not assign a
macro at this point since we want to include both sorting macros as drop-down items
from this menu option.
Figure 12.29 Naming the new menu option as Sorting by using the Modify Selection
option.
Now, we will return to the Macros category in the Commands tab (as in Figure 12.23)
and, this time, select Custom Menu Item from the list of commands. We click and drag
this command to the new menu option and place it in the automatic drop-down list that
appears. To enter the name of this menu option, “Stops,” we again use the Modify
Selection option. We then choose an icon and assign the “SortByStops” macro (see
Figure 12.30).
23
Chapter 12: Recording Macros
Figure 12.30 The first option in the Sorting menu is to sort by number of stops.
We then repeat these steps to add a “Price” option to the Sorting menu (see Figure
12.31). These examples show how easy it is to assign macros to customized toolbars
and menu options. Again, customizing toolbars can be an excellent tool in building a
solid user interface.
Figure 12.31 The final Sorting menu has the two options to sort by Stops and by Price.
12.4 Summary
¾ Macros are technically defined as units of VBA code. In Excel, a macro can be
thought of as a means to automate a series of actions in a spreadsheet
application. Macros can either be created directly with VBA code in the Visual
Basic Editor, or recorded in Excel.
¾ To record a macro, we must know exactly the actions that we wish to perform and
then use the Macro Recorder. Then, we can translate standard Excel
functionality into VBA code.
¾ Steps to Record a Macro: 1. Choose Tools > Macros > Record New Macro or
choose the Record icon from the VBA toolbar; 2. Perform a sequence of actions
24
Chapter 12: Recording Macros
in Excel; 3. Choose Tools > Macros > Stop Recording or choose the Stop icon
from the VBA toolbar.
¾ Main Sub Procedure Code:
Sub SubName()
… (code here) …
End Sub
¾ Some Objects, Methods, and Arguments used: Object = Range, Method = Select,
Object = Selection, Method = AutoFilter, Arguments = Field, Criteria, Object =
Selection, Method = Sort, Arguments = Key, Order.
¾ Events are actions that can take place in the Excel window that cause an action to
occur in the VBA code. We connect events to code by using Event Procedures.
Examples of event procedures include Click, Change, and Activate.
¾ An event procedure is just like a sub procedure, except that it is associated with a
particular action of an Excel object. Instead of asking a user to run a macro to
perform the filtering discussed above, you may want to have some buttons on the
screen that will perform corresponding macros when clicked. To do this, you
would first create the button, then use a Click event procedure. To create a button
in Excel, you need to use the Control Toolbox.
¾ Creating buttons as a user interface can greatly aid a user unfamiliar with macros
to perform desired functions. Another useful way to help a user with macros is to
create a toolbar with icons designated for each macro. This technique is called
customizing a toolbar.
12.5 Exercises
12.5.1 Review Questions
1. The following table presents row headings that will be used to create a table in
an Excel worksheet. Create a macro that does the following: copies the cells
from their current location, pastes them at the range C1:C5. Assign this macro to
a button.
25
Chapter 12: Recording Macros
3. Create a macro that takes the row headings presented in hands-on exercise 12.1
and transposes them to column headings.
4. A quality engineer has prepared a control chart to monitor the thickness of circuit
boards being produced at the plant. The following data is collected through the
random measuring of 3 circuit boards where each lot is sampled over the course
of one month.
The upper and lower control limits for the X-bar chart are calculated as follows:
X ± 1.023 * R
The upper and lower control limits for the R-bar chart are calculated as follows:
UCL = 2.575 * R and LCL = 0 * R
Create a macro to conditionally format the data in the table presented above. Set
the formatting condition for cells B2:D11 so that if the value in one of the cells is
not between the control limits for X-bar chart, the text font of the cell will be
bolded and the cell will fill yellow. Set the formatting condition for cells F2:F11 so
that if the value in one of the cells is not between the control limits for R-bar
chart, the text font of will be bolded and the cell will fill red.
5. A professor has an excel file with a sheet for each of the classes he is teaching
this semester. Each sheet contains the following information on all of his
students:
Last Name First Name Absences Test Scores Quiz Scores Total Grade
26
Chapter 12: Recording Macros
The professor would like to create the following macros with shortcut keys:
6. Often when class work is done in Excel, the professor requests that the final
answer of a problem be boxed in. Create a macro that will box in a selected cell
and assign this macro to a button in a toolbox so that the program can be easily
accessible.
United States, Great Britain, Canada , European Union, South Africa, Australia,
Brazil, New Zealand, China, and Mexico.
8. Create a macro for finding the sum, mean, and standard deviation for any set of
numbers placed in Column A. What values result when this macro is run for the
following sets of numbers?
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{8, 6, 7, 5, 3, 0, 9}
{1776, 1234, 378, 2521, 2020, 1492, 1453, 1126}
9. Write a procedure that selects cells the data in range A1:A6, and creates a copy
in cells B1:B6. The data in cells B1:B6 should be such that: the first letter of
phrases/names is capitalized, the data is arranged numerically and then
alphabetically, and finally the data type is displayed next to each cell (C1:C6).
10. The workers on an assembly line in a computer manufacturing facility have been
complaining about the temperature of the facility. The facility’s industrial engineer
has recorded the productivity of workers at various temperatures to find the
optimal temperature for production. Record a macro that will make a line graph of
the following table, give it a title, label the axis, and remove the legend. (Refer to
Chapter 5 for help with charts.)
27
Chapter 12: Recording Macros
11. A consumer agency ran a test on different brands of concrete to determine which
brand of concrete is the strongest and most dependable. The different brands of
concrete were used to make 10 solid cylinders each. A concrete compression
machine determined the amount of force in pounds that the cylinders could hold.
Record a macro that gives the average, minimum, and maximum values for each
brand of concrete, then makes a bar graph that includes the average, min, and
max for each brand of concrete, and labels the legend appropriately.
12. Consider the data given in hands-on exercise 10.9. Create a macro to find the
following:
Create four command buttons. Assign each macro to one of the buttons created.
a. Record a macro called “Scramble” that scrambles all of the rows of the table
above, so that after running the macro, no row is in the same position that it was
just in. Create a button on the sheet and assign the macro to it.
28
Chapter 12: Recording Macros
b. Record a macro called “QuarterAverages” that lists the average profits for each
quarter. Consider that after running the macro created in part a, the current
position of the rows will change. Therefore, the macro should give the average
profits for each quarter independent of the location of this information. Create a
button on the sheet and assign the macro to it.
c. Record a macro called “BranchAverages” that list the average profit for each
branch. Consider that after running the macro created in part a, the current
position of the rows will change. Create a button on the sheet and assign the
macro to it.
You should now be able to scramble the rows by any amount, and still use your
recorded macros to find the correct average for each quarter and for each
branch.
15. John owns an ethnic food restaurant. The waiters and hosts have mentioned few
customer complaints about the quality of food and service. John has decided to
find out the most common customer complaints. He left comment cards in each
table asking the customers to write down any complaints that they have. The
following is a collection of complaints obtained from the comment cards:
Complaint Tally
Burnt Food 14
Cold Environment 16
Smoky Air 12
Long Wait for
Food 25
Poor Selection 3
Undercooked
Food 4
Too Noisy 6
Record a macro that will create a Pareto chart with the given information. Create
a button and assign the macro to it. Comment on the results from Pareto Chart.
16. Place a scroll bar in the spreadsheet. Assign it to cell B3. Modify the properties
so that any movement, or change, in the scroll bar will change the value in B3 to
be any even number between 2 and 20.
17. Create three circles on the spreadsheet and format them to be black. Label them
“Go,” “Caution,” and “Stop,” respectively. Assign a macro to each, such that:
a. when the “Go” button is pressed, it turns green and the others turn black;
b. when the “Caution” button is pressed, it turns yellow and the others turn
black; and
c. when the “Stop” button is pressed, it turns red and the others turn black.
18. Create a new toolbar called “Zoom.” Make one button that zooms in by ten
percent each time it is pressed (example: 75 > 85 > 95 percentages). Create
29
Chapter 12: Recording Macros
another button that zooms out each time it is pressed (example: 95 > 85 > 75
percentages). Assign both buttons to the toolbar.
19. Record a macro that takes the selected data below, creates a graph, and adds a
Linear trendline.
20. Create your own procedure that will graph the selected data below and add an
exponential trend curve. (Hint: Use the recorded macro from the above problem
and reduce the code to only that which is needed.)
Year Sales
1 70
2 183
3 340
4 649
5 1243
6 1979
7 4096
8 6440
9 8459
10 12154
Create another procedure for graphing data and creating a Power trend curve.
Create three buttons on the spreadsheet: Linear Curve, Exponential Curve, and
Power Curve. Also display the R-squared value. Use these buttons to quickly
analyze which curve best fits a given set of data.
21. Consider the database given in hands-on exercise 10.10. Record macros to do
the following:
a. Sort the data by date, and within each date sort the data by product name.
b. Calculate the total, average and standard deviation of the number of units
sold.
c. Calculate the total, average and standard deviation of the dollar amount
made.
d. Calculate the total, average and standard deviation of the dollar amount
made per region.
30
Chapter 12: Recording Macros
22. Record macros that randomly generate data from the following distributions:
Create buttons and assign the macros recorded. Create a toolbar called
“Rand. Generator”. Assign the macros to the customized toolbar.
c. The payment amount if the loan would have to be paid in 5 equal yearly
payments at 5% interest rate.
d. The number of years would take to pay off the loan given that the interest
rate is 10% and the company makes equal yearly payments of $100,000.
31
Chapter 12: Recording Macros
Create buttons and assign the macros recorded. Create a new menu item
called “Trig Functions”. Set the macros created in this menu.
25. Given a set of data located in column A of a spreadsheet, record macros to find:
the maximum, the minimum, the average, the total, standard deviation of the data
as well as plots a bar chart. Create command buttons and assign the macros
recorded.
32
Chapter 13
More on Objects
Chapter Overview
13.1 Introduction
13.2 Objects and their properties and methods
13.2.1 Workbooks and worksheets
13.2.2 Ranges
13.2.3 Charts
13.2.4 Drawing objects
13.2.5 Application
13.6 Summary
13.7 Exercises
13.1 Introduction
Chapter 13: More on Objects
This chapter gives an overview of how to perform basic Excel tasks in VBA. We describe
several common properties and methods of various objects in Excel. We also illustrate
how to perform referencing and create names in VBA as well as how to use Excel
functions in VBA. This is a very important chapter for the reader to review since these
tasks will be performed often in a DSS. For example, since we are developing
spreadsheet-based DSS applications, the reader should know how VBA is used to
dynamically invoke any of the spreadsheet functions available in Excel. The objects,
properties, and methods in this chapter are used often in many of the DSS applications
we develop in Part III of the book.
Workbooks may not be a necessary object to manipulate if you are only working with
one; however, if you are working with multiple workbooks, you may need to know certain
methods and properties.
For example, we want to take some values from a workbook called “CH13 Workbook1”
and transfer them into a chart or another form of analysis in a different workbook, “CH13
Workbook2.” In “CH13 Workbook1,” suppose we have recorded the Revenue and Cost
of a particular company’s production over 12 months (see Figure 13.1). In “CH13
Workbook2,” we want to create two charts: Revenue vs. Cost for months 6 to 12, and
Profit for months 1 to 5 (see Figure 13.2). For the second chart, we will also need to
calculate the profit values.
Figure 13.1 “CH13 Workbook1” presents Revenue and Cost values for months 1 to 12.
2
Chapter 13: More on Objects
For the first chart in “CH13 Workbook2,” we will need to copy the data in cells A9:C15 of
“CH13 Workbook1” and paste it into cells A5:C11 of “CH13 Workbook2.” Let’s do this
using VBA code. We will type our code in a module in “CH13 Workbook2,” since this is
our final workbook in which the charts will be created. First, we insert a new module by
clicking Insert > Module or by using the Insert Module icon in the toolbar. Now, we create
the sub procedure and give it a name:
Sub WorkbookDemo()
End Sub
Before copying the data, we need to specify which workbook is active. An active
workbook is the workbook on which every following line of code will be performed. To
make a workbook active, we use the Activate method. We want to activate “CH13
Workbook1” first so that we can copy the data. So, we type:
Workbooks(“CH13 Workbook1”).Activate
Notice that we have written the object in the plural, as “workbooks”. Also note that we
could have typed the full extension of the workbook name if it was not in the same
directory as our other workbook: Workbooks(“../CH13 Workbook1.xls”).Activate. Figure
13.3 illustrates the drop-down list of methods and properties for the workbook object
where you can find Activate.
3
Chapter 13: More on Objects
Figure 13.2 “CH13 Workbook2” with two charts, one for Revenue vs Cost and another
for Profit.
Figure 13.3 Selecting the Activate method of Workbooks from the drop-down list of
methods and properties.
Now that we have activated the appropriate workbook, we need to copy the required
cells of data for the first chart. We use the Range object to refer to the cells we want to
copy, and then we use the Copy method to copy them. We will discuss more methods
and properties of the Range object in the next section.
Range(“A9:C15”).Copy
Now we need to paste this data into “CH13 Workbook2”; however, we must first activate
this workbook before referring to any cells in it. So, we again use the Activate method:
Workbooks(“CH13 Workbook2”).Activate
Now that it is active, we can use the Range object again and the PasteSpecial method.
This method takes an argument that specifies how the data should be pasted (see the
drop-down list in Figure 13.4). We will use the argument xlPasteAll to paste the data as
is. Note that there is another way to copy and paste data in VBA using the Application
object. We will discuss it in detail in the next few sections.
Range(“A5:C11”).PasteSpecial xlPasteAll
4
Chapter 13: More on Objects
Figure 13.4 The dialog box and drop-down list that appear to find an argument for the
PasteSpecial method.
Using the same methods, we can now type the VBA code to copy and paste the data for
the second chart. See Figure 13.5 for the final code. Let’s now go to Excel in “CH13
Workbook2,” to test our macro. After playing the WorkbookDemo macro, “CH13
Workbook2” should have all the necessary data, as seen in Figure 13.6.
Figure 13.5 Code that copies two different sets of data from one workbook to another.
The workbook “CH13_Workbook2” can now be used to create charts or perform further
analysis. Note that the Profit column in the second table of this workbook still has to be
calculated. For now, we can simply type the formula “=B23 – C23” into cell D23 and
copy the formula to the cells below. At the end of this chapter, we will discuss how to
enter formulas using VBA.
5
Chapter 13: More on Objects
Figure 13.6 “CH13 Workbook2” after the macro WorkbookDemo has run.
Like workbooks, the most commonly used method of worksheets is Activate. As you
work with different worksheets, you need to activate whichever one you are currently
referencing or operating. For example, say you have three worksheets: “Welcome” with
just text; “Input” with a table of input data; and “Analysis” with charts or other forms of
analysis. If you want to copy some data from the “Input” worksheet, you need to activate
it before referencing any of its ranges or using the copy method. Then, to create a chart
or show the value of some function of that data on the “Analysis” worksheet, you would
need to activate the “Analysis” worksheet first.
Aside from the Activate method, a very common property of worksheets is Visible. The
Visible property takes the value True or False to indicate whether or not the worksheet
(along with the worksheet tab at the bottom of the screen) can be seen by the user. This
property can be useful when preparing a decision support system, as it is part of good
navigation in GUI design. Using the same worksheets in the above example, we may, for
instance, want users only to view the “Welcome” worksheet first. We could then allow
them to view the “Input” and “Analysis” worksheets after clicking a command button.
In the case studies in Part III of this book, we use a subroutine associated with the Open
event procedure of the workbook that hides all worksheets except “Welcome”. While
there are several ways to accomplish this, we will now discuss the simplest. (In our
code, we actually define a variable of the data type object and use the For, Next and If,
Then structures, which we will discuss in the next few chapters.)
6
Chapter 13: More on Objects
We first go to the VBE and double click on Workbook from the Project Explorer window.
Then, we click on the drop-down list of event procedures and choose Open (see Figure
13.7). The sub procedure Workbook_Open() immediately appears. (We will discuss the
difference between Private and regular sub procedures in Chapter 16.)
Now we want to enter our code. We first need to make sure that the “Welcome”
worksheet is visible. To do this, we type the following:
Worksheets(“Welcome”).Visible = True
Notice that we can refer to the Project Explorer for the names of all worksheets in the
workbook. Next, we want to make sure that both the “Input” and “Analysis” worksheets
are hidden. So, we set their Visible properties to False.
Worksheets(“Input”).Visible = False
Worksheets(“Analysis”).Visible = False
Since worksheets are objects defined in VBA, once we type the period after the
statement Worksheets(“..name..”), we see a drop-down list of properties and methods.
From here, we can also find the Visible property (see Figure 13.8). Figure 13.9 presents
the complete code. We can go to the Excel window to test the macro.
7
Chapter 13: More on Objects
Figure 13.8 The Workbook_Open sub procedure makes the “Welcome” worksheet
visible and hides the “Input” and “Analysis” worksheets.
We can use the Visible property to hide and show worksheets as we navigate the user
through the program. We will discuss this process in more detail in Chapter 19.
Both the workbook and worksheet objects also have the Name property. It can be used
to assign a name to a workbook or worksheet. For example, if we wanted to change the
name of the “Welcome” worksheet to “Introduction,” we would type the following in VBA:
Worksheets(“Welcome”).Name = “Introduction”
This property can also be used for the Range object, but we will discuss this in more
detail in a later section.
If desired, you can use the Sheets object instead of Worksheets. It allows you to refer to
a Chart sheet as well as a worksheet. Both have the same set of properties and
methods.
There are additional properties and methods available for the workbook and worksheet
objects; however, you will most often use those that we have discussed.
13.2.2 Ranges
The Range object is probably the most commonly used in Excel. You have already seen
the Name property and the Copy and PasteSpecial methods used earlier with the Range
object. You also learned about the Sort and AutoFilter methods used in Chapter 12. We
now want to review a few more important properties and methods.
One more common method is Cut. After cutting a cell or range of cells, you can use the
PasteSpecial method to paste the values in any worksheet or any workbook. However,
the Cut method also has its own paste feature: the Destination argument. You can cut
and paste in one statement as follows:
8
Chapter 13: More on Objects
Range(“A1:F12”).Cut Destination:=Range(“A13”)
The Destination argument can take any range from any worksheet or workbook as its
value.
Many of the properties for the Range object can be used to adjust formatting. For
example, to change the color of any range of cells, we use the Interior property. When
we enter a range in VBA and then choose the Interior property, we type another period
after Interior to see a list of sub properties (see Figure 13.9). From this list of sub
properties, we chose ColorIndex and enter a number signifying a specific color, or
Color and enter either a VB Constant (vbRed, vbYellow, etc) or a specific color using
the RGB function. (For a list of color indices, search for PatternColorIndex in VBA Help;
for a list of common VB Constants for colors, search for Color Constants in VBA Help;
for a list of common RGB values, search for RGB Function in VBA Help.)
We could enter either of the following lines of code to format our range to have red cells:
Range(“A1:F12”).Interior.ColorIndex = 3
Range(“A1:F12”).Interior.Color = vbRed
When we type either line in the VBE and then run the macro in Excel, the
worksheetappears as we have shown in Figure 13.10 with the “Welcome” worksheet
from the earlier examples.
Another useful sub property of Interior is Pattern. You can choose xlSolid, xlChecker,
and others as values to this sub property. We encourage you to experiment with them.
Figure 13.9 The Interior property has a set of sub properties that can be used to
format ranges.
9
Chapter 13: More on Objects
example, you may want to explain what a certain sub procedure’s function is to another
user using your code. You could simply type the ‘ mark, followed by a brief text
description of your code. This is beneficial to reminding yourself of what is being done
for future reference or debugging and also for communicating to other programmer who
may read your code what you have done. We discuss commenting and other
programming principles in more detail in Chapter 24.
Figure 13.10 Both the ColorIndex and Color sub properties can be used to format the
interior of ranges.
A common formatting modification in Excel involves the borders of a range. This format
setting is often used when creating tables and organizing data in the Excel spreadsheet.
There are a few ways to change range borders. One way is to use the Borders property.
Let’s create a small table on the “Input” worksheet in the above example. We begin by
creating our sub procedure in VBA; we will name it Borders(). First, we activate the
“Input” worksheet using the method discussed in section 12.1.1. Then, we specify the
range where we want our table to be located, followed by the Borders property (see
Figure 13.11). If we type another period after the Borders property, we see a list of sub
properties. These are specific formatting options for the border of the range, such as
LineStyle, Color, and Weight. Let’s begin by setting the border weight of our range to
xlThick, a predefined XL Constant:
Range(“B3:D10”).Borders.Weight = xlThick
If we now run our macro in Excel, we should see the table shown in Figure 13.12.
10
Chapter 13: More on Objects
Figure 13.11 The Borders property has a list of sub properties available.
Figure 13.12 This range has the Borders property with the Weight sub property value
equal to xlThick.
Now we can try setting some other sub property values. For example, let’s make the
LineStyle of the first row of our range dashed instead of solid by typing:
Range(“B3:D3”).Borders.LineStyle = xlDash
We have again used an XL Constant, xlDash, to specify the desired line style. Let’s also
modify the color of the second and third row’s borders by using the Color sub property.
We make this section of border yellow by typing:
Range(“B4:D5”).Borders.Color = vbYellow
11
Chapter 13: More on Objects
See Figure 13.13 for the final code and Figure 13.14 for the result of the macro in Excel.
Instead of typing a period after the Borders property of the Range object, we can type an
open parenthesis and receive a new list of XL Constants (see Figure 13.15). These
constants allow us to specify what section of the border we want for our range. For
example, if we chose xlEdgeBottom, we will only have a border on the bottom of the
entire range.
12
Chapter 13: More on Objects
Figure 13.15 Borders can also take predefined xl values to convey which set of borders
should be modified.
Let’s specify a new range and set the Borders property to xlInsideHorizontal so that each
row of our range has a line above and below it. Now that we have selected
xlInsideHorizontal from the drop-down list, we can further specify the same sub
properties we used above by typing another period. Let’s set the weight of this border to
xlThick.
Range(“B12:E13”).Borders(xlInsideHorizontal).Weight = xlThick
See Figure 13.16 for the modified sub procedure and Figure 13.17 for the result of the
modified macro in Excel.
13
Chapter 13: More on Objects
Figure 13.16 The last line of code in the Borders() sub procedure specifies the Weight
value for the horizontal border line.
The other way to specify the desired border formatting is a method instead of a property;
it is the BorderAround method. BorderAround has arguments similar to the sub
properties of the Borders property; these are LineStyle, Weight, and Color. As with
other methods, the structure for entering arguments uses :=. Take, for example, the
following line of code:
Notice in Figure 13.18 that the VBE provides us with a list of XL Constants for this
argument’s values. In Figure 13.19, we have also added the argument
Color:=RGB(0,0,255). This argument uses either the RGB Function or a VB Constant to
14
Chapter 13: More on Objects
specify the color. Here, we have given the RGB values for the color blue. See Figure
13.20 for the final Borders() macro result.
Note that the limitation of the BorderAround method is that it only affects the outlining
border of the range and can not add borders inside the range [as with the property
Borders(xlInsideHorizontal) for example].
Figure 13.18 The BorderAround method has arguments for LineStyle, Weight, and
Color.
15
Chapter 13: More on Objects
Figure 13.19 The Borders() sub procedure now includes the BorderAround method for
a given range.
There are two more basic properties of the Range object that we would like to discuss.
The first, Value, is a very important property, as it allows you to assign the value of what
will actually be stored in the range. This value can be a string of text, a numerical value,
a formula, or a variable value. We will discuss formulas later on in this chapter and
variables in the following chapter, but for now let’s consider an example of setting the
range value to a string of text or a numerical value.
Suppose we want to list some numerical results in a table on an “Analysis” sheet from
above examples. We first want a title for the table: “Analysis Sheet.” To enter this text
16
Chapter 13: More on Objects
into a cell using VBA, we simply specify the range we want and use the Value property
as follows:
Notice that since the value of this cell is text, we include it in quotation marks. However,
for numerical values, or variable names, no quotation marks are necessary. Let’s now
enter two values, a sum and an average, in the table. The first value can be entered as
follows:
Range(“B3”).Value = 350
In Figure 13.21, we have entered row titles for these two values and the values
themselves in a sub procedure called ValueFont(). See Figure 13.22 for the macro result
in Excel.
Figure 13.21 Assigning text values and some numerical values to different ranges.
The last Range property we will discuss is the Font property. The Font property also has
a list of sub properties (see Figure 13.23). Some of the more commonly used sub
properties are Bold, Size, and Color.
17
Chapter 13: More on Objects
Suppose we want to make the title of the table we just created in the ValueFont() sub
procedure bold. The Bold sub property takes the values True and False. We therefore
name the range where the title is, use the Font property, and set the Bold sub property
equal to True.
Range(“A2”).Font.Bold = True
We may also want to make the title have a larger font. To do this, we simply increase the
value of the Size sub property.
Range(“A2”).Font.Size = 14
We can also make the numerical values in our table red. We could use either the
ColorIndex or Color sub properties to do this. If we are using the Color sub property, we
set the value equal to an RGB function or a VB Constant. Let’s use the VB Constant
vbRed for this example:
Range(“B3:B4”).Font.Color = vbRed
See Figure 13.24 for the modified ValueFont() sub procedure. The modified macro result
is shown in Figure 13.25.
18
Chapter 13: More on Objects
Figure 13.24 The Font sub properties of Bold, Size, and Color.
Aside from these common properties, the Range object additionally offers a set of
methods that are very useful. We have already discussed Copy and PasteSpecial, which
we will use quite often, as well as Sort and AutoFilter, which are also quite useful.
Additionally, there are three common methods that can be used to clear cells. These are
the Clear, ClearContents, and ClearFormats methods. (There are others, but these
are the three most common.)
Let’s use the table we created in the “Analysis” worksheet to experiment with these
methods. To do so, we create a sub procedure ClearCells() and again activate the
“Analysis” worksheet. (Remember that this step is important because even if this macro
is run from another worksheet, it will only clear cells from the “Analysis” worksheet.) Let’s
begin by clearing the formatting we did to the numerical values (see Figure 13.26). To do
this, we use the ClearFormats method.
Range(“B3:B4”).ClearFormats
Running this macro will demonstrate that the numerical values are no longer red, but
rather the default color of black.
19
Chapter 13: More on Objects
Figure 13.26 The drop-down list of properties and methods for the Range object
displays all of the clear methods.
There is an important difference between the next two methods: ClearContents and
Clear. ClearContents removes all values from the specified range. However, that is all it
removes; all of the formatting will stay as it is. This method is useful if we want to retain
the formatting for a particular range. For example, if we want to change the title of our
analysis table, we can use the ClearContents method, since we still want the new title to
be bold and a larger font. In Figure 13.27, we have cleared our current table title using
the ClearContents method and given the title range a new value.
Range(“A2”).ClearContents
Range(“A2”).Value = “Results”
Figure 13.27 Using the ClearFormats method to change the range font formatting. The
ClearContents method only clears the range value.
20
Chapter 13: More on Objects
Figure 13.28 The result of the ClearCells() macro. The numbers are no longer red and
the table heading has been changed; however the formatting of the table heading is the
same.
The Clear method, on the other hand, clears everything in the specified range, including
values, formatting, formulas, and comments. We have reentered the new title code using
the Clear method instead of the ClearContents method to show the difference (see
Figure 13.29).
Range(“A2”).Clear
Range(“A2”).Value = “Results”
Note that when this macro is run, the new title is no longer bold or a large font (see
Figure 13.30).
Figure 13.29 The Clear method clears all formatting and values from the range.
Figure 13.30 The result of the modified ClearCells() macro. Note that the formatting of
the table heading has been cleared.
21
Chapter 13: More on Objects
Also associated with formatting the Range object is the FormatConditions object, which
places conditional formatting on a specified range of cells. Therefore, hierarchically, it is
related to a specific Range object. There are three main methods and several properties
for this object. To apply conditional formatting to a range, use the Add method. Its
format is as follows:
The Type argument takes two values: xlCellValue or xlExpression. These argument
values refer to the two conditional formatting options of CellValueIs or FormulaIs
respectively. The Operator argument, which determines the inequality for the conditional
formatting comparison, has several values: xlLess, xlLessEqual, xlGreaterEqual, and
xlBetween, among others. The Formula argument is the right hand side of the inequality
of this comparison. For example, if you want to format a range of cells to be red if their
values are less than 10, the Formula argument value would be 10 (and the Operator
argument value would be xlLess). There can be up to two Formula arguments. (For
example, you would need two when you have the xlBetween Operator.) Note that you
can only add three conditional formats to one range of cells; therefore, if you use the
Add method more than three times, it will not work.
The second method is Modify. Use this method to modify a conditional format that has
already been added. In case there are more than one format on a range of cells, you
must first index the FormatConditions object to specify which format to modify. The
arguments for the Modify method are the same as the Add method. The values you allot
to the arguments using the Modify method overwrite all initial or previous values. The
format is below:
22
Chapter 13: More on Objects
Once you have added a conditional format, you must then use the properties of this
object in order to determine what you want the formatting to be after the condition has
been met. These properties are actually the same formatting properties we have
discussed already for the Range object: Interior, Font, and Borders. These properties,
along with their sub properties, can be used with a particular FormatConditions object,
as follows:
Let’s look at a quick example to understand how these methods and properties are put
together. In Figure 13.31(a), in the range C1:C10, there is a list of numbers between 1
and 20. If we want to place a conditional format on this range so that any cell with a
value less than 10 becomes red, we type the following code:
The result of running a procedure containing this code is portrayed in Figure 13.31(b).
(a) (b)
Figure 13.31 (a) A list of ten numbers between 1 and 20. (b) Applying the conditional
formatting to display values less than 10 as red.
There are other basic properties and methods for the Range object. We recommend you
search through the Object Browser in the Range class for a full list of options.
Remember, another easy way to learn how to find a particular VBA code is to record a
23
Chapter 13: More on Objects
macro first and then simply view the generated code (as we demonstrated in Chapter
12). You can then modify this code according to your specific formatting desires.
13.2.3 Charts
There are many aspects of the chart object that can be modified. In Chapter 5, we
created charts by deciding the chart type, data source, possible data series, and several
chart options. All of these parts of the chart object can be modified directly in the VBA
code.
Charts.Add
To set other options for the chart, we actually use the ActiveChart object rather than the
Chart object. With the Chart object, we are limited to methods such as Copy, Delete,
and Select. Therefore, we use the ActiveChart object to specify the chart type, source
data, and other chart options because we need to specify a specific chart before
modifying it. So, we either Add, or, more commonly, Select a chart to activate it before
we use any methods or properties.
Let’s now set some of the chart options that we would select using the Chart Wizard.
First, we need to determine what type of chart we want: bar graph, pie chart, or
scattered chart, etc. To do this, we use the ChartType property, which takes XL
Constants as values. In this example, we want a bar graph, which is designated by
xlColumnClustered.
ActiveChart.ChartType = xlColumnClustered
Now we need to select the source data for this chart. To do so, we use the
SetSourceData method. This method has several arguments, but we only need to use
two for now: Source and PlotBy. We use the Source argument to specify the location of
our data, giving both the worksheet and range names. The PlotBy argument, as you may
remember from the Chart Wizard, allows us to choose whether we want to plot our data
by its columns or rows. In this case, as in most, we want to plot by columns. For our
examplelet’s use the SetSourceData method as follows:
Notice that we have used the Sheets object instead of the Worksheets object here to
locate our data (refer to Section 13.1.1). The third main option we should now decide
upon is our chart location. As you may remember from Chapter 5, we can either have
our chart appear in a new Chart sheet or as an object in any specified worksheet. To
specify the chart location, we use the Location method. This method takes two
arguments: Where and Name. The Where argument can take one of three XL Constant
values: xlLocationAsObject, xlLocationAsNewSheet, or xlLocationAutomatic. The third
option is simply your current default. The Name argument is the text name of the
24
Chapter 13: More on Objects
worksheet or the chart sheet where the chart will be displayed. For our example, we
keep our chart as an object in the “Charts” worksheet.
We have now completed specifying the necessary options of a basic chart. These three
options − chart type, source data, and location − are needed to create any chart. We can
now modify some of the additional chart options. Most of these options are controlled by
properties of the ActiveChart object. There are several of these properties, but we will
demonstrate just a few.
Let’s begin by opting to hide the chart legend. To do this, we use the HasLegend
property, which takes the value True or False. To hide the legend, we type:
ActiveChart.HasLegend = False
Another option is whether or not to title the chart, and if so, what that title is. We can set
this option in VBA using the HasTitle and ChartTitle properties of the ActiveChart
object. The HasTitle property also takes True and False values; however, the ChartTitle
property has a set of sub properties that can be given specific values. Before assigning a
title to the chart, we should first set the HasTitle property to True.
ActiveChart.HasTitle = True
The ChartTitle property has several sub properties, including Font, Border, Interior, and
other formatting options. For this example, we are interested in the Text sub property.
This sub property allows us to enter the text that will become the title of our chart; in this
case, we name the chart “Annual Report.”
See Figure 13.31 for the completed Graph() sub procedure; the resulting chart is shown
in Figure 13.32. Notice that the chart type is a bar graph and that the highlighted data
range is our source data for the chart. Also take note of its location as an object in the
“Charts” worksheet, the title, and the hidden legend.
25
Chapter 13: More on Objects
Figure 13.32 The Graph() sub procedure creates a chart and sets some properties.
Figure 13.33 The resulting chart created by the Graph() macro has the chart type,
source data, location, and chart options set by the VBA code.
There are some other useful methods and properties of charts that allow further
modification of formatting and data analysis. For example, to use a chart type that is not
from the standard list, we can use the method ApplyCustomType. Let’s convert our
graph from the above example into a 3D bar graph. To do this, we set the ChartType
argument of the ApplyCustomType method to “xl3DcolumnClustered.”
ActiveChart.ApplyCustomType ChartType:=xl3DColumnClustered
26
Chapter 13: More on Objects
There are some other argument values for this method that you can experiment with.
Another important method of the ActiveChart object is SeriesCollection. The
SeriesCollection method first requires a numerical indication of which series should be
manipulated. In our example, we only have one series of data, so we specify it by typing:
ActiveChart.SeriesCollection(1)
ActiveChart.SeriesCollection(1).Interior.ColorIndex = 37
See Figure 13.33 for the final Graph() sub procedure and Figure 13.34 for the results of
the modified macro.
27
Chapter 13: More on Objects
There are other properties and methods of the Chart object, or ActiveChart object, that
can be useful. We recommend that you experiment on your own by using the above
examples as a reference.
Quick Programming Note: If you are creating an application in which a user generates
a chart based on his or her data, you will need to ensure that your chart is dynamic; that
is, it does not have a fixed Data Range for its Source Data. To make such a dynamic
application, we do not recommend adding a new chart in your code. If you do this, you
will have multiple charts in your spreadsheet cumulating each time a user runs your
application. Even if you select and delete any previous charts before creating a new one,
this approach would also require several lines of code to set the chart details upon each
creation. The idea in building a dynamic application is to try to minimize coding as much
as possible by doing some preparation in the spreadsheet.
28
Chapter 13: More on Objects
We recommend creating a chart in Excel first and then just selecting and modifying the
source data in the code. This way, you can set all the details/formatting of the chart in
the spreadsheet without several lines of code. Once you have created and formatted the
chart (set to any temporary range of source data), note its name or shape index on the
spreadsheet. (Find the shape index by noting how many shapes, buttons, or other
drawing objects were already in the spreadsheet when you added the chart. The
indexing begins at 1.) Once you have found the name or shape index, the only code you
need in order to make the chart dynamic is to select the chart and modify its source
data:
By using this approach, the user can input any number of values to be charted. Then,
using the End property or another method to select this input range, you can set this
range as the Source argument value in the SetSourceData method.
Drawing objects, or shapes, can be useful to help the user visualize a problem scenario
or a suggested solution. You can use the Drawing Toolbar to draw a variety of shapes
on the spreadsheet. These objects can be formatted using VBA code.
Before formatting specific drawing objects though, it is a good idea to name them.
Naming drawing objects can be important when you want to search through several
different shapes and modify a particular one, or a few. Drawing objects, or shapes, can
be named either in Excel by using the Insert > Name menu options as discussed in
Chapter 3, or in VBA.
For example, let’s consider a small graph, or network, consisting of five nodes and five
arcs. To draw this network, we first choose a circular object from the AutoShapes on
the Drawing Toolbar, place it on the spreadsheet, and copy and paste it four times.
These are our five nodes (see Figure 13.35). Now, to draw the arcs, we use a line object
from the AutoShapes, place it on the spreadsheet, copy and paste it four times, and then
situate each between a pair of nodes (see Figure 13.35).
29
Chapter 13: More on Objects
Figure 13.36 Creating a network consisting of five nodes and five arcs by drawing
circular objects and line objects from the Drawing Toolbar.
Let’s first name our objects using Excel. To do so, we select the first and the last node in
the network by holding down the Shift key. Then, we group them by right-clicking and
choosing Grouping > Group from the drop-down menu. Next, with this new group
selected, we click Insert > Name > Define from the menu options or simply type the
name directly in the name window in the upper-left hand corner of the window. Let’s
name this set of objects “Tnodes.” Repeating this method, we now select the remaining
three nodes, group them, and name them “Inodes.” Now, we select all of the arcs, group
them, and name them “Arcs.” If we select any of these sets of objects, we see the
appropriate name appear in the name window. (For example, the “TNodes” are selected
in Figure 13.35.)
We can also name drawing objects in VBA using the Shapes object and its properties
and methods. As a quick example, let’s draw a simple circle from the AutoShapes. We
click on the circle to view the name that appears in the name window; in our case, the
Excel-generated name is “Oval 21.” Let’s now go to the VBE and create a new sub
procedure called Network(). First, we activate the relative worksheet, “DrwgObj,” that we
are working on. Now, to access our shapes, we refer to the ActiveSheet that we are
working with and then type the following:
ActiveSheet.Shapes(“Oval 21”).Select
The Select method is necessary for shapes before applying further properties or
methods. We have now selected the circle that we drew in our spreadsheet. To name
this drawing object, we simply use the Name property on our Selection. Let’s call this
object “Test.”
Selection.Name = “Test”
See Figure 13.36 for the code in the Network() sub procedure. To view the result of
running this macro in Excel, see Figure 13.37. Notice that after running the macro, the
name “Test” appears in the name window when the circle is selected.
30
Chapter 13: More on Objects
Figure 13.37 Using ActiveSheet, Shapes, and Selection to apply the Name property to
the drawing object.
Figure 13.38 Naming the circle “Test” by running the Network() macro.
Quick Programming Note: Naming a drawing object can be done both in Excel and in
VBA; however, you should consider whether or not it is necessary to use VBA code to
name your objects. If you are writing a dynamic program, one in which your shapes have
not yet been drawn or require some user input, then you will have to use VBA to identify
and name the objects. However, if you are designing a program that allows the user to
manipulate previously created shapes, it is better to create the names in Excel to save
coding space and time.
To move shapes in the worksheet, you should cut and paste them to a range. However,
the Cut method used with shapes does not allow the Destination argument. To paste a
shape after cutting it, you must use the ActiveSheet.Paste method. This method also
uses the Destination argument. The upper left hand corner of the shape will be pasted in
the specified range. For example, in the following code, we have cut the “Test” circle and
pasted it in cell C1 to illustrate moving a shape:
ActiveSheet.Shapes(“Test”).Cut
ActiveSheet.Paste Destination:=Range(“C1”)
Now, let’s return to our previously drawn network. Since we have grouped and named
our shapes already in Excel, we do not need to create the names again with VBA. We
can go ahead and format our sets of shapes in the Network() sub procedure. First, let’s
change the color of the “TNodes” to red. To do this, we use the ShapeRange and Fill
properties. These properties include the sub properties of ForeColor and
SchemeColor. We set the SchemeColor to the color index of 10 to make the shapes’ fill
setting red.
ActiveSheet.Shapes(“TNodes”).Select
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 10
31
Chapter 13: More on Objects
We can similarly color the “INodes” grey by using the same properties; the color index
for grey is 22 (see Figure 13.38).
Figure 13.39 The Network() sub procedure modifies the formatting of the network
shape objects.
Now let’s format the arcs. We can modify the color, thickness, and ends of the line
drawing object. For this example, we first increase the line weight from the default of
0.75 to 3.0. To do this, we use the ShapeRange and Line properties. With the Line
property, we set the Weight sub property by giving the numerical value followed by a
number sign (#). We again begin the code by selecting the appropriate Shape object.
ActiveSheet.Shapes(“Arcs”).Select
Selection.ShapeRange.Line.Weight = 3#
There are other sub properties of Line that we can experiment with. To modify the ends
of the line drawing object, we can use six main sub properties: EndArrowheadStyle,
EndArrowheadLength, EndArrowheadWidth and BeginArrowheadStyle,
BeginArrowheadLength, BeginArrowheadWidth. The values of these sub properties
are MSO Values that are pre-defined by Excel. For this example, we change our lines to
have an arrow at the end. (That is, they all have arrows that point to the right of the
screen). To do this, we use the EndArrowheadStyle property and the
“msoArrowheadOpen” value.
Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadOpen
See Figure 13.38 for the final Network() sub procedure. Figure 13.39 depicts the result
of running this macro on our network in Excel.
32
Chapter 13: More on Objects
Again, there are other useful properties and methods that can be applied to drawing
objects; however, these will probably be the most common ones you use.
Fill ForeColor,
SchemeColor
End and Begin MSO Values
ArrowheadStyleLength,
Width
13.2.5Application
The Application object is useful for some common functions as well as some other
features for running VBA code. We will discuss the Application functions in a later
section. For now, we will illustrate three very useful Application properties and methods.
The first is the ScreenUpdating property (see Figure 13.40). This property, which takes
the values True or False, help your code run more efficiently. When the ScreenUpdating
property is set to False, none of the following actions in the VBA code will be reflected
on the Excel screen while the macro is being run. This property is a good programming
idea since it keeps users from seeing the screen flicker while each code item is
processed and therefore helps the code run faster. When you have reached the end of a
long sequence of code, you should remember to set the ScreenUpdating property back
to True so that the user can see the results of the macro on the screen.
33
Chapter 13: More on Objects
Figure 13.41 The Application object has several properties and methods, including the
ScreenUpdating property.
Let’s consider an example. Suppose we are creating a meal plan for each day of the
week. We have three possible daily meal plans, and we want to assign each one to the
days in the week (see Figure 13.41). Suppose we have randomly decided to copy and
paste the first meal plan to Days 1, 4, and 7, the second meal plan to Days 2 and 4, and
the third meal plan to Days 3 and 6.
We have a simple sequence of VBA code that will copy and paste each range
appropriately (see Figure 13.42). If we first run this macro without using the
ScreenUpdating property in the code, we can see the screen flicker as the copying and
pasting actions take place; we encourage you to experiment with this. We will go ahead
and add the following lines of code to the beginning and end of the macro:
Application.ScreenUpdating = False
Application.ScreenUpdating = True
If we run the macro again, we notice an increase in speed and an elimination of screen
flickering. See Figure 13.42 for the completed Applications() sub procedure and Figure
13.43 for the result of the macro.
34
Chapter 13: More on Objects
Figure 13.43 The Applications() sub procedure uses the ScreenUpdating property at
the beginning and end of the code.
You may also notice that when the macro has completed running in Excel, you can see
that the last range that was copied is still highlighted with a flashing dashed-outline. This
leads us to another useful property of the Application object: the CutCopyMode
property. When the CutCopyMode is set to True, which is the default, Excel keeps the
last range which was copied highlighted until another action takes place. However, if the
last action in our code is to copy and paste a range, we do not want the user to see this
flashing range. To avoid this, we can simply set the CutCopyMode property to False at
the end of our code (before setting the ScreenUpdating property back to True).
In Figure 13.44, we have added the following line of code to the Applications() sub
procedure:
Application.CutCopyMode = False
35
Chapter 13: More on Objects
We have also added a line that selects the range A1 so that the last range where we
pasted values is not highlighted. You can see the difference in the resulting macro in
Figure 13.45.
Figure 13.45 Adding two lines of code to the Applications() sub procedure, including
the CutCopyMode property.
Figure 13.46 The result of the modified macro does not show any highlighted ranges.
We would now like to discuss the Application object method, Wait . The Wait method
takes a time value as its argument; that is, it will pause the program until the specified
time value has been reached. Recalling from Chapter 4, we know the Now function
calculates the current time. In VBA, Now is a constant for the current time value. We can
also use the function TimeValue in VBA to assign an integer-valued time amount.
Let’s consider another example. With the data in the Applications() sub procedure, we
can change the color of the possible meal plans to yellow, and the actual daily meal
plans to blue by using the Interior and Color properties of the Range object. However,
between changing the first range to yellow and the second to blue, we want to ask the
36
Chapter 13: More on Objects
program to wait three seconds. To do this, between these two lines of code, we type the
following:
Now the program will wait until the time value reaches the current time plus three
seconds of time. See the modified sub procedure in Figure 13.46. Go ahead and
experiment with running the macro in Excel to note the delay in coloring the ranges. The
result of the macro can be seen in Figure 13.47.
Figure 13.47 The modified Applications() sub procedure uses the Wait method
between two formatting commands for ranges in the spreadsheet.
Figure 13.48 The result of the modified macro. There was a three-second delay
between the yellow and blue color changes.
37
Chapter 13: More on Objects
The Wait method will become useful when creating simulation programs, as we will see
in Chapter 20. There are some other Application properties and methods you can
experiment with, but these three are the most commonly used.
For example, if we want to modify several formatting aspects of a certain range, weneed
several different lines of code:
Range(“A1:C8”).Interior.Color = vbRed
Range(“A1:C8”).Font.Bold = True
Range(“A1:C8”).Font.Name = “Arial”
Range(“A1:C8”).Borders(xlEdgeBottom).LineStyle = xlDash
Using the With statement, we can reduce the coding by naming the range object first
and then simply listing all the properties:
With Range(“A1:C8”)
.Interior.Color = vbRed
.Font.Bold = True
.Font.Name = “Arial”
.Borders(xlEdgeBottom).LineStyle = xlDash
End With
See Figures 13.48 and 13.49 for the VBA code in the Borders2() sub procedure and the
result of running the macro in Excel.
Notice that there is still a period before each property within the construct. Also notice
that the code ends with the statement End With. As with most programming structures,
which we will see in Chapter 16, there will be a beginning and ending statement specific
to each structure. When using the With construct, you must always start with With
followed by the object name and always end with End With.
38
Chapter 13: More on Objects
Figure 13.49 The Borders2() sub procedure uses the With construct to format a
specified range.
Let’s look at one more illustration of the With construct by referring to a previous
example. When discussing the charts, we wrote a sub procedure, Graph(), which used
many properties and methods of the ActiveChart object, as shone in Figure 13.50. We
can simplify this code by using the With construct with the ActiveChart object. In Figure
13.51, we can see a modified code in the Graph2() sub procedure that performs the
same actions as the original Graph() sub procedure, but this time, the With construct is
used.
Figure 13.51 The original Graph() sub procedure uses many properties and methods of
the ActiveChart object.
39
Chapter 13: More on Objects
Figure 13.52 The new Graph2() sub procedure uses the With construct with the
ActiveChart object.
There are several ways to reference ranges and cells using VBA. The first one we will
describe uses the Offset property, which considers the named range to be in the 0th row
and 0th column. It then offsets the range selection by a certain row count below and
column count to the right of this named range. That is, if we type the following statement
Range(“A1”).Offset(1, 0)
Range(“A1”).Offset(0, 1)
40
Chapter 13: More on Objects
Let’s consider an example. Suppose we have a table of data collected from an airline
survey (see Figure 13.52). We will use cell A3 as our range name since it is at the corner
of the table. If we want to highlight the 10th passenger, we can use the Offset property in
the following way:
Range(“A3”).Offset(0, 10).Interior.ColorIndex = 15
This command looks at cell A3 and moves 0 rows down and 10 columns to the right (see
Figure 13.53). The result of this code is shown in Figure 13.54.
Figure 13.54 Using the Offset property to count 0 rows and 10 columns from the
named range.
Figure 13.55 The result of the Referencing() macro. The 10th passenger is highlighted.
41
Chapter 13: More on Objects
We can also highlight the entire column of data for the 10th passenger using the Offset
property. To do this, we refer to a larger range by typing the following:
This command finds the first and last cell of the range by using the Offset property (see
Figure 13.55). The result of this macro is shown in Figure 13.56.
Figure 13.56 The large range uses the Offset property to highlight all of the data for the
10th passenger.
The Offset property can also be used to refer to cells above and to the left of the named
range. To do this, simply use negative values as the row and column counts,
respectively.
Another way to reference cells using VBA is with the Cells property. Unlike the Offset
property, which considers the named range to be in the (0, 0) position, the Cells property
considers the named range to be in the (1, 1) position. That is, using the Offset property
to reference cell A2 from cell A1, we would type:
Range(“A1”).Offset(1, 0)
However, to use the Cells property, we would reference in the following way:
Range(“A1”).Cells(2, 1)
Therefore, returning to our previous example with the airline survey data, if this time we
want to highlight the 4th passenger using the Cells property from cell A3, we would type:
42
Chapter 13: More on Objects
Range(“A3”).Cells(1, 5).Interior.ColorIndex = 15
Notice that it may be confusing to shift our counting scheme; that is since the named
range is included as the first row and first column, we have to reference 5 columns even
though we are looking for the fourth entry. It is therefore usually better when using the
Cells property to make a named range the first cell in the table rather than the first cell
on the corner of the table. That is, if we want to use the Cells property to highlight the 7th
passenger, we can use cell B3 as our named range and thus count the 7th column to the
right. We would now type:
Range(“B3”).Cells(1, 7).Interior.ColorIndex = 15
See Figure 13.57 for the modified macro and Figure 13.58 for the result.
Figure 13.58 Using the Cells property to highlight the 4th and 7th passengers. Notice the
named range changes.
The next two properties, Columns and Rows, reference columns and rows in the
named range, respectively. When we wanted to highlight all of the 10th passenger’s
information in a previous example using Offset, we referenced this column of data using
a long range expression. We can shorten this code by using the Columns property.
The Columns property takes a numerical index value to find the numbered column within
the named range. Suppose that this time we want to highlight all of the third passenger’s
information. We would type:
43
Chapter 13: More on Objects
Range(“B3:K9”).Columns(3).Interior.ColorIndex = 5
Figure 13.60 Using the Columns property to find the third column of data.
This command looks at the third column of the range of data; it considers Columns(1) to
be the first column of data (in this case B3:B9). This additional code is added to the
Referencing() sub procedure shown in Figure 13.59. The result of the macro is
presented in Figure 13.60.
Figure 13.61 The result of the Columns property in the modified macro.
The Rows property works in a similar way; a numerical index value of the desired row
should be given. With respect to the named range, the Rows property considers the first
row of data to be Rows(1). For example, if we want to make all of the Age data in our
table have red font, we can use the Rows property in the following way:
Range(“B3:K9”).Rows(2).Font.Color = vbRed
44
Chapter 13: More on Objects
Figure 13.62 Using the Rows property to change the font of the second row of data.
See Figure 13.61 for the modified code and Figure 13.62 for the result of the macro.
The Hidden property is another useful property of Rows and Columns. It takes the
values True and False and is equivalent to right-clicking on a row(s) or column(s)
heading and choosing Hide or Unhide from the list of drop-down options. The format is
as follows:
This property can be useful in keeping necessary values or formulas in cells hidden from
the user. It can also be useful in designing dynamic programs for which you may need to
create several tables but do not know how many until the user informs you. In this case,
instead of generating several tables in the code, you can generate the largest amount of
tables you would need and keep them hidden. Then, you can simply unhide the rows or
columns for the number of tables needed by the user.
There is also a specific formatting method that can be used with the Rows or Columns
reference; this is the AutoFit method. This method is equivalent to selecting Format >
Row > AutoFit or Format > Column > AutoFit from the Excel menu. It causes the row
45
Chapter 13: More on Objects
height or column width to increase or decrease in order to fit the size of the value in the
range. This method has no arguments, and its format is:
Range(cells or name).Rows.AutoFit or
Range(cells or name).Columns.AutoFit
As in the spreadsheet, this type of formatting can also be applied to an entire column or
row. That is, instead of selecting a particular range, you can select the column or row
heading and format it to AutoFit.
The next two properties, EntireColumn and EntireRow, are used to modify every
column or row in the named range, including the column or row settings for the entire
worksheet. For example, if we use the EntireColumn property on the table range B3:K9,
it will affect every column in this range (columns B, C, D, E, F, G, H, I, J, and K) for its
entire length on the worksheet. If we want to shift the alignment of our numerical values
from the center to the left of each cell, we type the following:
Range(“B3:K9”).EntireColumn.HorizontalAlignment = xlLeft
Now, the text in every column in this range for its entire length will be left-aligned. That
is, even if we type a new value in row 10 or below, or rows 1 and 2, in the columns B
through K, it will automatically be left-aligned.
The EntireRow property works in the same way. Suppose we want a thicker border
between the Age and Gender rows of the table. Also suppose that we plan to expand
this table by adding more passengers. We would therefore like to use the EntireRow
property so that the new entries will still have a thicker border between the Age and
Gender values. So, we use B4:K5 as the named range and format an Inside-Horizontal
border.
Range(“B4:K5”).EntireRow.Borders(xlInsideHorizontal).Weight = 3
Now, there is a thick border between rows 4 and 5 for the entire worksheet. See Figure
13.63 for these additional lines of code and Figure 13.63 for the result of the modified
macro.
46
Chapter 13: More on Objects
Figure 13.64 Using the EntireColumn and EntireRow properties to format the table
and worksheet.
Figure 13.65 The result of the modified macro shows the left-alignment and thick
border.
The last property for referencing that we will describe is the End property. It is very
useful as it can help you find the end of a row or column of any range of data. The End
property can take four values: xlDown and xlUp for columns, and xlToRight and xlToLeft
for rows. You do not need to name an entire data range to use this property; just one cell
in the data range works.
For example, using the above data, the code Range(“A3”).End(xlToRight) would
reference cell K3 since that is the last cell in the row with data in it. This property can be
useful in referencing a large row or column of data for which you do not know the last
cell of data.
Suppose we want to copy and paste the titles of the table to create another table below
it. We would type the following to reference the range of table titles:
Range(“A3”, Range(“A3”).End(xlDown)).Copy
47
Chapter 13: More on Objects
This code will copy the range A3:A9 since A9 contains the last cell of data looking down
from A3. See Figure 13.65 for the final Referencing() sub procedure and Figure 13.66 for
the final macro result.
Figure 13.66 Using the End property to copy and paste the table titles.
Remember that you can also use the End property to search up and left from a cell in
the middle or end of a data range.
Note: The End property only works for one data type at a time. That is, if you have a list
of data with both numbers and text, the End property will search until it finds the last
entry of the first data type. For example, in a table with the values “1, 2, three, 4” in four
consecutive cells, the End property would select the cell containing the value “2” not “4.”
The End property also only works if there is at least one value in the cell adjacent to the
specified range from which you are searching. That is, if you have one value “Input” in
the range from which you are searching, and there are currently no values in the “Input”
column or row, the End property will not select a cell. We discuss some tricks for
avoiding these problems in Chapter 16.
These properties are the most useful ways to reference your data. We suggest
practicing with them as they will be helpful in managing and manipulating large data.
48
Chapter 13: More on Objects
Naming ranges
As we have already discussed, the most common way to assign object names is by
using the Name property. The names that you will assign most often will probably be for
the Range object. When you name a range in Excel using Insert > Name > Define, the
name appears in the name window whenever the corresponding range is selected. The
same occurs after naming a range in VBA.
Let’s return to the above example. Suppose we want to name a range of passenger
numbers “Passengers.” We will use the Name property. We will also need to use the
End property (to the right, or to the end of the first row), if we suppose that we do not
know how many passengers are in the table.
If we now highlight the entire range of data in the row beginning with A3, we see the
range name appear in the name window in the upper left hand corner of the window (see
Figures 12.67 and 12.68).
49
Chapter 13: More on Objects
Figure 13.68 The Naming() sub procedure uses the Name property to give a name to
the range.
Figure 13.69 The “Passengers” range is highlighted and the name appears in the name
window.
Now that we have named the range, we can refer to it by its name for future use. If we
want to add a thick border around this range, wesimply type:
Range(“Passengers”).BorderAround Weight:=xlThick
See Figures 12.69 and 12.70 for the code and the result of the modified macro.
Figure 13.70 The range name can now be used to directly reference the range.
50
Chapter 13: More on Objects
There are two main ways to use Excel-defined formulas in VBA: using the Range object
and using the Application object.
There are two main properties to use with the Range object that allow us to use the
Excel spreadsheet functions in our VBA code. The first is the Formula property. It allows
you to enter any Excel formula as you would in a spreadsheet by entering the formula,
including the equals sign, as the property value. Reference the cells using column letters
and row numbers.
For example, using the Airline Survey data, if we want to find the total number of
international trips taken by all passengers, we can put it in the cell L9 by typing:
Range(“L9”).Formula = “=SUM(A9:K9)”
51
Chapter 13: More on Objects
We can also apply a formula to a range of cells. Suppose that we want to not only
calculate the total number of international flights, but also the total number of flights per
year, times in ATL and times in JFK for each passenger. Since each sum references its
respective row of values of equal length, we can enter the entire array of formulas using
one line of code:
Range(“L6:L9”).Formula = “=SUM(A6:K6)”
Remember from Chapter 3 that since relative referencing is used, the formula will
automatically modify for each relative row of data. See Figures 12.71 and 12.72 for the
code and result of this macro.
Figure 13.72 Using the Formula property to assign a formula to a range of cells.
The other property that can be used to enter any Excel formula in VBA code is the
FormulaR1C1 property. It enters formulas using the R1C1 Notation that we discussed in
Chapter 3. If we were to calculate the same sum of international flights that we did using
the Formula property, we would type:
Range(“L9”).FormulaR1C1 = “=SUM(RC[-10]:RC[-1])”
This calculates the sum of values beginning in L9 referenced to the same row and 10
columns previous to L9 referenced to the same row and 1 column previous, which is
B9:K9.
Figure 13.73 The formulas assigned the array use relative referencing.
52
Chapter 13: More on Objects
Let’s use this property to calculate the average age of passengers in the table. We can
place the result of this calculation in cell L4 by typing:
Range(“L4”).FormulaR1C1 = “=AVERAGE(RC[-10]:RC[-1])”
If we were to place the result of this same calculation in cell A11 instead, the code would
change to:
Range(“A11”).FormulaR1C1 = “=AVERAGE(R[-7]C[1]:R[-7]C[10])”
See Figure 13.73 for these two modifications of the Formulas() macro and verify that
both codes return the same value when the macro is run in Figure 13.74.
Figure 13.74 The FormulaR1C1 property enters an Excel formula using R1C1 Notation.
Figure 13.75 Both cells A11 and L4 calculate the same sum using different R1C1
Notation.
Note that the FormulaR1C1 property can also be applied to a range of cells as we did
with the Formula property.
Both of these properties are very useful for applying all Excel functions to a program.
Whether you referencing with column letters and row numbers or R1C1 Notation will
depend on your particular usage.
53
Chapter 13: More on Objects
We would like to quickly mention one other method that can be useful when working with
formulas in VBA: the AutoFill method. As we learned in Chapters 3 and 4, if you enter a
formula or value in a cell and want to copy it to an adjacent row or column, you can just
place your cursor on the edge of the range until you see a small cross, then copy-and-
drag to paste the values in the highlighted range. This operation is performed in VBA
using the AutoFill method.
The AutoFill method takes two arguments: Destination and Type. The named range for
the AutoFill method will contain the formula or value that you want to copy; the
Destination range gives the location of the range you want to paste these values to, and
the Type argument allows you to specify exactly what you want to copy and paste (as in
the PasteSpecial method).
For example, say we want to calculate the average number of flights and airport visits in
the Airline Survey table in the column next to the calculated sums. We could first
calculate the average number of flights per year in cell M6 and then use the AutoFill
method to copy and paste this formula into the three cells below. We would type the
following two lines of code:
Range(“M6”).FormulaR1C1 = “=AVERAGE(RC[-11]:RC[-2])”
Range(“M6”).AutoFill Destination:=Range(“M6:M9”), Type:=xlFillDefault
See Figures 12.75 and 12.76 for the code and result of the modified macro.
Figure 13.76 The AutoFill method copies a formula or value in the named range to a
specified destination range.
54
Chapter 13: More on Objects
The Application object has several Excel-defined functions as sub properties of the
WorksheetFunction property. There are many functions to choose from (see Figure
13.77), including most of the functions available from the Excel function list. For now, we
will just demonstrate three of the main ones: Max, Min, and Average.
55
Chapter 13: More on Objects
Figure 13.78 The WorksheetFunction property of the Application object has several
Excel functions as sub properties.
Let’s continue modifying the Formulas() sub procedure to perform calculations on the
Airline Survey data. We can first calculate the maximum number of flights per year taken
by any passenger using the Max sub property of the WorksheetFunction property. We
place the value of this calculation below the table in cell A12 by typing the following:
Range(“A12”).Value = Application.WorksheetFunction.Max(Range(“B6:K6”))
The sub properties, such as Max, require the range of values for which they are
performing the calculation; in this case, we have entered Range(“B6:K6”), which is the
row with the number of flights per year. We can similarly calculate the minimum number
of times to ATL and place this result in the next cell, A13:
Range(“A13”).Value = Application.WorksheetFunction.Min(Range(“B7:K7”))
See Figure 13.78 for the addition of these two lines of code as well as an additional line
that calculates the average number of times to JFK. Figure 13.79 displays the result of
the modified macro.
56
Chapter 13: More on Objects
Figure 13.79 Adding three calculations to the Formulas() sub procedure using the
WorksheetFunction property.
Figure 13.80 The result of the last three calculations in cells A12, A13, and A14.
One last useful WorksheetFunction sub property is Round. The Round sub property
takes an initial value and the number of decimal places to round to as input. The format
is as follows:
57
Chapter 13: More on Objects
We again recommend you experiment with some of the other sub properties of the
WorksheetFunction property of the Application object. This particular method of using
formulas may be useful when you do not want to record the results as a formula in a cell,
for example, when setting variable values.
13.6 Summary
¾ The workbook and worksheet objects use the Activate method to select a
workbook or worksheet on which to perform associated code. The worksheet
object also uses the Visible property to hide or unhide a worksheet.
¾ Almost all Excel objects have the Name property, which can be used to assign a
text value to the name of any of these objects.
¾ The Range object uses the following properties: Interior (with sub properties
ColorIndex, Color, and Pattern); Borders (with sub properties Weight, LineStyle,
Color, and XL Border Constants); Font (with sub properties Bold, Size, and
Color); and Value. The Range object also uses the following methods:
BorderAround, Clear, ClearContents, ClearFormats, Sort, AutoFilter, Copy, and
PasteSpecial.
¾ The Chart object uses the methods Add, Copy, Delete, and Select. The
ActiveChart object uses the methods SetSourceData, Location,
ApplyCustomType, ChartType, HasLegend, HasTitle, ChartTItle, and
SeriesCollection. Remember that it is necessary to set the chart type, source
data, and location for any new chart before modifying any options.
¾ Drawing objects, or shapes, are associated with four different objects:
ActiveSheet, Shape, Selection, and ShapeRange. These objects use the following
properties: Shapes, Name, Fill, Line, EndArrowheadStyle, EndArrowheadLength,
EndArrowheadWidth, BeginArrowheadStyle, BeginArrowheadLength, and
BeginArrowheadWidth. They also use the Select and Add methods.
¾ The Application object uses the ScreenUpdating and CutCopyMode properties. It
also uses the Wait method with the Now and TimeValue arguments.
¾ The With construct can help reduce code when modifying several properties of
one object.
¾ There are several properties of the Range object to reference ranges and cells.
These are Offset, Cells, Rows, Columns, EntireRow, EntireColumn and End.
¾ Formulas can be used in VBA with the Range object and the Application object.
The Range object uses the Formula, FormulaR1C1 properties. You can also use
the AutoFill property to copy formulas with the Range object. The Application
object uses the WorksheetFunction property with several sub properties, including
Min, Max, and Average.
¾ Make sure to review the Quick Programming Notes provided in the chapter to
improve the efficiency of your code.
13.7 Exercises
58
Chapter 13: More on Objects
1. Open a new workbook and save it as “Exercise.xls.” Then, write a sub that
performs the following:
a. Rename “Sheet1” to “Exercise Sheet.”
b. Create a rectangle on the sheet large enough to display a sentence.
c. Give the rectangle a background color.
d. Center the text vertically and horizontally.
e. Modify the rectangle to NOT move or size with cells.
59
Chapter 13: More on Objects
e. Create the following subs that perform the appropriate actions and assign
each one to its own button: “ShrinkOval,” “MoveDown,” “MoveLeft,”
“MoveRight,” and “MoveUp.”
3. Write a sub called “FormatRanges” that formats three ranges as shown below.
5. With the table from the previous exercise, use VBA to fill in the grades for the
quizzes and exams and calculate their final grades.
a. Create random grades for the quizzes and exams according to
Normal(75, 7) using the AutoFill method. Grades should be integer
values.
b. Insert an “Average” column and use the Fill method to enter a formula
into the column that calculates each student’s average.
c. Insert a “Letter Grade” column and use the Fill method to enter a formula
that will calculate each student’s letter grade (A = 90:100, B = 80:90, C =
70:80, D = 60:70, F = 0:60).
d. Sort the table according to the “Average” column in ascending order.
e. Filter the table such that only the students with a passing grade (>= C)
are shown. Prior to executing your code, if all of the students have
60
Chapter 13: More on Objects
6. The following table contains information about a company’s electrical use and
costs. Enter this table into a worksheet and create a column chart to plot the
Demand vs. Month.
a. Create a sub called “ViewCosts” that modifies the chart to display Costs
vs. Month with the appropriate axis labels and formats. Create a button
labeled “View Costs Chart” and assign the sub to it.
b. Repeat part (a) with an appropriately named sub and button for the
Energy Consumed.
c. Repeat part (a) with an appropriately named sub and button for the
Demand.
d. Create a sub called “ChangeToLineChart” that will change the chart type
to Line Chart. Create a button labeled “Change to Line Chart” and assign
the sub to it.
e. Repeat part (d) for changing the chart type to Column Chart.
61
Chapter 13: More on Objects
a. Name the three cells under the distribution headings “CurrentRow”. Write
a sub called “RandomValues” that performs the following:
i. Enters an appropriate formula into each cell of the range
“CurrentRow” that calculates a random number from the
corresponding distribution.
ii. Names the three cells under the distribution headings in the next
row down “CurrentRow”.
b. Create a button on the worksheet called “Random Values” and assign it
the sub “RandomValues”. Click the button several times. Realize that
after each press of the button, an additional row of random variables is
calculated. Create a table with 20 rows of random variables.
c. Write a sub called “RandomValuesGraph” that creates a Line Chart with
markers that plot the 20 values under the distribution headings. Label
each series in the legend according to the distribution.
62
Chapter 13: More on Objects
b. Copies and pastes these values on top of itself so that the values do not
change. (Make sure to paste the values only and keep the copied cells
from flashing.)
c. Using the Application object, output the Max and Min of these values to
the spreadsheet.
11. A marketing manager is trying to determine which marketing method has the
greatest influence on product sales. Using the data below, create different charts
for each marketing method (ex: TV vs. Sales, Radio vs. Sales, etc.). Make all of
these charts separate sheets (full size charts). Create a button for each chart on
the data sheet. When you press a button, the data sheet should be hidden and
the corresponding chart sheet should be shown.
12. Race Animation “The Turtle and The Hare”: Create a simple animation to
represent the race between the turtle and the hare. Prepare your spreadsheet by
creating some shapes and buttons as shown in the figure below. Name the
shapes appropriately. Also name a starting cell for each (ex: B4 = “TStart” and
B7 = “HStart”).
63
Chapter 13: More on Objects
13. Quality Control: Create an X-bar chart by manipulating and graphing data
provided by a user. Using a Scatter graph, graph the data values for each
sample given. Then, calculate the mean and standard deviation of this data. Add
a line to the chart for the mean value, 3-sigma (=3*standard deviation) above the
mean, and 3-sigma below the mean (refer to the figure). After a user has entered
20 data values, your procedure should calculate the mean and standard
deviation and then update the source data of the chart.
14. Create a simple shopping and billing program. A user should fill in his or her
name and address and, using data validation, select his or her credit card type
from a list. Update the value in the “Date” cell (using an Excel function). Then the
shopper should select from a list of products, again using data validation, and
enter the quantity he or she wants. After the shopper selects a product, he or she
should click the “Add Item” button. This button should find the “Individual Price”
for the product using a lookup function. When the user is done selecting
products, he or she should click the “Calc Total” button. This button should find
the total price for each product and the total price overall. You should then add
some formatting to the final product list and tell the user how much will be
charged to his or her credit card (enter this statement into a cell below the
product list). See the figure below. (Refer to Chapter 10 for using data validation.)
64
Chapter 13: More on Objects
15. Map Search: Help a salesperson in a rural area find the best route from any
origin to visit all the cities based on either shortest distance or shortest time. Use
the tables below for the time and distance between cities. Create two separate
procedures that take the value in the origin cell and find it in the table (by
matching a value either in the first column or first row of the corresponding table).
Then find the minimum distance or time in that city’s row or column. Use lookup
functions to find the city name with the minimum distance or time and then repeat
the previous action. Do this until all of the cities have been traversed. Bonus: Add
animation by coloring the cities in a map as they are selected to be traversed
(see the figure below).
16. The following table presents the flight information of an airline company. Use
VBA to perform the following:
a. Create a sub called FlightsToAtlanta that highlights the flights that have
Atlanta as the destination city.
b. Create a sub called FlightsFromMiami that highlights the flights that have
Miami as the start city.
c. Create a sub that highlights the flights that run daily.
d. Create a sub that highlights the flights that last at least 5 hours.
65
Chapter 13: More on Objects
19. Create a subroutine that fills the squares of a 6 by 6 table as shown in the table
below. In your code, use the Wait method and Offset property.
20. The following table is part of the database of a small town hotel. Use VBA to find
the following:
a. The InDate and OutDate of customer Regina Murphy using the VLOOKUP
function.
b. Name the table as Reservations.
c. Highlight the third row of the table using the Rows property.
d. Highlight the information about the fines paid using the Columns property.
e. Hide the TotalAmt paid column.
f. Centrally align the BookingId column. Centrally align the RoomType column
using the EntireColumn property.
g. Find the name of the customer that made the last reservation using the End
property.
67
Chapter 13: More on Objects
Create buttons on this worksheet and assign the subs created to the buttons.
17. The following table presents information about flight reservations of an airline
company. Use VBA to perform the following:
a. Create a sub that presents the number of customers that have a
reservation for flight with FlightNr 1 to flight with FlightNr 6.
b. Create a sub that calculates the average amount of payments made.
c. Create a sub that finds the maximum and minimum payment amount.
d. Create a sub that calculates the total amount made from Economy class
flights, Club and Business class.
18. The following table presents information about the faculty in a University. Use
VBA to perform the following:
a. Sort the information by hire date.
b. Calculate the average, min and max salary.
c. Filter the data so that only the information about the Professors is
displayed.
d. Filter the data so that only the information about the Operations Research
Professors is presented.
66
Chapter 14
Variables
Chapter Overview
14.1 Introduction
14.5 Applications
14.5.1 Creating and Modifying Shapes
14.5.2 Making Calculations and Creating a Report
14.6 Summary
14.7 Exercises
Chapter 14: Variables
14.1 Introduction
This chapter and the next three chapters illustrate the main topics of programming in
VBA: variables, sub procedures and function procedures, programming structures, and
arrays. These chapters will be especially important to the reader with little or no
programming experience.
This chapter discusses the use of variables in the VBA programming language. We
show the reader how to create variables of various data types, assign values to them
from the programmer and from the user, and how to use them in functions and formulas.
At this point, we are advancing the level of which we will use VBA from simple Excel
object manipulation to other programming functionality. By understanding variables, the
reader will be able to use more advanced programming structures and even implement
algorithms when developing a DSS.
Dim i As Integer
(There are other possible declarations that we will mention in a later section in this
chapter.) Each variable declared in VBA must have a specified data type. If one is not
specified, it will be assumed as a Variant data type. A Variant variable assumes the data
type of the last value assigned to the variable. (We do not recommend using Variant
data types; it is better programming practice to have a specific variable name and
corresponding memory allocated to the data type for which a variable will be used.)
Below are examples of correct and incorrect formats for defining two integer variables (in
the incorrect example, i is defined as a Variant):
It is important to remember that VBA for Excel is case-sensitive; that is, upper-case
letters and lower-case letters are treated uniquely. For example, the variable size is
different from Size. Names given to variables should not conflict with pre-set names
used by VBA in common functions or programming structures. You should also avoid
repeating names you may have already given to sub procedures or function procedures.
We recommend for variable names of more than one word, you capitalize each new
word, for example: SizeOne or SizeTwo. It is important to keep a naming convention for
all variables in your program. That is, if you want to keep track of variable data types,
you may use the convention intSize to show that this variable is an integer data type and
similarly prefix other variable names to reflect their data type. You may want to name
your variables as they pertain to a particular category of variables such as CarsProfit,
CarsCost, TrucksProfit, TrucksCost, etc. You should pick a naming convention and be
2
Chapter 14: Variables
consistent in using this convention throughout your code. We discuss variable naming as
a programming principle in Chapter 24.
A data type categorizes the values that can be assigned to a variable. There are several
different data types available in VBA; for now, we will focus on the following seven most
common data types: integer, double, string, Boolean, range, worksheets, object.
Message Boxes and Input Boxes allow you to communicate with the user using VBA
code. A Message Box prints information to a small dialog box, and an Input Box
prompts the user to enter information into a small dialog box.
A Message Box can print a string of text or a variable value. The VBA code to generate
a Message Box is MsgBox. To print a string of text, we could, for example, type the
following (the resulting Message Box is also provided):
MsgBox size
We can print both text and variables in one Message Box by concatenating them with an
& sign. For example, given that h and w are variable names:
MsgBox “The height is “ & h & “ and the width is “ & w & “.”
We can use the statement vbCrLf to enter a new line of text in the Message Box by
concatenating it with the string or variable values. For example, we can retype the above
statement on two separate lines as follows (see the resulting Message Box below):
MsgBox "The height is " & H & vbCrLf & " and the width is " & W & "."
3
Chapter 14: Variables
MsgBox can also be used as a function in VBA. If used as a function, then more settings
for the dialog box can be defined as arguments for the function. The function format is:
The prompt is either the string or variable (or concatenation of both) that is displayed in
the dialog box; this function is required, all other arguments are optional. The buttons
argument takes a VB Constant value to determine the number and style of buttons
available to the user. The table below lists some of the more common button types and
the image below is an example of the vbYesNo button type.
The title argument allows you to enter text that you want to appear as the title of the
dialog box. We will usually ignore the helpfile and context arguments, which allow you
to give help options to the user.
You can also capture the user’s response to your Message Box. For example, you may
want to take different actions in your code if the user presses YES or NO (we will give an
example of this when we discuss programming structures in Chapter 16). To do this, you
can declare a variable called response as a Variant data type. Then, set this variable
equal to the MsgBox function, and you can check the value of this response using the
VB Constants shown below:
4
Chapter 14: Variables
To determine the user’s response, we will use some programming structures that we will
discuss in Chapter 16.
Input Boxes allow the user to enter a value for any variable type. They have a prompt,
an OK and Cancel button, and an empty text box as shown in the image below:
An Input Box is created using the InputBox VBA function. Since Input Boxes receive
some input from the user, the InputBox function is always assigned to a variable. This
function takes the following general form:
The prompt and title arguments of the InputBox function are the same as those of the
MsgBox function; likewise, prompt is the only required argument. Notice that unlike the
MsgBox function, there is no buttons argument for the InputBox function; the only button
on an Input Box is OK. The default argument allows you to enter a default value to
display in the Input Box; this default value is entered if no change is made in the dialog
box. The xpos and ypos arguments allow you to position the Input Box relative to the
left and top edges of the screen. The helpfile and context arguments are the same as
those of the MsgBox function.
The InputBox function is always assigned to a variable (as with the response variable for
the MsgBox function). You may want the user to enter a parameter before drawing an
object (size, height, etc) or a value to be used in a calculation. As we will see shortly,
there are several different variable data types that can be defined in VBA; therefore, the
InputBox function can be used to capture a value for any of these data types in their
possible respective situations.
Note that Input Boxes always have an OK and Cancel button. To determine if the user
has pressed Cancel rather than OK, you can simply check the value of variable you
assigned to the InputBox function. If the variable value is “”, then the user has pressed
Cancel (or never entered a value). It is useful to have some error checking statements
after receiving data from an Input Box; we will discuss this in more detail in Chapter 16.
5
Chapter 14: Variables
Integers and doubles are both numerical values (there are many other numerical data
types such as long and single which we will not review in detail here; more information
can be found using the Microsoft Help tool). Integers are non-decimal numbers ranging
in value from -32,768 to 32,767. Doubles have values from -1.79769E308 to -4.94065E-
324 for negative values and from 4.94065E-324 to 1.79769E308 for positive values.
We will use integers very often for simple numerical values, for counting in loops, and for
enumerating arrays. Integers are also important data types when solving integer
programming problems with VBA (see Chapter 19). To declare a variable to be an
integer, we use the following format (in this example, the variable name is number):
The double data type is necessary when working with data that is non-integer and/or
very large. Double data types may be used as values in tables or arrays. To declare a
variable as a double we use the following format (in this example the variable name is
data):
In future examples, we will often use both integer and double variables in our code.
14.2.3 String
A string is a segment of text. This text can include upper- and lower-case letters,
punctuation marks, and numbers; however, these numbers cannot be used in
calculations, as they will be viewed as text.
To declare a variable as a string, we use the following format (in this example the
variable name is label):
We will use strings to name objects, label objects, and label data in spreadsheets.
14.2.4 Boolean
6
Chapter 14: Variables
We will use Boolean variables often in logic statements, If, Then statements, and loops
(see Chapter 16). We will discuss Boolean variables in more detail later.
14.2.5 Range
A range is a variable that can be used with all of the properties and methods of the
Range object. To declare a variable as a range variable, we use the following format (in
this example the variable name is MyRange):
We must also use the Set declaration to initialize the value of a range variable. For
example, to set MyRange equal to cell A1, we would type the following:
We will use ranges often to increase the efficiency of our code. For example, we may
frequently reference a particular cell while creating a table; therefore, the range name for
this cell may appear often in our code. However, if we ever want to shift the position of
the table we are creating, we would have to modify our entire code at every point where
the referenced cell appears. To avoid this tedious task, we could instead define a
StartCell as a range variable. We can set the value of this range variable at the
beginning of our code and reference this StartCell variable name throughout the
remainder of the code. Therefore, if we want to shift the table we are creating, the only
change we have to make to the code is in the initialization of the StartCell range
variable.
We may also use ranges to make our code more dynamic. For example, if we are
creating a table and want to place it where our user wants to place it, we may take from
an Input Box the name of a cell and then set our range variable equal to this range
value. That is, users may want to start their table in cell C4. If they entered this cell name
as text in an Input Box, we might use the following code to initialize our StartCell.
We will use ranges often and see more examples of how to best utilize a range variable
soon.
14.2.6 Worksheets
The worksheets data type defines a Worksheets object. This variable can be assigned
any of the properties or methods used with the Worksheets object. To declare a variable
as a worksheet, we type the following format (in this example ws is the variable name):
Dim ws As Worksheets
7
Chapter 14: Variables
We may also want to use this variable when creating a function procedure that performs
a method on a worksheet. For example, we may want to create a function that closes the
active worksheet. In this case, we would want to use a worksheet variable in the
procedure. We will see a detailed example of this technique in Chapter 15.
14.2.7 Object
The object variable can be used to define any object in Excel. This can be considered a
drawing object, a range, a worksheet, or any other object. Any of the properties or
methods associated with any object in Excel can be used with the object variable. To
declare an object variable, we type the following format (here MyObject is the name of
the variable):
Even though this is a variable with a wide range of properties and methods, its running
time is slower than when a specific class of objects is defined. Therefore, if we know we
want to create an object variable that will only be used with properties or methods
associated with the Range object, it is better to declare a range variable instead.
8
Chapter 14: Variables
There are two types of variable declarations: Private and Public. A Private variable is
declared using the Dim statement. This variable can be private on a procedure level or
on a module level.
If we declare a variable as private on a procedure level, then it can only be used in the
sub procedure in which we defined it. That is, if we declare the integer variable i in a sub
procedure called Sub1, we cannot use the variable i in another sub procedure called
Sub2 unless we again declare the variable in the second sub procedure. To do so, we
would need to type the following:
Sub Sub1()
Dim i As Integer
.....
End Sub
----------------------------------
Sub Sub2()
Dim i As Integer
....
End Sub
Alternately, if we declare a variable with the Dim statement on a module level, then it can
be used in any sub procedure in that module; however, it cannot be used in other
modules. For example, we could declare the variable i at the top of our module and refer
to it in both Sub1 and Sub2 without having to declare it again.
Dim i As Integer
-------------------------
Sub Sub1()
.......
End Sub
------------------------
Sub Sub2()
........
End Sub
If we consider both Sub1 and Sub2 to be in Module1, we could not use the variable i in
any procedure in another module, Module2, without declaring it again.
A Public variable, on the other hand, can be used in any sub procedure in any module.
To declare a public variable, we use the Public statement. For example, if we declare the
variable i using the Public statement at the top of any module, say Module1, we can use
it in Sub1, Sub2, and in any procedure in Module2 without having to declare it again. To
do this we would type the following at the top of any module:
Public i As Integer
It is common programming practice to use Private variables often and Public variables
only when needed. That is, to avoid errors while running your code, you should try to
9
Chapter 14: Variables
create variables with as small a scope as possible. However, in some cases you may
need to use Public variables in order to keep the variable value as several different
procedures are performed. We will give examples of this scenario when we discuss user
interface procedures in Chapter 18. We also review variable scope as a programming
principle in Chapter 24.
To keep track of your variable values as your code is running, you can use the Watch
window in the VBE. To do this, click on View > Watch Window from the menu. Then, just
highlight any variable from the Code Window and drag it to the Watch Window. As you
run your program (for example, using the Step-Into debugging function, as explained in
Chapter 22), you will be able to observe the values of your variable changing in the
Watch Window. You can also hold your cursor over a variable name in the Code
Window to see a small display of its value.
We will describe seven basic math functions: Abs, Sqr, Mod, Int, Rnd, Exp, and Log.
There are other math functions available in VBA; however, these seven are the most
commonly used.
The Abs function calculates the absolute value of a variable. This function can be used
with both integer and double data types (as well as with other numerical variables). It will
return the same data type that is used in the function. For example:
Abs(-10) = 10
The Sqr function calculates the square root of a number. It can also be used with any
numerical data type (greater than 0); however, it will always return a double data type.
For example:
Sqr(100) = 10
10
Chapter 14: Variables
The Mod function calculates the modular value of a number, given its divisor. The format
of this function is divisor Mod number. Consider the example below.
Sub ModularFunction()
Dim x As Integer, y As Integer
x=2
y=4
MsgBox x Mod y 'answer is 2
x=4
y=2
MsgBox x Mod y 'answer is 0
End Sub
The Int function removes the decimal part of a double variable and returns the integer
part; therefore, the result is always an integer data type. For positive numbers, the Int
function always rounds down; for negative numbers, the Int function returns the first
negative integer less than or equal to the original variable value. For example:
Int(5.6) = 5
Int(-4.3) = -5
The Rnd function generates a random number. You can either enter a seed as a
parameter for the function, or leave the seed value blank. This function returns a double
data type between 0 and 1. To create random integers in a specific range, use the
formula:
The Exp function raises the constant e to a power given in the function statement. The
value returned is always a double data type. For example:
Exp(2) = e2 = 7.389056099
The Log function calculates the natural log (the logarithm with base e, of a given
number). The result is a double data type. You can calculate logarithms with base n for
any number by dividing the natural logarithm of that number by the natural logarithm of
n. For example, to calculate the log of 15 with base 10 (Log1015), type:
This is actually a derived function that we will talk about later in this section. We will offer
examples of these functions toward the end of the chapter.
The Log function can also be used with the Rnd function to generate random values
from the Exponential distribution. As we learned in Chapter 8, there is a set of inverse
functions in Excel that can be used with the RAND function to generate random numbers
from distributions. For example, the Excel functions needed to generate a random
number from the Exponential distribution are:
11
Chapter 14: Variables
=LNINV(RAND(), -mean)
To accomplish this in VBA using the Log and Rnd functions, set a variable equal to the
following:
-mean * Log(Rnd())
We will discuss more functions for working with distributions in VBA in Chapter 20.
We will describe four basic trigonometric functions: Sin, Cos, Tan, and Atn. These
functions all take an angle as a parameter, and this angle value should be entered in
radians. (To convert degrees to radians, multiply degrees by pi/180.)
The Sin, Cos, and Tan functions take an angle and return the ratio of two sides of a
right triangle. The value returned is a double data type. The result of the Sin and Cos
functions are always between -1 and 1. For example:
Sin(pi/4) = 0.7071067812
Cos(pi/3) = 0.5
Tan(pi/6) = 0.5773502692
The Atn function calculates the arctangent of a given ratio. The result is an angle in
radians, which is a double data type between -pi/2 to pi/2 radians. (To convert radians
to degrees, multiply radians by 180/pi.) For example:
Atn(0.5773502692) = pi/6
VBA does not have many more pre-defined trigonometric functions; however, we can
derive several other trigonometric functions using these four functions.
(Note: If you need to convert degrees to radians or vice versa using the value pi, you will
need to define a constant as a variable declaration. A constant is a variable whose
value never changes. To declare a constant, use Const. For example, if you define the
constant variable pi to be 3.14, you should type:
Const pi = 3.14
Using the above functions, we can derive other functions. There is a long list of
examples of derived functions in Microsoft Visual Basic Help; we will demonstrate just a
few here.
We can derive the arcsine or inverse sine function using the Atn and Sqr functions and
name it the Arcsin function. This function is defined as follows:
12
Chapter 14: Variables
We can derive the secant function using the Cos functionand name it Sec. It is defined
as follows:
Sec(X) = 1 / Cos(X)
We will use function procedures to create these and other derived math functions. We
see an example of this technique in Chapter 15.
Input Data
Math Function Type Resulting Data Type Resulting Value
Abs numerical numerical > 0 absolute value
Summary
Sqr numerical > 0 double square root
Mod numerical numerical modular
Int double integer integer value
Rnd numerical double; (0,1) random number
Exp numerical double ex
Log numerical double logex
Trigonometric Input Data
Function Type Resulting Data Type Resulting Value
Sin numerical double sine of an angle
(radians)
Cos numerical double cosine of an
(radians) angle
Tan numerical double tangent of an
(radians) angle
Atn numerical double arctangent of a
ratio
Derived
Functions Uses Equation
Arcsin Atn, Sqr Atn(X / Sqr(-X * X + 1))
Sec Cos 1 / Cos(X)
There are several functions in VBA that can be used to convert one data type to another.
These functions can be useful for many reasons. For example, it may be necessary to
convert input which was initially given as a Variant data type to another specific data
type before performing calculations. Even though a Variant is usually treated as the
initial data type value it is assigned, converting a Variant variable will ensure the data
type used for subsequent actions. This conversion may be more relevant when the
variable is intended to have a numerical value. Two main conversion functions used with
numerical values are CInt and CDbl. CInt converts a variable to an Integer data type
and CDbl converts a variable to a Double data type. The input for these functions can be
any variable or expression.
CInt(variable or expression)
13
Chapter 14: Variables
CDbl(variable or expression)
If you are converting a variable to an Integer or Double data type using these functions,
the variable value must meet the range requirements of the data type to which you are
converting. That is, you cannot convert a number that is larger than 32,767 to an Integer
data type. However, if a variable value is decimal (or fractional), it can still be converted
to an Integer, but will be rounded down in the process. The variable or expression you
are converting can also be a string. If this is the case, these functions will extract the
numerical values from the string. For example, the following string expression would be
converted as shown:
The Val function also extracts a numerical value from a string. The input for this function
is an expression. However, note that using the Val function will not enforce any data type
on the extracted number. Therefore, we recommend using one of the numerical
conversion functions directly:
Val(expression)
Any numeric expression can also be converted to a string using the CStr function. The
input for this function is again any variable or expression:
CStr(variable or expression)
For example, if a numerical expression is used with this function, the result is:
CStr(25.1278) = “25.1278”
This function can also be used to convert Boolean data types to True or False strings,
depending on the Boolean variable’s value.
Conversions can also be useful when working with dates. There are several formats in
which a user can enter a date. For example: January 1, 2004 or Jan 1, 2004 or 1/1/04,
etc. However, to work with dates in your code for comparisons or calculations, you will
need to ensure that the input is converted to a Date data type. This can be done using
the CDate function. The input for this function should be a variable or expression
containing a valid date in some format:
CDate(variable or expression)
Two other useful conversion functions are Asc and Chr. These functions, which use the
ASCII list of numbers and characters, can be used to convert letters to numbers and
numbers to letters, respectively:
Asc(character)
Chr(number)
14
Chapter 14: Variables
This function may be useful, for example, if you have an array of letter values (A through
Z) that you need to convert to numerical values in order to perform comparisons or
calculations. You could use the following functions:
Asc(“A”) = 65
Chr(65) = “A”
For a complete list of conversion functions, search for Type Conversion Functions in the
VBA Help window.
Similar to the text functions in Excel that we discussed in Chapter 4, there are several
string functions available in VBA. Two useful string functions are UCase and LCase,
which can be used to convert a string into all upper case or all lower case, respectively.
The input for these functions is string variables or string expressions:
Another useful string function is Len. This function determines the length of a string
variable value or string expression:
Len can also be used with Variant data types. However, if the data type is numeric, then
the Len function returns the number of bytes required to store the data type (regardless
of the length of the number).
Conversion
Function Input Resulting Data Type
CInt variable or expression integer
Summary
CDbl variable or expression double
Val expression numerical value
CStr variable or expression string
CDate variable or expression date
Asc character number
Chr number character
15
Chapter 14: Variables
14.5 Applications
Let’s consider two applications, or extended examples, in which we implement some of
the new techniques learned in this chapter. First, we will work with Message Boxes,
Input Boxes, and some various data types. Then, we will work with several different
functions.
Let’s create a program in which a user can create either a square or a circle, label its
shape, and position its shape on the spreadsheet. We also want to keep track of how
many squares and circles the user has created and allow his or her to delete a square or
circle as well.
The first sub procedure we want to write creates a square. To do this, we will first
declare variables that can be assigned to an Input Box to find the desired width and
height of the square. We will call these variables width and height. Both of these
variables should be define as double data types, so we write the following code:
Sub CreateSquare()
Dim width As Double, height As Double
16
Chapter 14: Variables
The InputBox parameters have the respective prompts needed: a title for each Input Box
and a default value = 50. Now we will use the AddShape method associated with the
ActiveSheet and Shapes objects to create the square. The shape type parameter should
be msoShapeRectangle. The position parameters can be anything for now, and we will
set the width and height parameters as our variable values. Therefore, we add the
following line of code:
Now, we can type the code that displays a Message Box telling the user that the macro
has run successfully and the square has been created:
The final code appears in Figure 14.2. We can now assign this macro to the “Create
Square” button and test it to make sure a square is drawn (see Figure 14.3).
17
Chapter 14: Variables
Figure 14.3 The Message Box from the macro with the created square.
We can now create a similar sub procedure to create a circle. Here, however, we
declare a radius variable instead of width and height. We can then insert this radius
variable as both the width and height parameters of the AddShape method. We do this
to guarantee that a circle, not an oval, is created. The msoAutoShapeType for a circle is
msoShapeOval. The code for this sub procedure appears in Figure 14.4. We assign this
macro to the “Create Circle” button; the Input Box for this sub procedure is in Figure
14.5.
18
Chapter 14: Variables
Figure 14.5 The Input Box for the radius variable value.
Note that we have declared width and height as private variables on the procedure level
for the CreateSquare() sub procedure, and radius as a private variable in the
CreateCircle() sub procedure. If we had decided to use a width and height value, instead
of radius, to create the circle it would have been more efficient to declare width and
height as either private variables on the module level or as public variables. In doing so,
we could have used width and height in both sub procedures and only declare them
once.
Let’s now consider how we will be able to tell the user how many squares and circles he
or she has created. We will first need two variables: NumSquares and NumCircles,
which both can be integer data types. We first need to initialize these variables to start
with value 0. We cannot perform this initialization in either the CreateSquare() or
CreateCircle() sub procedures since we want to increase these values each time the
macros are called. Therefore, we can either create a new sub procedure called Main() or
Initialize() and assign it to a new “Start” button, or we can use the Workbook_Open()
event procedure. In future case studies, we will usually have a “Welcome” sheet with a
“Start” button assigned to some Main() sub procedure; however, for now let’s use the
Workbook event procedure.
19
Chapter 14: Variables
In the Workbook_Open() event procedure, we can also clear the range of cells where
the shapes will most likely be created and/or positioned. We assume this range is
A12:K50 and specify that it is in our first worksheet using the Delete method. The
complete code for the Workbook_Open() procedure appears in Figure 14.7.
Figure 14.7 This event procedure initializes the variable values and clears previously
created shapes.
Now we can return to the CreateSquare() and CreateCircle() sub procedures and insert
code that will increase the corresponding variable values and display this value (instead
of the original Message Box) to the user. These additional lines of code are:
NumSquares = NumSquares + 1
MsgBox "You now have " & NumSquares & " squares in your worksheet."
NumCircles = NumCircles + 1
MsgBox "You now have " & NumCircles & " circles in your worksheet."
(Note: To increment variable values in VBA, we use the statement: variable = variable +
1. Similarly, to decrement variable values in VBA, we use: variable = variable – 1.)
The new Message Box appears in Figure 14.8 after creating two squares.
20
Chapter 14: Variables
Let’s now create sub procedures to delete a square and a circle. Our code will ask the
user to select the shape he or she wishes to delete before pressing one of the “Delete”
buttons on the spreadsheet. Then, the code for both macros is simply:
Selection.Delete
We also need to decrease the NumSquares or NumCircles variable values for each
corresponding shape that is deleted; this way, the count is correct the next time we
create one of these shapes. The final code for both the DeleteSquare() and
DeleteCircle() procedures appears in Figure 14.9. These macros are assigned to the two
delete buttons.
21
Chapter 14: Variables
Figure 14.9 These procedures delete the selected shape and decrease the
corresponding count variable.
We have two more sub procedures to construct: LabelShape() and PositionShape(). For
both macros, we will ask the user to first select a shape; this way we can use the
Selection object in the same way as the Delete sub procedures.
To label our shape, we first prompt the user to see what text will be used as the label.
We declare a private variable called text and assign it to an Input Box as follows:
text = InputBox("Please enter label for your shape:", "Shape Label", "Square 1")
We assign a title and default value to this Input Box. Now, we can use the Selection
object with the Characters property and Text sub property and assign the value equal to
our text variable.
Selection.Characters.Text = text
The LabelShape() procedure appears in Figure 14.10. We assign this macro to the
“Label Shape” button; the result of running this macro in Excel appears in Figure 14.11.
22
Chapter 14: Variables
Figure 14.11 The created square has been labeled “Square 1.”
Instead of asking the user to provide two numerical values for the left and top arguments
of the AddShape method in order to position a shape, , we will cut and paste the
selected object to a specified range. We will first declare a private variable called
position to capture the range name where the user wants to move the selected object.
We will then use a range variable called place to use this range value in our Paste
method. The variable declarations, InputBox function, and range assignment are coded
as follows:
position = InputBox("Please enter cell name where you would like to position
your shape (below A12):", "Position", "A12")
Set place = Range(position)
We now use the Cut method on the Selection object and then use the ActiveSheet
object and Paste method to paste the object to the desired range. We specify this range
with the Destination argument as demonstrated below:
Selection.Cut
ActiveSheet.Paste Destination:=place
The final code is displayed in Figure 14.11. This macro is assigned to the “Position
Shape” button. The result of positioning the selected square in cell A12 appears in
Figure 14.12.
23
Chapter 14: Variables
[Note: To continue a line of code onto the next line in the Code Window, use the
underscore ( _ ). If you are continuing a line of code in the middle of a text string, you
must concatenate it (with the & sign) and use quotation marks, as shown in Figure
14.12.]
We have created six simple sub procedures along with one event procedure to complete
the functionality of this program.
We will now create a program that can make various calculations and create reports.
The user will be able to generate random numbers in a specified interval that will then be
converted to their absolute value and made into integers. The program will also
calculate each of the number’s square root, exponential power, and natural logarithm.
The user can also provide an angle to calculate the number’s sine, cosine, and tangent.
Finally, we will provide a “Clear All” button to clear current reports.
We first prepare our spreadsheet with the appropriate buttons and table headings (see
Figure 14.14). Let’s only calculate one random number and one angle at a time for now.
(We will learn loop structures in Chapter 16 to expand this ability.) We only need three
sub procedures for this program: CalcRandNum(), CalcAngles(), and ClearAll().
24
Chapter 14: Variables
Figure 14.14 Preparing the spreadsheet before writing the sub procedures.
We’ll begin with the random number generation and calculations. First, we want to ask
the user to specify an interval in which he or she wants the random number to be
generated. We will use the InputBox function to acquire this interval from the user with
two variables. We declare these as private variables in the CalcRandNum() sub
procedure and name them low and upp. We define the prompt, title, and default values
of the InputBox function as follows:
low = InputBox("Please enter the lower bound of the interval in which you" & _
"would like to generate a random number: ", "Lower Bound", -10)
upp = InputBox("Please enter the upper bound of the interval in which you" & _
"would like to generate a random number: ", "Upper Bound", 10)
We now use the following function to generate a random number in this interval:
25
Chapter 14: Variables
Now we should set this variable to a particular range in the worksheet. We do this in the
CalcRandNum() sub procedure by typing:
We can use this variable as a Range object and apply the Offset method to insert
calculation results into our table. So, we insert the first value, the random number
generated in the specified interval, into the first column:
RandStart.Offset(1, 0).Value = x
We can now use the Value property of our range variable to insert all other calculations
directly; that is, we do not need to make the calculation first and then insert the value.
Instead, we can do both in one step. The final code appears in Figure 14.15. (Notice that
the Sqr and Log functions are used with the Abs function to ensure that no errors are
created.) This macro is assigned to the “Random Number Calculations” button. The
result of this macro is shown in Figure 14.16.
For the CalcAngles() sub procedure, let’s begin by asking the user to enter an angle in
degrees (since most users will be more familiar with degrees than radians). We do this
using the InputBox function, with the prompt, title, and default value defined:
26
Chapter 14: Variables
Notice we used the same x value as before, which we declared as a public variable. We
now need to convert this value into radians before using any trigonometric calculations.
To do so, we will first need to declare the constant pi. We declare it in the same area in
which we declare public variables, by typing:
Const pi = 3.14
We can now use the following formula to make the angle conversion:
x = x * pi / 180
Now we use another range variable in this procedure to insert calculation values into the
angle calculation table. We also declare it as a public variable so that we can use it
again in the ClearAll() procedure. We name this variable AngleStart and declare it as
follows:
We can now use the Offset method again to insert into the report: the original degree
value of the angle; the converted radian value of the angle; and the Sin, Cos, and Tan
calculation results. The final code for this sub procedure is provided in Figure 14.17. This
macro is assigned to the “Angle Calculations” button; the result of this macro appears in
Figure 14.18.
Figure 14.17 The CalcAngles() sub procedure and all public declarations.
27
Chapter 14: Variables
The last procedure to write is the ClearAll() sub procedure. This procedure will simply
clear the values in both tables. We will use the ClearContents method so as to keep the
borders and the table’s other formatting features. We will only clear the second row of
both tables. (That is, we will not clear and retype table headings.) To do this, we use the
RandStart and AngleStart variables to define the ranges that we want to clear. To clear
the second row of the random number calculation table, we type:
These are the only two lines of code in this procedure. We can now assign this macro to
the “Clear All” button and perform a test to make sure that it works.
14.6 Summary
¾ Message Boxes and Input Boxes allow us to communicate with the user using
VBA code. A Message Box prints information to a small dialog box, and an Input
Box prompts the user to enter information into a small dialog box.
¾ The MsgBox function format is: MsgBox (prompt, [buttons], [title], [helpfile,
context]). The prompt argument is required; all others are optional. You can
capture the user’s response to your Message Box by setting the response
variable equal to the MsgBox function. You can check the value of this response
using VB Constants.
¾ The InputBox function has the following format: InputBox (prompt, [title], [default],
[xpos], [ypos], [helpfile, context]). The prompt argument is required; all others are
optional. The InputBox function is always assigned to a variable.
¾ A data type categorizes the values that can be assigned to a variable.
¾ To declare a variable in VBA, use the command Dim. It is important to remember
that VBA for Excel is case-sensitive.
28
Chapter 14: Variables
¾ Integers and doubles are both numerical values. Integers are non-decimal
numbers ranging in value from -32,768 to 32,767. Doubles have values from -
1.79769E308 to -4.94065E-324 for negative values and from 4.94065E-324 to
1.79769E308 for positive values.
¾ A string is a segment of text. This text can include both upper- and lower-case
letters, punctuation marks, and numbers.
¾ A Boolean is a variable whose value is either True or False.
¾ A range is a variable that can be used with all of the properties and methods of
the Range object. We can use this data type to define a StartCell. We can set the
value of this range variable at the beginning of our code and reference this
StartCell variable name throughout the remainder of the code.
¾ The worksheets data type defines a Worksheets object. This variable can be
assigned any of the properties or methods used with the Worksheets object.
¾ The object variable can be used to define any object in Excel, whether it is a
drawing object, a range, a worksheet, or any other object. Any of the properties or
methods associated with any object in Excel can be used with the object variable.
¾ There are several math functions in VBA that can be used with variables or
values; some are:
o The Abs function calculates the absolute value of a variable.
o The Sqr function calculates the square root of a number.
o The Mod function calculates the modular of a number given its divisor.
o The Int function removes the decimal part of a double variable and
returns the integer part; therefore the result will be an integer data type.
o The Rnd function generates a random number.
o The Exp function raises the constant e to some power given in the
function statement.
o The Log function calculates the natural log (the logarithm with base e, of
a given number).
¾ There are a few trigonometric functions in VBA that you can use with variables or
values:
o The Sin, Cos, and Tan functions take an angle and return the ratio of two
sides of a right triangle.
o The Atn function calculates the arctangent of a given ratio. The result is
an angle in radians, which is always a double data type.
¾ You can also derive other functions using the pre-defined VBA functions:
o The Arcsin function calculates the arcsine or inverse sine as follows:
Arcsin(X) = Atn(X / Sqr(-X * X + 1))
o The Sec function finds the secant and is defined as follows:
Sec(X) = 1 / Cos(X)
¾ There are several conversion functions in VBA:
o CInt converts to integer data type
o CDbl converts to double data type
o Val finds numerical value in an expression
o CStr converts to string data type
o CDate converts to date data type
o Asc converts characters to numbers
o Chr converts numbers to characters
¾ There are several string functions in VBA:
o UCase converts the string to all uppercase
o LCase converts the string to all lowercase
o Len finds the length of the string
29
Chapter 14: Variables
14.7 Exercises
14.7.1 Review Questions
1. What is the difference between Message Boxes and Input Boxes? List the
arguments and give an example of each type of box.
2. List seven variable types and an example of each.
3. Can a String variable be set to the value of any number variable, such as Integer
or Double, and vice versa? If so, how does the number change?
4. Can Worksheet and Object variables both be used to reference worksheets? Can
a Worksheet variable be set to an Object variable, and vice versa?
5. What is the result of the following code:
Sub PIN()
Dim x As Integer, y as integer
x = InputBox(“Enter last two digits of SSN:”)
y = InputBox(“Enter birthday month:”)
MsgBox “Your new PIN number is: “& y & x & (10 *Rnd + 1)
End Sub
30
Chapter 14: Variables
15. Using the Message Box in the previous problem, prompt the user for a value first
and then display the countdown.
3. Create a sub called “NameAndAge” that performs the following. (Give an appropriate
title for each Message Box and Input Box.)
a. Asks for the user’s name and stores it in a string variable.
b. Asks for the user’s age and stores it in an integer variable.
c. Displays a message similar to: “User’s Name is User’s Age years old.”
4. Create a sub called “Average” that asks the user to input two integers and then
presents the average of the two numbers. Provide an appropriate title for each
Message Box and Input Box.
a. Use an Input box for each integer and direct the user to enter an integer
value.
b. Store the two integers in integer variables: Integer1 and Integer2.
c. Store the result of the average calculation in single variable: Result.
d. Display the result rounded to two decimals in a message similar to: “The
average of Integer1 and Integer2 is Result.”
5. Create a template for a “For Sale” sign (as shown below). You may want to use
several range names to make the following tasks easier.
a. Ask the user to enter the item name in an Input Box. Place this value in
the template everywhere the word “Item” appears.
31
Chapter 14: Variables
b. Ask the user to enter a description of the item. Place this text in the
template accordingly.
c. Ask the user to enter his or her phone number and place it in the
template.
d. Ask the user to enter the date on which his or her sale ends and place it
in the template.
6. Create a sub called “StringManipulation” that performs the following actions. Assign
an appropriate title for each Message Box and Input Box.
a. Ask the user to input four characters of a word in string variable FirstWord
and display the variable in a message.
b. Ask the user to input four characters of a word in string variable
MiddleWord and display the variable in a message.
c. Ask the user to input four characters of a word in string variable LastWord
and display the variable in a message.
d. Concatenate the FirstWord, MiddleWord, and LastWord into one word,
store it in Word, and then display the contents of Word.
8. Create different sub procedures that generate the following sequences of numbers so
that the sequences are generated one number at a time each time a corresponding
button is pushed:
a. Even numbers
b. Odd numbers
c. Fibonacci
d. Geometric
9. Perform the following tasks for creating a user-defined chart of user-provided data:
a. Ask the user to input the title of two columns of data.
32
Chapter 14: Variables
b. Ask him or her to also provide you with five points for each column.
c. Output all of this information to the spreadsheet.
d. Ask the user what kind of chart he or she wants. Only provide three
options and make sure the user supplies you with the exact chart type
name that Excel uses (ex: xlColumnClustered).
e. Create a chart of the data that you enter into the spreadsheet of the chart
type the user provided.
10. Create a highlighting program. Allow a user to select a range of text and then
highlight it by specifying a color.
a. After the user selects the range of cells they want to highlight, prompt them
for the color they want to use. Only give them a few choices. Hint: use
ColorIndex property (ex: ColorIndex = 3 for red).
b. Then, apply this color formatting to their selection.
12. Combine VBA with Excel functions. Using the following table, prompt the user for a
product name and a month number. Use these values along with a LOOKUP
function to search for the amount sold for the specified product during the specified
month.
33
Chapter 14: Variables
14. In this exercise the student will create a histogram from scratch (without using the
Data Analysis toolpack). Create a procedure that performs the following:
a. Randomly generates 30 data points from uniform distribution between 5
and 30.
b. Uses the intervals (5 -10), (10 – 15), (15 – 20), (20 – 25), and 25+ to
calculate frequencies.
c. Uses the Application object and a counting function to find the frequency
of the randomly generated data for each of the intervals in part (b).
d. Creates a table with the intervals and the corresponding frequency
values.
e. Graphs this data as a bar chart.
15. Perform the following extensions to the Creating and Modifying Shapes application:
a. Add the ability to create a triangle, delete a triangle, and count the
number of triangles currently displayed.
b. Add an option to format the selected shape.
i. Prompt the user to specify what color he or she wants the shape
to be.
ii. Prompt the user to specify what line thickness he or she wants the
shape to have.
16. Perform the following extensions to the Making Calculations and Creating Reports
application:
a. Add a table below the angle calculation table that reports the results of an
arctangent calculation.
b. Create a button called “Arc Tangent” to which you will assign a macro
called CalcArcTan().
c. This macro should prompt the user for a ratio, calculate the arctangent,
and convert the answer to degrees. (All three values should be displayed
in the table.)
17. Create a sub called “RandomColors” that assigns colors in a random way to each
cell of the range D4:H8. Hint: set the ColorIndex of each cell in the range to a
random integer between 1 and 10.
18. Create a sub called “ConvertInputs.”. Give an appropriate title for each Message
Box and Input Box. This sub should:
a. declare a Double variable called InputValue;
b. set InputValue equal to the result from an Input Box requesting the
user to enter a number; and
c. return the square of InputValue rounded to three decimal
numbers.
19. Create a sub called “ActivateWorksheet.”. Give an appropriate title for each
Message Box and Input Box. This sub should:
a. ask the user for the name of a worksheet to activate;
b. activate the worksheet if it exists in the active workbook; and
c. otherwise, display an appropriate message.
34
Chapter 14: Variables
20. On a spreadsheet, create three triangle shapes. Name these shapes “Tri1”, “Tri2”,
and “Tri3” respectively. Create a sub called “FindObject.” This sub should:
a. Ask the user for the name which triangle to select;
b. Ask the user what color they want to make this triangle; and
c. Change the color of the specified triangle appropriately.
Data Results
Mean =
Std Dev
=
Min =
Max =
MSE =
22. Create a small program that can calculate the distance between cities. Given the
X-Coord (latitude) and Y-Coord (longitude) of several US cities below, create four
buttons corresponding to the following options: find vertical distance; find horizontal
distance; find Euclidean distance; and compare three cities to see which pair is
closest (see the figure below).
b. Perform these options by prompting the user for the city names with an
Input Box. Then use a lookup function to find the necessary latitude or
longitude values. Use VBA functions to find the values that you are
looking for and display them to the user in a Message Box.
c. For the comparative option, prompt the user to enter three cities and then
assign three variables to the distance between each pair of cities. Use an
Application.WorksheetFunction to find the minimum of these variable
35
Chapter 14: Variables
values. Then display a Message Box to the user with the pair of city
names that are the closest.
23. Create an address book. Given the list of people and addresses below, create a
procedure that allows a user to search for a particular name and returns the
address from the table.
a. Search for the name using the match function and the Application object.
Name the range that contains the names in the address table.
b. Then use the Offset property (or Cells property) to gather the
corresponding information. You may need to name another range to
make this easier.
c. Present this information to the user in a Message Box as per the following
example:
24. Create a personal budget organizer that keeps track of the companies to which you
pay bills each month (see the figure below).
a. Record the name and account number for each company and the amount
and date of the last payment.
b. The user should be able to add a new company to the list. He or she
should provide the name and account number using Input Boxes.
c. The user should also be able to enter a new payment. To do so, he or
she should first provide the company name through an Input Box. Then,
the user can enter the amount and date of the payment via Input Boxes
as well. Use a match function to enter the payment information into the
right row in the table.
d. The user should also be able to see how much he or she spent in bills for
the month. Calculate this total and display it to the user in a Message
Box.
36
Chapter 14: Variables
Assign each procedure to a button. To the first button called “Reset” assign the
procedure of part (a). To the second and third buttons, called “Compare to
Normal” and “Compare to Exponential”, assign the procedure created in parts (b)
and (c).
a. Ask the user to choose a currency type to convert the money from and a
currency type to convert the money to.
b. Ask the user to input the amount of money to be converted.
c. Find the exchange rate using the table below.
d. Create a message box that presents the result of the money exchange.
37
Chapter 14: Variables
27. Create a procedure called “WeightConverter” that helps converting weight. The
following table presents the conversion rates. The procedure should:
a. Ask the user to choose unit to convert from and a unit to convert to.
b. Ask the user to enter the amount to be converted.
c. Create a message box that presents the results of the conversion.
38
Chapter 14: Variables
30. Create a procedure called “VolumeConverter” that helps converting volume. The
following table presents the conversion rates. The procedure should:
a. Ask the user to choose unit to convert from and a unit to convert to.
b. Ask the user to enter the amount to be converted.
c. Create a message box that presents the results of the conversion.
39
Chapter 15
Sub Procedures
and Function Procedures
Chapter Overview
15.1 Introduction
15.2 Sub Procedures
15.2.1 Calling Other Subs
15.2.2 Passing Variables in Subs
15.6 Summary
15.7 Exercises
15.1 Introduction
Chapter 15: Sub Procedures and Function Procedures
This chapter discusses how to use sub procedures and function procedures in VBA. This
is a useful topic since most of the programs for DSS applications will have several
different procedures. The reader should learn how to call various procedures and pass
variables to procedures.
The ideal module structure for your program is to have one Main() sub procedure from
which other sub procedures are called. To call another sub procedure, we use the
command Call followed by the sub procedure name. (For sub procedures, you do not
need to include the parenthesis in the sub name to call it; this is not true for calling
function procedures.) The Main() sub procedure can then be assigned (as a macro) to a
“Start” button on the “Welcome” worksheet, which the user would see upon opening the
workbook. The entire program would begin once the user pushes this button.
For example, let’s say we have a program that gathers user input, performs calculations,
and creates a report. In this case, we divide these functions into three different sub
procedures, called GetInput(), Calculations(), and CreateReport(), respectively. We can
then call these three sub procedures from the Main() sub procedure as follows:
Sub Main()
Call GetInput
Call Calculations
Call CreateReport
End Sub
We may need to call other sub procedures from the GetInput(), Calculations(), or
CreateReport() procedures if there is a set of actions that must be performed before one
of these sub procedures can continue. This set of actions may be long enough to group
as another sub procedure. For example, there may be a small sub procedure called
CheckInput() that must be called to verify the validity of the data before the GetInput()
sub procedure can finish.
We can also pass variables to a procedure when calling it. The default method for
passing variables is by reference; we specify ByVal in the sub argument to pass by
value. Passing a variable by reference implies that we are giving the name of the
variable to which the procedure can refer to find the current variable value; that is, we
are referring to the storage space of that value. Passing a variable by value implies that
we are copying the current value of the specified variable and using that value in the
procedure. To pass a variable in VBA, we need to insert the variable as an
2
Chapter 15: Sub Procedures and Function Procedures
argument/parameter of the sub when it is called. When calling a sub, we use the
following structure to pass variables:
If passing by reference, the variable name used as an argument when the sub is called
and the variable name used in the sub procedure statement do not need to be the same;
however, they must be of the same data type if the data type is specified in the sub
statement. For example we could type the following:
We could also use the same variable names as declared in the original sub if we desire.
That is, we could have called the Format sub by passing the variables RangeUser and
ColorUser. If we want to pass by value instead of by reference, we type the following
instead:
Passing variables in sub procedures can be helpful when performing a task multiple
times with changing parameters. As in the above example, we have formatted cells
multiple times using different property values specified in the passed values. We can
also use subs and variables in this way to perform more complicated tasks. Consider the
following example in which we have called a sub repeatedly in a loop by passing the
offset values i and j.
For i = 1 to 5
For j = 1 to 5
Call InserValues(i,j)
Next j
Next i
-----------------------------------
Sub InsertValues(i,j)
With Range(“Values”).Offset(i,j)
.Value = i*j
.BordersAround Weight:=xlThin
End With
End Sub
Subs can also be used to update other variable values. Consider the example below in
which we call a sub passing two variables x and y that are used to update another
variable SumVar.
3
Chapter 15: Sub Procedures and Function Procedures
Call Sum(x, y)
----------------------------------------
Sub Sum(x, y)
SumVar = x + y
End Sub
While this method is viable, function procedures offer a more efficient way to write this
calculation code.
Sub Procedures:
Sub SubName()
Summary Dim variable As DataType, x As DataType
…
Call SubName2(variable)
…
End Sub
-------------------------------------------------------------
Sub SubName2(variable)
…
use variable in a set of actions
use this variable to update the value of another variable
End Sub
Call FunctionName(a,b,c)
--------------------------------------
Function FunctionName(a,b,c)
….(actions)…
End Function
OR
x = FunctionName(a,b,c)
(OR MsgBox FunctionName(a,b,c), etc.)
------------------------------------------
Function FunctionName(a,b,c)
…(actions)…
4
Chapter 15: Sub Procedures and Function Procedures
FunctionName = value
End Function
Just as with sub procedures, it is not necessary that variables be passed when calling a
function. That is, your function may not require any input from the procedure in which it
is called, and it may not necessarily return any value. However, most of the time, we will
use functions that pass variables and return some value. As with sub procedures, the
default method for passing variables is by reference; we can specify ByVal in the
function argument to pass by value.
We use the following structure to call a function that will not return a value:
Call FunctionName(variable)
x = FunctionName(variable)
If the function receives a variable as input to the function, the function procedure
statement must include a variable name as an argument. For example, if we create a
function that calculates the sum of two values, we define the function procedure as
follows:
Function Sum(a, b)
……
End Function
The variable name used as an argument when the function is called and the variable
name used in the function procedure statement do not need to be the same; however,
they must be of the same data type if the data type is specified in the function statement.
For example we could call the above function from another procedure using this
statement:
Call Sum(x, y)
This would imply that we have declared x and y as variables in the procedure from which
we call our function. The variables a and b in this example do not need to be declared;
they will be the variables used throughout the function procedure. In this case a and b
will assume the respective data types assigned to x and y. However, if we had defined
our function statement with data types in the argument, we would be restricted to only
passing variables of that data type. For example:
To return a value from a function in VBA, we assign a value to the name of the function
procedure. That is, instead of using a return command or variable, we set the function
name equal to the value that we want to return. For example, to return the value of the
sum in the above function, we would type the following in the function procedure:
5
Chapter 15: Sub Procedures and Function Procedures
Function Sum(a, b)
Sum = a + b
End Function
Function Procedures:
Sub SubName()
Summary Dim variable As DataType, x As DataType
…
x = FunctionName(variable)
…
End Sub
-------------------------------------------------------------
Function FunctionName(a variable name)
…
FunctionName = value
End Function
Note that both sub procedure and function procedure names are case-sensitive and
cannot conflict with VBA commands.
An interesting feature about creating function procedures is that they become User-
Defined functions in the Excel list of spreadsheet functions. That is, if you create a
function procedure, with or without parameters, you can find this function listed in the
User-Defined function category in Excel. This is beneficial for enhancing
yourspreadsheet capabilities by using a small amount of VBA code.
A Public procedure, however, can be called from any other procedure. You can type the
word Public in front of the Sub or Function statement, or you can write the Sub or
Function statement without a preceding statement.
Consider four small sub procedures as an example of Private and Public procedures.
Two of these procedures are Private: Test1() and Test2(). Since they are in the same
module, they can call one another. For example, suppose these two sub procedures
have the following code:
6
Chapter 15: Sub Procedures and Function Procedures
If we run the Test1() sub procedure (from the VBE, using the run button), we will see one
Message Box that reads “This is Test1,” followed by another that reads, “This is Test2.”
If we have a third sub procedure called Test3() that is Public but in another module, we
will not be allowed to call either of the Private sub procedures in the original module.
That is, Test3() cannot contain the code Call Test1 or Call Test2. However, we can call
this Public procedure from one of our Private procedures. In other words, we can modify
the Test1() procedure to say:
If we now consider a fourth sub procedure called Test4(), which is also Public and in the
same module as Test1() and Test2(), even though Test4() is Public, it can still call the
Private procedures since they are in the same module.
Private: Cannot be called from a procedure that is not in the same module and
cannot be found in the macro list when trying to play a macro from Excel.
15.5 Applications
There are various functions that may be useful to create. Many functions are already
available to us through Excel formulas and VBA math functions; however, some
programs and DSS’s require customized functions. In this section we will demonstrate
navigating functions and some derived math functions.
In many of the case studies that we develop, and in general good GUI design, there are
several buttons in our workbook used to navigate through the different spreadsheets.
Some of these buttons may be called Continue, Next, Back, Previous, etc. Each time
one of these buttons is clicked, we want to close the current worksheet, i.e. hide it and
make the next appropriate worksheet visible. (We want to try to always have only one
7
Chapter 15: Sub Procedures and Function Procedures
sheet showing to minimize the user’s awareness of background actions being performed
in the spreadsheet and code.) If we try to create one sub procedure to associate with
these buttons, we find that each time a button is clicked we are not performing
completely identical actions.
For example, consider a workbook with several worksheets: “Input,” “Step 1,” “Step 2,”
and “Optimization”; the user should view these worksheets in the order listed. If we click
a Next button on the “Input” worksheet, we want to hide the “Input” sheet and make the
“Step 1” sheet visible. Here is the coded sub procedure:
Sub NexSheett()
Worksheet(“Step 1”).Visible = True
Worksheet(“Input”).Visible = False
End Sub
(Note: We must make the new sheet visible before hiding the current sheet. If we make
these statements in the reverse order, we will momentarily have a workbook with no
visible worksheets, which will create an error in VBA.)
However, we cannot assign this same macro to the Next button found on the “Step 1”
worksheet. If we press Next on the “Step 1” sheet, we want to make the “Step 2” sheet
visible and hide the “Step 1” sheet. Its code is:
Sub NexSheett()
Worksheet(“Step 2”).Visible = True
Worksheet(“Step 1”).Visible = False
End Sub
We therefore compare the similarities of these sub procedures to see if we can construct
a function procedure that works with any of these button types on any worksheet. There
are two different worksheet names that we need to know to set the appropriate Visible
properties: the worksheet that we are hiding and the worksheet that we are showing.
The worksheet that we are hiding is just the current worksheet, or the ActiveSheet.
Therefore, we can just use the ActiveSheet object and set the Visible property equal to
false. Regardless of which sheet has the Next button, we can always use the
ActiveSheet object to hide it.
The name of the worksheet that we are opening, however, will change each time. Our
solution is to create a function procedure that passes a worksheet name as its variable;
we call this the NextSheet() function. We can capture the desired worksheet name in
each unique sub procedure associated with the buttons on each sheet and then call a
function to make this worksheet visible and hide the current worksheet. Our function can
be used as follows:
8
Chapter 15: Sub Procedures and Function Procedures
End Sub
--------------------------------------------------
Function NextSheet(name)
Worksheets(name).Visible = True
ActiveSheet.Visible = False
End Function
(This particular sub procedure would be associated with the Next button on the Input
worksheet; note that you cannot assign functions to buttons, only macros.)
We use this function structure above to assign related macros to the Next button on
each of the sheets in our workbook. The final code for these sub procedures and the
function procedure appear in Figure 15.1.
Figure 15.1 Each sub procedure is associated with a button on each worksheet. The
NextSheet() function receives the name value of the worksheet that it
displays
We may also have a particular sheet that is usually hidden but that can be shown at
some point, for example, an “Example” worksheet that the user may refer to at times
while using the program. To view this “Example” sheet, there may be a “View Example”
button on all the other sheets in the workbook. If the user clicks this button, we want to
close the current sheet and show the “Example” sheet. On the “Example” sheet, we may
have a “Return to Program” button that should re-open the previously visible sheet.
9
Chapter 15: Sub Procedures and Function Procedures
So we know which sheet to re-open when the user clicks the “Return to Program” button,
, we need to capture the original worksheet name in which we first click the “Example”
button. We accomplish this using a worksheets variable and a navigating function; this
time we don’t need to pass a variable.
Let’s consider the same set of worksheets as in the above example with an additional
“Example” sheet. We declare a Public variable ws as a worksheet data type and use a
function to capture this value and hide the worksheet. When the “Return to Program”
button is clicked, this same variable can be used to re-open the appropriate sheet. The
code is as follows:
Public ws As Worksheet
----------------------------------------------
Sub ViewExample()
Worksheets(“Example”).Visible = True
Call CloseCurrent()
End Sub
----------------------------------------------
Function CloseCurrent()
Set ws = ActiveSheet
ws.Visible = False
End Function
----------------------------------------------
Sub ReturnToProgram()
ws.Visible = True
Worksheets(“Example”).Visible = False
End Sub
These sub procedures are assigned to appropriate buttons in the workbook to implement
the navigational tools (see Figure 15.2).
Figure 15.2 The “Return to Program” button takes the user to the previously visible
sheet that was captured by the worksheet variable.
To create navigating functions, you can also use the index value of the worksheets. To
find the index value (or tab number) of a worksheet, you can simply use its Index
property. For example, if you write the code MsgBox Worksheets(“Sheet 1”).Index, a
Message Box with the value 1 is shown. Once you know a worksheet’s index value, you
can simply refer to it by this index (instead of by the worksheet name). For example,
instead of writing Worksheets(“Sheet 1”).Visible = True, you can just write
Worksheets(1).Visible = True.
10
Chapter 15: Sub Procedures and Function Procedures
As we learned in Chapter 14, the pre-defined VBA math functions and trigonometric
functions can be used to derive new functions. To actually create these derived functions
in Excel, we create a function procedure and both pass a variable and return some
value. For example to create the Log base n derived math function, we create the
following function:
Function LogBaseN(x, n)
LogBaseN = Log(x) / Log(n)
End Function
Prior to calling the function, the values for x and n would need to have been assigned in
the sub procedure that calls this function. In these functions, we may not directly call the
function using the Call statement; we can instead just refer to the function name, such as
in a Message Box or another function. When we refer to this function, it will call the
function procedure to determine the return value. Therefore, we should have something
like the following code in another sub procedure:
Sub FindLog()
Dim x As Integer, n As Integer
x = InputBox(“Enter x value of Log base n of x function: “)
n = InputBox(“Enter n value of Log base n of x function: “)
MsgBox “The value of Log base “ & n & “ of “ & x & “ is: “ & LogBaseN(n, x)
End Sub
Note that the variables x and n are declared Privately in the sub procedure but can still
be passed to the function. Also observe that since the data type for x and n is the same,
we can just state it once after declaring both variables. It is irrelevant in which order you
enter sub procedures and functions into a module. The module does not read the code
from procedure to procedure; rather, one particular procedure is run, and only if another
procedure is called is it also run.
15.6 Summary
¾ The ideal module structure for a program is to have one Main() sub procedure
from which other sub procedures are called.
¾ To call another sub procedure, use the command Call followed by the sub
procedure name.
¾ A sub procedure, like a variable, can also be defined as Public or Private.
¾ Declare a Private sub procedure by putting the word Private before the Sub
statement. Private sub procedures can only be called from procedures in the
same module and are not listed when you try to run a macro in Excel.
¾ A Public sub procedure can be called from any other procedure. The word Public
can be put in front of the Sub statement, or the Sub statement can be written
without a preceding statement.
¾ Function procedures are sub procedures that can pass variables or return values.
A function procedure can be called from any sub procedure or any other function
procedure, using the Call statement.
¾ Function procedures are similar to sub procedures and follow this basic structure:
11
Chapter 15: Sub Procedures and Function Procedures
Function FunctionName()
….
End Function
¾ To pass a variable in VBA, you should insert the variable as an
argument/parameter of the function when it is called. Use the following structure
to call a function: Call FunctionName(variable to be passed)
15.7 Exercises
15.7.1 Review Questions
1. Write a function called CalculateHypotenuse that takes variable Side1 and variable
Side2 as arguments, where Side1 and Side2 are the sides of a right triangle. Write a
Main sub that uses the function to calculate the hypotenuse of the three triangles
listed below.
Side1 Side2
Triangle 1 3 7
Triangle 2 4.5 2.2
Triangle 3 4 5.5
The Main sub sends the side lengths to the function as doubles if they appear as
doubles in the table above. For each triangle, the Main sub displays in a
message the side lengths and the hypotenuse length rounded to two decimal
places.
12
Chapter 15: Sub Procedures and Function Procedures
2. Using the idea of indexing worksheets, create a navigational function for using a Next
and Back button on any worksheet.
3. Create a program that enables the user to compute random numbers from a Normal
distribution and graph the corresponding values. This program will ask the user to
input the total number of random numbers to be generated, the mean and standard
deviation of the distribution. The program will then compute the random values, and
display user’s input, the random values and the corresponding graph. Below we
describe the sub procedures needed to complete this exercise:
a. The InputData sub procedure asks the user to input the total number of random
numbers to be generated, the mean and standard deviation of the distribution.
This data is then displayed in the worksheet.
b. The RandomNr sub procedure generates and displays the random numbers in
the worksheet.
c. The Reset sub procedure clears the current worksheet.
d. The Graph sub procedure graphs the random numbers generated.
e. The Main sub procedure calls the other subroutines in the appropriate order.
Place a button labeled “Random Values” on the worksheet. Assign the Main sub
procedure to this button.
4. Create a button on the active worksheet that calls a program to perform the following
operations:
a. AutoFormat the table below to a preset format design.
b. Use the data in the table to create a 3-D Column Graph using a function
procedure.
5. Create a button that will copy necessary information from one worksheet onto another
to create a final report that organizes the list by types of products sold and includes
only the company name, products sold, and annual sales figures. Also create a Pie
Chart using the annual sales values provided through a function procedure.
Company Annual
Name Sales Rep Phone Products Sales
Quality Foods Donna O'Keefe 111-1111 dry goods $52,965.00
KitchenWare Ryan Brown 222-2222 cookware $16,432.00
13
Chapter 15: Sub Procedures and Function Procedures
6. Modify the code below so that the main subroutine has the following structure and the
sheet has the following layout:
Sub main()
Call RequestInfo
Call Calculate(Price, Amount)
Call PrintReport
End Sub
Original code:
Dim Customer As String, Product, Amount As Double, Price As Currency, Sale
As Currency
Sub HandsOn2()
Customer = InputBox("Enter customer name:")
Product = InputBox("Enter product sold:")
Amount = InputBox("Enter quantity of " & Product & " sold:")
Price = InputBox("Enter unit price of " & Product & ":")
Sale = Price * Amount
Range("A2").Value = Customer
Range("B2").Value = Product
Range("C2").Value = Amount
Range("D2").Value = Sale
MsgBox "Transaction Complete. Congratulations on your sale to " & Customer &
"."
End Sub
7. Create a procedure which prompts the user for the size of data s/he is interested in
generating. Prompt the user for a mean and standard deviation for the Normal
distribution; store these in variables. Prompt the user for a lambda value for the
Exponential distribution; store this in a variable. Prompt the user for a lower and an
upper bound value for the Uniform distribution; store these in variables. Then call the
following three functions:
a. Normal Function: (pass size, mean, stdev) generate a column of random
values from the Normal distribution.
b. Exponential Function: (pass size, lambda) generate another column of random
values from the Exponential distribution.
14
Chapter 15: Sub Procedures and Function Procedures
c. Uniform Function: (pass size, lower and upper bound) generate another
column of random values from the Uniform distribution.
10. A person preparing his cost sheet wants to review his list of orders and refer to a
table with prices to calculate his total bill. His orders are in a large list of three
columns. The price table is on another sheet. He wants to reference the price for
each order item and create a new table with only two columns: one for the item and
one for its price (see tables below).
a. Write a procedure that:
i. captures the value of the selected cell in a variable and
ii. prompts the user to enter the type of item in the list (Book = 1, CD
= 2, Movie = 3).
Orders:
Book A Movie F
CD B Movie C
Movie A CD A
Book D Movie D
Book B Book C
CD D CD F
CD E Book E
Book F Movie B
15
Chapter 15: Sub Procedures and Function Procedures
Price List:
Book A $35.00 CD A $15.00 Moive A $22.00
Book B $40.00 CD B $13.00 Movie B $21.00
Book C $37.00 CD C $14.00 Movie C $25.00
Book D $28.00 CD D $18.00 Movie D $19.00
Book E $32.00 CD E $16.00 Movie E $18.00
Book F $27.00 CD F $15.00 Movie F $21.00
Cost Sheet:
Book A
CD B
Movie A
Book D
Book B
CD D
CD E
Book F
Movie F
Movie C
CD A
Movie D
Book C
CD F
Book E
Movie B
11. Create a procedure to convert the temperature from degrees Celsius to degrees
Fahrenheit or vise versa. This procedure: asks the user to choose whether s/he
wants to convert the temperature from degrees Celsius to degrees Fahrenheit or
vise versa; calls function procedures that take as input the temperature entered by
the user and return the result of the conversion rounding it up 2 decimal points;
creates a message box to present the user with the results of the conversion.
12. The following is part of a library database. Create the following sub procedures that
will help to search the database.
16
Chapter 15: Sub Procedures and Function Procedures
Finally, create a main sub procedure that prompts the user about the type of
search to be performed (by title, author name, etc) and depending on the user’s
choice calls one of the sub procedure created. Assign the main sub procedure to
a button titled “Search the Database”.
Create a main sub procedure that prompts the user to enter the range of
data and chooses one of the sub procedures to format the data on the given
range.
17
Chapter 15: Sub Procedures and Function Procedures
Create a main sub procedure that prompts the user to enter the choice of the
shape and calls one of the sub procedures above to create the shape selected
by the user.
Create a main sub procedure that prompts the user to enter the range of data and
choose a chart type. The main sub procedure then calls one of the procedures described
above to chart the
18
Chapter 16
Programming Structures
Chapter Overview
16.1 Introduction
16.2 If, Then Statements
16.2.1 Logical Checks and Booleans
16.6 Applications
16.6.1 Banking Account Management
16.7 Summary
16.8 Exercises
16.1 Introduction
Chapter 16: Programming Structures
This chapter shows the reader how to use several different programming structures in
VBA. Programming structures in VBA include If, Then statements, several different loop
structures, Select, Case statements, and others. These structures allow you to place
conditions on running the program, performing calculations, and communicating with the
user. We use them when building any decision support system.
If condition Then
action1
End If
The If, Then statement defines the action to perform if the condition is false by using the
Else and ElseIf statements. The Else statement allows you to specify the action to be
performed if the condition is not met. (If this statement is not included in the If, Then
statement, then the program will perform no action if the condition is not met.) The
general format for using the Else statement is:
If condition Then
action1
Else
action2
End If
The ElseIf statement allows you to construct nested If statements. Recall the example
from Chapter 4 that used another IF function as the false argument of the original IF
function. In other words, instead of performing a direct action if the original condition is
not met, another If condition is considered. Note that when you use the ElseIf statement,
you must type Thenagain. (This step is not necessary for the Else statement.) To
elucidate this point, let’s look at a brief example:
Here we use both the Else and ElseIf statements to create a nested If statement. If the
first condition is true, the first Message Box will be displayed; if it is false, another If
condition is listed using the ElseIf statement. If this condition is true, the second
Message Box will be displayed; if it is false, the Else statement runs and the third
Message Box is displayed.
2
Chapter 16: Programming Structures
We also often use the If, Then statement for error checking. We will discuss some error
checking functions in a later section in this chapter and in more detail in Chapter 22;
however, we can now provide some smaller examples of error checking that are most
useful. Let’s revisit the MsgBox and InputBox functions.
We have already stated that a response variable can be defined (as a Variant data type)
and assigned to the MsgBox function to capture which button the user has pressed.
Then, we may want to perform a different set of actions depending on which button was
pressed (the result of the MsgBox function). An example of using the result of the
MsgBox function would be to determine whether or not the user wants to repeat an
action that he or she has just completed.
Suppose we have a sub procedure in which we prompt the user to enter an input value.
After recording the value, we may ask the user if he or she wants to enter another input
value. If the answer is yes, we repeat the sub procedure by calling it reiteratively (from
within itself). If the answer is no, then we simply end the sub. (Note that we specify that a
Yes and No button should be on the Message Box by using the vbYesNo value for the
button type argument.) The code for this example is:
Sub RecordInput()
Dim input As Double, response As Variant
An Input Box also has two possible button choices: OK and Cancel. (These buttons
cannot be changed.) There may be a procedure in which you do not want to continue
with subsequent actions if a user has pressed the “Cancel” button on an Input Box. For
example, in the above procedure, RecordInput, we may not want to enter the value to
the spreadsheet or perform the other tasks if the user never supplies any input in the
Input Box. To determine if a user has pressed Cancel on an Input Box, we use an If,
Then statement to check the value of the variable assigned to the InputBox function. If
this variable is empty, that is, equal to “”, then the user has pressed the Cancel button.
Consider the following modification of the above code:
Sub RecordInput()
Dim input As Double, response As Variant
3
Chapter 16: Programming Structures
End If
End Sub
We can also use If, Then statements to check for the possible problems mentioned
when using the End property. As we discussed in Chapter 13, the End property will not
return any range selection if there are no adjacent cell values to the range from which
you are searching. Suppose we are entering user input values into a column of data. We
always want to enter the newest value at the bottom of the list; therefore, we use the
End(xlDown) and Offset(1,0) properties to find the last value in the list and move one cell
down to enter the new value. Consider the following code that prompts the user to enter
input in a column that begins with a cell named “DataStart”:
Range(“DataStart”).End(xlDown).Offset(1,0).Value = UserValue
However, if the value that the user enters is the first in the list, the End property will not
return a range to Offset from and so we cannot enter the value. We will therefore add an
If, Then statement to check if the value is the first added. If it is, then we will enter it
directly below the initial cell in the column, “DataStart.” Otherwise, we can use the End
and Offset properties as seen above.
We will return to this discussion in a later section in this chapter and in Chapter 22.
We can also use If, Then statements with logical checks and Boolean variables. Logical
checks include And and Or statements, similar to the AND and OR functions we learned
in Excel (see Chapter 4).
The And logical statement requires every condition of the If, Then statement to be true in
order for the proceeding action to be performed. If only one of the conditions is false, the
action will not be performed and the following Else or ElseIf statement will be read, or
the If, Then statement will end. This statement structure is as follows:
4
Chapter 16: Programming Structures
The Or logical statement requires only one condition in the If, Then statement to be true
in order for the proceeding action to be performed. Every condition would have to be
false to skip it and read the Else, ElseIf, or End If statements. This statement structure is
as follows:
Compare these logical checks with the following two If, Then statements:
In the first If, Then statement, the And check requires that both conditions are met. If this
is true, then the value of x should be between 500 and 1000; therefore the program
displays the first Message Box. However, if either condition is false, then the statement
in the second Message Box must be true. In the second If, Then statement, we can see
the difference in using the Or logical check. With Or, either of the conditions can be true
to display the first Message Box. That is, either x can be greater than 1000 or less than
500. If neither condition is met, then the second Message Box must be true.
And and Or conditions can also be combined using parenthesis. For example, we could
type the following code:
If (x < 1000 And x > 500) Or (y < 1000 And y > 500) Then
MsgBox “One of your numbers is between 500 and 1000.”
End If
If, Then statements are used with Boolean variables to check if their values are True or
False. We can use the following two statements:
5
Chapter 16: Programming Structures
(Else and ElseIf statements are also useable.) To check if a Boolean variable is True, we
simply state the name of the variable; that is, the default value of any Boolean variable is
True. Instead of the first statement above, we could just type:
If variable Then
action1
End If
If found Then
MsgBox “The solution has been found.”
ElseIf found = False Then
MsgBox “The solution has not been found.”
End If
(Note that we do not need to use the ElseIf statement since the only other alternative to
found = True is found = False.) We can now begin to see how Boolean variables will be
useful in our programs. We will discuss more uses of Boolean variables in the next
section.
6
Chapter 16: Programming Structures
The End Select statement must be used to close the Select, Case structure. The word
Case always appears before each possible expression value. Note that the cases are
mutually exclusive. Here, the possible values we consider are 1 and 2. We can also give
a range of values as a case instance:
We can also use the optional Case Else statement to specify all other cases that are not
listed. The ability to provide a range of values as a case instance using the To statement
can be extended to string values:
Here the range of text is considered alphabetically; string values for the name variable
are checked on letter intervals. We may also include some conditions as cases, instead
of simple instances. This approach is useful in replacing several ElseIf statements in an
If, Then structure. For example:
We can also place a condition on a range of values using the Is statement. Employing
this statement may be more efficient than repeating the Select Case object name for
each Case:
7
Chapter 16: Programming Structures
16.4 Loops
Loops are programming structures that allow you to repeat a set of actions a certain
number of times. You can specify the number of loops by counting up to (or down to) a
certain value. Otherwise, the loops can run continuously while or until a certain condition
is met.
You can use the For, Next and For Each, Next loops to repeat a loop while counting up
to or down to a certain number; we refer to both of them generally as For Loops. To
count, you can use a simple index variable such as i or a counting variable such as
count or iteration; these variables are integer data types. The more common of these
two structures is the For, Next loop; its structure is as follows:
For i = 1 to n
actions
Next i
Use this structure to count up to a number n, starting at 1. For, Next loops can also be
nested. Each Next statement completes the loops using the counting variable for that
loop. Consider the example below (we will see another example of this in Chapter 17
when using arrays).
For i = 1 to n
For j = 1 to m
actions
Next j
Next i
8
Chapter 16: Programming Structures
There is also a Step parameter with this loop. The Step value specifies how much the
counter variable (in this case I) should increase during each loop. If Step is not specified,
the default value is 1. Therefore, the above structure would increase i by 1, starting at 1
until, and including, n. If you wish to count down to a number, the Step value should be
negative and the start value should be greater than the end value. For example:
For i = n to 1 Step -1
actions
Next i
This loop will count down to 1 starting at n and decreasing i by 1 each time. Notice that
the increase or decrease in the counter variable by the Step amount is part of the loop
structure; therefore, the variable does not need to be incremented or decremented in the
set of actions as in several other programming languages. Also note that it is not
necessary to specify the counter variable name after the Next statement.
The For Each, Next loop works almost identically to the For, Next loop. The only
difference is that For Each, Next counts a certain number of objects in a group of
objects; that is objects are counted rather than using a counter variable.
Note that we do not need to specify the object being counted after Next. Here we are not
repeating this loop until the counter variable reaches a certain value, but rather until
there are no more worksheets to be found in the workbook. Whenever each object in the
collection of objects has been examined, the loop ends.
16.4.2 Do Loops
There are two main Do Loops: Do, While and Do, Until. These Do Loops perform a set
of actions repeatedly while or until a condition is met. There are two main structures for
these loops. Let’s consider the Do, While loop first. In the structure below, a While
condition is considered before a set of actions is performed.
Here, the While condition is checked first, a set of actions are performed, and then the
count value is increased. In the structure below, the set of actions will be performed, and
then the While condition will be checked before the actions are repeated.
9
Chapter 16: Programming Structures
Do
actions
count = count + 1
Loop While count < 10
In this example, the actions occur first; then the count value is increased, and then the
While condition is checked. It is important to consider which format is best, depending on
whether or not it is appropriate to perform the actions prior to checking the While
condition. These two structures are also optional for the Do, Until loop.
The difference between looping while the condition is met and until the condition is met
is important to note. For the Do, While loop, if the condition is true, it signals the loop to
repeat the actions. However, for the Do, Until loop, if the condition is true, it signals the
loop to stop repeating the actions. For example, compare the values generated by the
following two loops.
For the first loop, assuming the value of the count variable is initialized at 1, the condition
will be met the first time (1 <= 10) and the next 9 times. The action (x = 2*x, again
assuming x is initially 1) will therefore be repeated 10 times, yielding the final values of x
= 210 and count = 11. The second loop, however, will stop repeating when count reaches
10 (but not including 10), thus yielding final values of x = 29 and count = 10.
These Do Loops can also be used with Boolean variables. That is, the Boolean variable
can be used as the condition for the Do, While or Do, Until loops. These conditions
usually imply some nested If, Then statements that change the value of the Boolean
variable once a certain result is found. For example:
10
Chapter 16: Programming Structures
Here we are performing some actions while a certain result is still not found. Once the
found variable is set to True, the While condition is no longer met and the loop ends. We
could have also used the Do, Until loop by changing the condition to:
We will commonly use Boolean variables as conditions for Do Loops in our decision
support systems. Note that both the Do, While and Do, Until loops can have nested
loops. A Do, While loop can contain a Do, Until loop and vice versa. Also, as we have
shown above, we may often include If, Then statements or other programming structures
within loops.
(Note that there is also a While, Wend statement available in VBA; it has the same
functionality as the Do, While loop. However, since the Do, While structure offers more
flexibility, we will use it instead of the While, Wend statement.)
To exit a procedure, we use either Exit Sub or Exit Function, depending on if we are
currently running a sub procedure or function procedure, respectively. When Exit
Function is stated, the function procedure stops executing and the program returns to
the point in the code from which the function was called. When we use the Exit Sub
11
Chapter 16: Programming Structures
statement, the sub procedure stops executing, and, if the sub was called from another
sub, the program returns to this sub and begins executing the next line of code.
For example, in the two procedures below, we use both the Exit Function and Exit Sub
statements with If, Then statements. With a Divide function, we perform simple error
checking.
Sub Calculations()
x = InputBox(“Enter x value.”)
y = InputBox(“Enter y value.”)
value = Divide(x, y)
If value = “none” Then
Exit Sub
End If
MsgBox “x divided by y is “ & Divide(x, y)
End Sub
------------------------------------------------
Function Divide(a, b)
If b = 0 Then
Divide = “none”
Exit Function
End If
Divide = a / b
End Function
If the denominator in the Divide function is 0, then we do not continue evaluating the
function since we know it will lead to an error; so, we use the Exit Function statement.
Once the Exit Function statement is read, the program returns to the line of code below
where the function was called. This line is the If, Then statement of the sub procedure.
Since we also do not want to display the faulty result of this function, we use Exit Sub to
exit the sub procedure.
Being able to exit a procedure if a specific condition or event occurs saves time and
avoids errors when running a code.
To exit a loop, we use Exit For and Exit Do, depending on if we are currently in a For,
Next or For Each, Next loop or in a Do, While or Do, Until loop. Similar to the above exit
statements, Exit For stops executing a For, Next or For Each, Next loop and moves to
the line of code after the Next statement. The Exit Do code stops executing a Do Loop
and moves to the line of code after the Loop statement.
Again, we will usually use an If, Then statement to check if a certain condition requires
the use of one of these exit statements. An example of using Exit For may be as follows:
Sub Organize()
Set StartCell = Range(“A1”)
12
Chapter 16: Programming Structures
For i = 1 to 100
StartCell.Offset(i, 0).Value = i * (i – 1)
If i * (i – 1) > 50 Then
MsgBox “The calculation values exceed the limit.”
Exit For
End If
Next i
End Sub
Notice that a benefit of having an If, Then statement check to use the Exit For code is
that this reduces the responsibility of setting an exact range for the For, Next loop. In this
example, we may not know at which point the product of i and i – 1 will exceed 50;
therefore we can set the end of the For range as a large number and use the Exit For
statement to end the loop when this limit is exceeded. An example of using the Exit Do
statement in a Do, Until loop may be as follows:
Do
x = x^2
If x mod 5 = 0 Then
MsgBox “The number “ & x & “ is divisible by 5.”
Exit Do
End If
x=x+1
Loop Until x >100
We may now be interested in finding the first squared number divisible by 5. We again
set our Until condition to a large value and exit the loop when the first number that meets
the If, Then condition is found. (Note: The Mod function finds the remainder left after
dividing x and 5. The general structure for this function is: number1 Mod number2.)
These statements can also be used with nested loops. The exit statement applies to the
inner most loop in which it is used. For example:
For i = 1 to 100
For j = 1 to 50
StartCell.Offset(i, j).Value = i + j
If i + j > 60 Then
Exit Do
End If
Next j
Next i
Here, the Exit Do statement ends the inner For, Next loop indexed by j and moves to the
next i value in the outer For, Next loop.
We can stop executing the entire program with the End statement. Just as the End Sub,
End Function, End If, and End With statements end the execution of the enclosed lines
of code, the End statement stops executing all code and exits all loops and procedures.
13
Chapter 16: Programming Structures
End can be a useful function if there is a significant condition or requirement that must
be met before the program can function correctly. For example:
Sub Main()
Call Username
Call FindFile(username)
If filename = “” Then
MsgBox “No file was found for this user.”
End
End If
Call EditRecord
End Sub
In this code, the Username sub procedure is called to prompt the user for a username.
Then, the FindFile function procedure is called to find the filename listed for the given
username in a particular database. If the file is found, then the record for this user can
be edited. However, if no file that matches the username given is found, then there is no
need to continue running the program as this is a necessary requirement for all further
actions.
Name Action
Exit Sub Exits the current sub procedure; the program returns
to the line of code following that one where the sub
Summary procedure was called.
Exit Function Exits the current function procedure; the program
returns to the line of code following that one where
the function procedure was called.
Exit For Exits the current For loop (either For, Next or For
Each, Next) and moves the program to the line of
code after the loop.
Exit Do Exits the current Do loop (either Do, While, or Do,
Until) and moves the program to the line of code after
the loop.
End Exits the entire program; all procedures and loops
are exited.
We discuss error checking and debugging in detail in Chapter 22; however, we want to
mention here a few error checking functions and tips that can be used with basic
programming structures. The intention of these functions is that the program encounters
any errors before the user does. For it is preferable to have already identified an error
and provided the user with a Message Box with clear instructions, rather than have the
user be prompted with a VBA Error message and brought to the VBE Code Window.
Some of these errors that can be detected involve variable data types.
14
Chapter 16: Programming Structures
If a user is prompted to present some input (whether through an Input Box, as described
in this chapter, or through a user form, as described in Chapter 18), this input is usually
assigned to a variable. This variable must have been declared previously with a specific
data type. Therefore, an error can occur if the data that the user provides as input does
not match the data type already assigned to the variable.
One way to avoid this problem is to take advantage of the Variant data type. We suggest
using a temporary Variant data type variable to assign to any input that is provided by
the user. You can then employ the conversion functions described above to ensure that
this variable has the data type needed for subsequent calculations. Below is an example
of this technique:
x = CInt(TempInput)
However, there is still a possible error here; if the user supplies a string expression
without any numbers (for example, “two”), the CInt function will not work as expected.
Therefore, we suggest another VBA function to check if a variable is a number or not:
IsNumeric. This function returns the value True or False depending on whether or not
the variable or expression is a numeric data type. For example:
IsNumeric(1234) = True
IsNumeric(“two”) = False
We can therefore modify the previous code to strengthen the error checking, as follows:
As mentioned before in the conversion functions, CDate can only be used on values that
can be converted to dates. To determine whether or not a variable value or expression
meets the date criteria, we can use a VBA function called IsDate. The result of this
function is again either True or False. Some examples of valid and invalid dates are:
IsDate(“tomorrow”) = False
You could use this function with an If, Then statement, as follows:
15
Chapter 16: Programming Structures
16.6 Applications
We will now develop one application that uses several of the programming structures
discussed above.
The program that we will be working with stores deposits, withdrawals, and the current
balance of a user’s banking account. Users can record a new deposit or new withdrawal,
or they can sum current deposits or withdrawals. Each time a new deposit or withdrawal
is made, the balance is updated. We want to begin enforcing a minimum balance
requirement of $100 for this account.
There are only two worksheets for this program: the “Account Welcome” sheet and the
“Account” sheet (see Figures 16.1 and 16.2). Like any welcome sheet, the “Account
Welcome” sheet has a program title and description. We have also added a Start button;
this button will be associated with a Main sub procedure, as we recommended in
Chapter 15.
Figure 16.1 The “Account Welcome” sheet contains a program description and the
Start button.
16
Chapter 16: Programming Structures
The “Account” sheet is where the program actions occur. The record of account
transactions is displayed with the date and description of each transaction. Withdrawals
are shown as negative values in the “Amount” column and deposits are positive entries.
The balance is maintained in the “Balance” column. Each button is associated with the
action stated in the button caption. The Sum buttons calculate their respective sums and
place them in the cells below each button. The Exit button, as in all of our applications,
brings the user back to the welcome sheet.
Figure 16.2 The “Account” sheet contains the account record and all program button
options.
However, the first, and most important organizational sub procedure is the Main sub
procedure. This macro is associated with the Start button on the welcome sheet. So,
17
Chapter 16: Programming Structures
let’s clear previous sum values from their respective ranges, set any range variables,
initialize any other variables if needed, hide the welcome sheet, and bring the user to the
“Account” sheet. We will also need to assign a short ExitAccount procedure to the Exit
button.
Before we begin typing these procedures, let’s create all of the variables we currently
know we need. We need a deposit and withdrawal variable to insert the new values
and update the balance. In order to perform error checking, we use a value variable
(declared as a variant data type) to receive user input before assigning values to the
deposit and withdrawal variables. Additionally, we include sumDep and sumWith
variables to calculate the respective sums. And we introduce two range variables to
identify the “Amount” and “Balance” columns: AccountStart and BalanceStart. Later,
we will use an integer counter i for our loops and a response variable (declared as a
variant data type) in a MsgBox function.
Let’s again create the Workbook_Open event procedure; however, this time we will
improve our code by using a worksheet variable, ws, and a For Each, Next loop:
In this loop, we examine each worksheet object in the Active Workbook and check its
name property. Only if the name is equal to “Account Welcome” do we set the Visible
property to True; otherwise the Visible property of all the other worksheets will be set to
False. (In this case, we only have two worksheets, but for future programs, this new
structure will greatly decrease the code’s size for this procedure.) Note that we have left
the Private declaration for this procedure now that we understand its implications.
We define all previously discussed variables, including the worksheet variable, as public
variables:
Let’s begin by creating the Main sub procedure. In preparation for coding, we name the
two ranges in which the sum values are calculated: “DepositSum” and “WithdrawalSum”
respectively. This step makes it easier to clear the values of these cells and place the
new values when needed. The code for the Main sub procedure is:
18
Chapter 16: Programming Structures
Sub Main()
Range("DepositSum").ClearContents
Range("WithdrawalSum").ClearContents
sumDep = 0
sumWith = 0
Worksheets("Account").Visible = True
Worksheets("Account Welcome").Visible = False
Worksheets("Account").Activate
End Sub
The first two lines of code clear the sum ranges. The next two lines set the values for the
range variables of the table. (See StartCell functionality of range variables in Chapter
14.) The next two lines of code initialize the sum variable values as they will be
incrementally modified later using a loop. The last lines of code for this procedure are
simply navigational; they hide the welcome sheet and display the “Account” sheet. We
can now simply state the navigational procedure for the Exit button:
Sub ExitAccount()
Worksheets("Account Welcome").Visible = True
Worksheets("Account").Visible = False
End Sub
Now let’s create the sub procedures for the Sum buttons. For each button, we look
through every entry of the “Amount” column until we reach the end of the column using a
Do, Until loop. For each entry, we check if the value is a withdrawal or deposit and
update our respective sum variables using If, Then statements. We opt for the counter
variable i for the loop; note that with the Do Loops, we must increment the counter in the
loop (unlike in the For Loops). The procedures are:
Sub SumDeposits()
i=1
Do Until AccountStart.Offset(i, 0).Value = ""
If AccountStart.Offset(i, 0).Value > 0 Then
sumDep = sumDep + AccountStart.Offset(i, 0).Value
End If
i=i+1
Loop
Range("DepositSum").Value = sumDep
End Sub
19
Chapter 16: Programming Structures
Sub SumWithdrawals()
i=1
Do Until AccountStart.Offset(i, 0).Value = ""
If AccountStart.Offset(i, 0).Value < 0 Then
sumWith = sumWith + AccountStart.Offset(i, 0).Value
End If
i=i+1
Loop
Range("WithdrawalSum").Value = sumWith
End Sub
To search through each entry in the “Amount” column, we use the AccountStart range
variable and the Offset property: AccountStart.Offset(i, 0). The loop continues until no
entry is found: Do Until AccountStart.Offset(i, 0).Value = “”. Then, for deposits, we only
want to sum positive values; for withdrawals, we only want to sum negative values. So,
we use an If, Then statement to check if the current entry is greater or less than 0. If the
condition is satisfied, we update the sum variable by incrementing it by the current entry
value. When the loop ends, we place the final sum value in the appropriately named
range.
Now let’s work with the NewDeposit sub procedure. The first line of code asks the user
for the amount of the new deposit. Temporarily, we set the value variable equal to our
InputBox function so we can perform error checking on the input.
Sub NewDeposit()
value = InputBox("Please enter amount to deposit.", "New Deposit", 150)
To perform the error checking, we use the IsNumeric function with the value variable to
determine if the user enters a numerical data type. If so, then we can continue running
the sub procedure. If not, then we cannot continue with the procedure actions. We
therefore inform the user of an error using a simple Message Box and then use Exit Sub.
We perform the error checking with an If, Then statement:
If the user provides a numeric value, then we assign this value to the deposit variable.
(Observe that it was necessary to use the value variable, which is a variant data type, to
avoid an immediate error. In other words, if we assign the deposit variable, a double
data type, to the InputBox function, and the user does not enter a numeric value, or even
a double data type, then an immediate VBA error will appear. We want to prevent the
user from seeing any errors by VBA.)
20
Chapter 16: Programming Structures
We now insert this value into the “Amount” column, using the AccountStart range
variable and the End property. We also format the table’s new row by changing the
background color of the appropriate cells:
With AccountStart.End(xlDown).Offset(1, 0)
.value = deposit
.Interior.ColorIndex = 0
End With
Range(AccountStart.End(xlDown).Offset(0,-3), _
AccountStart.End(xlDown).Offset(0, -2)).Interior.ColorIndex = 0
BalanceStart.End(xlDown).Offset(1, 0).Interior.ColorIndex = 0
The only action left to perform is to update the balance by calling the UpdateBalance
function. (We discuss this in more detail below.) After those actions are complete, we
can simply inform the user to enter a date and description for the new entry with a
Message Box:
With the MsgBox function and response variable, we can add an additional option to
continue adding more new deposits. We prompt the user if he or she wants to enter
another deposit and set the Message Box buttons to vbYesNo. The MsgBox function is
assigned to the response variable, so we can now check the value of the response
variable and perform the related actions. We use another If, Then statement to ascertain
if the response is “Yes” (which is equal to the VB Constant vbYes), and, if it is, call the
NewDeposit sub procedure again. Otherwise, we simply end the sub:
The NewWithdrawal procedure is quite similar. We do not provide the option of creating
a new withdrawal here, but we can add it later. The only main changes are that we use
the withdrawal variable instead of deposit and enter the value in the table as a negative
number:
Sub NewWithdrawal()
value = InputBox("Please enter amount to withdraw.", "New withdrawal", 150)
'error checking
If IsNumeric(value) = False Then
MsgBox "You have not entered a numerical value. Please try again."
Exit Sub
End If
21
Chapter 16: Programming Structures
withdrawal = value
'update balance
Call UpdateBalance(withdrawal, "W")
End Sub
Let’s now discuss the UpdateBalance function, which has two parameters: the
deposit/withdrawal value and a simple letter signifying deposit with “D” and withdrawal
with “W.” Notice that these variables are passed when the function is called from the
above two procedures; the function declaration refers to these two parameters simply as
x and y. We can then use a Select, Case statement to check the value of the y
parameter. In the case that it is “D,” we add the deposit value to the last entry of the
“Balance” column. In the case that it is “W,” we subtract the withdrawal value from the
last entry of the “Balance” column. We must also perform one more check when a
withdrawal is made; we must ensure that the $100 balance requirement is still met. We
use an If, Then statement to check the result of this calculation. If the withdrawal results
in a balance less than $100, we do not perform the transaction. The user is then
informed that his or her balance is too low, the initial entry to the “Amount” column is
cleared, and the function is exited using the Exit Function statement. However, if the
requirement is still met, then we update the balance and tell the user that he or she can
enter the date and description for the withdrawal. The final procedure is as follows:
Function UpdateBalance(x, y)
Select Case y
Case "D"
BalanceStart.End(xlDown).Offset(1,0).value = _
BalanceStart.End(xlDown).value + x
Case "W"
If BalanceStart.End(xlDown).value - x < 100 Then
MsgBox "This withdrawal cannot be made due to the $100 “ & _
& “balance requirement."
AccountStart.End(xlDown).ClearContents
Exit Function
End If
BalanceStart.End(xlDown).Offset(1,0).value = _
BalanceStart.End(xlDown).value - x
MsgBox "You may now enter the date and description of your “ & _
& “withdrawal into the table."
End Select
End Function
22
Chapter 16: Programming Structures
The procedures are now complete. We assign them to their respective buttons and test
the macro. The application is finished.
16.7 Summary
¾ If, Then statements allow you to perform conditional actions. If a condition is met,
a certain set of actions is performed; if it is not, another set of actions may be
performed instead. The If, Then statement extends defines the action to be
performed if the condition is false with the Else and ElseIf statements. The ElseIf
statement allows you to construct nested If statements.
¾ If, Then statements are also used with logical checks and Boolean variables.
Logical checks include And and Or statements. The And logical statement
requires every condition in the If, Then statement to be true in order for the
proceeding action to be performed. If only one of the conditions is false, the
action will not be performed and the following Else or ElseIf statement will be
read, or the If, Then statement will end. The Or logical statement requires only
one condition in the If, Then statement to be true in order for the proceeding
action to be performed. Every condition would have to be false to skip it and read
the Else, ElseIf, or End If statements.
¾ Loops are programming structures that allow you to repeat a set of actions a
certain number of times. You can use the For, Next and For Each, Next loops to
repeat a loop while counting up to or down to a certain number. The Step
parameter used with this loop specifies how much the counter variable, in this
case i, should increase during each loop.
¾ The For Each, Next loop works almost identically to the For, Next loop. The only
difference is that For Each, Next counts a certain number of objects in a group of
objects; that is objects are counted rather than using a counter variable.
¾ There are two main Do Loops: Do, While and Do, Until. Theyperform a set of
actions repeatedly while or until a condition is met. For the Do, While loop, if the
condition is true, the loop repeats the actions. However, for the Do, Until loop, if
the condition is true, the loop stops repeating the actions.
¾ Do Loops can also be used with Boolean variables. The Boolean variable can be
used as the condition for the Do, While or Do, Until loops. These conditions
usually imply some nested If, Then statements that change the value of the
Boolean variable once a certain result is found.
¾ Use the Select, Case statement to list possible situations in which certain actions
should be performed. The general structure of the Select, Case statement
presents a particular expression that is to be evaluated as well as a list of cases
of that expression’s possible values. There is also an optional Case Else
statement that specifies all other unlisted cases.
¾ The ability to give a range of values as a case instance using the To statement
can be extended to string values. You can place a condition on a range of values
using the Is statement.
¾ VBA provides several Exit Statements that allow current actions to stop and
moves the program to ensuing code. We usually use these statements with the
If, Then structure.
¾ To exit a procedure, use either Exit Sub or Exit Function, depending on if you are
running a sub procedure or function procedure, respectively. The program then
returns to the line of code following that where the sub procedure was called.
23
Chapter 16: Programming Structures
¾ To exit a loop, use Exit For and Exit Do, depending on if you are in a For, Next or
For Each, Next loop or in a Do, While or Do, Until loop. These statements then
move the program to the line of code after the loop.
¾ To stop executing an entire program, use the End statement. Just as the End
Sub, End Function, End If, and End With statements end the execution of the
enclosed lines of code, the End statement stops executing all code and exits all
loops and procedures.
16.8 Exercises
16.8.1 Review Questions
10. What will the output of this program be, given the following table?
Product
1
2
1
2
2
2
1
3
3
24
Chapter 16: Programming Structures
1
2
3
1
3
3
1
1. Write a sub that asks a user to input a string with exactly three letters followed by
three integers or vise versa three integers followed by three letters. Prompt the user
to input a string until the entered string contains exactly three letters followed by
three integers or vise versa.
2. A number is prime if and only if the number is divisible by no other number than itself
and one. Write a function called DetermineIfPrime that determines whether a number
is prime. Write a sub called Main that determines whether or not the number in each
cell in a selected range is prime. Apply a For Each loop and use the function for each
cell. If a number is prime, change the background color of the cell and bold the cell.
Try to optimize the running time of your program by limiting the number of iterations
in a loop that are required to determine if a number is prime.
3. The Least Common Multiple (LCM) of two positive integers a and b is the smallest
positive integer that is divisible by both a and b. The Greatest Common Divisor
(GCD) of two positive integers a and b is the largest integer c that divides both a and
b. It may help to know that for two positive integers a and b: ab = LCM(a,b) *
GCD(a,b).
a. Write a function called CalculateLCM that calculates the LCM of two
positive integers.
25
Chapter 16: Programming Structures
4. Write a logic loop that assigns grade letters to the students in the following table
based on the grade scale of 90-100 = A, 80-89 = B, 70-79 = C, 60-69 = D, 0-59 = F:
(Note: assume each Exam is weighted the same.)
Using the same grade sheet as the above example, create a logic loop that
increases the grade of the first exam by 5 points and then assigns a final grade
based on 40% of Exam1 grade and 60% of Exam2 grade.
5. Write a program that evaluates the sales information below. It should also print a
sales report that displays a total of all units sold of each product as well as a
Message Box that reports the final sales figure for the given data. The pricing
conditions are as follows:
Pricing Matrix
QS950 $30.25
RS220 $20.15
MR130 $25.50
TW680 $33.10
The discount matrix provides the percent discount given to customers based on their
order quantity.
Discount Matrix
1-3 0%
4-7 5%
8-14 7%
15 or more 10%
Using this pricing information, determine the total amount sold, based on the sales
data below:
26
Chapter 16: Programming Structures
6. Write a program that plays the game “Paper-Rock-Scissors” with the user. The rules for
the game are that the user will be prompted to choose paper, rock or scissors. The
computer will then randomly generate a value. Values less than .3 = paper, between .3
and .6 = rock, and above .6 = scissors. If the user’s choice matches the computer’s
choice, the user is a winner.
7. Write a program that converts military time into the 12-hour time equivalent. For
example, if the user enters 2045 into the InputBox when prompted for the time, the
MessageBox should appear informing the user that the time is 8:45 PM.
27
Chapter 16: Programming Structures
attached to the existing values in the spreadsheet. (Hint: use the End
property and the necessary error checking.)
10. A dating program has a list of users stored with personal information (see the table
below). Each user has a username and a password. Create a button that enables the
users to sign-in. The new users should be prompted to enter the following information in
the database: username, password, age, etc. Returning users should enter their
username and password to access the data. If the username and the password entered
are not found in the database, the user should be prompted to try again. The existing
users should be able to edit their information. For this purpose use InputBox. Set the
default value of the InputBox to the current user’s information.
Education
Username Password Age Gender Single/Married Level
JDoe marbles 24 M Single BS
MSue kitty23 23 F Single HS
11. Create a procedure that generates a random month and day using a Select, Case
statement. The user should be prompted on how many dates he or she wants to
generate. Repeat the procedure this many times. Then check if any of the dates are
equal.
28
Chapter 16: Programming Structures
14. Card guessing game: Given an Ace, and two Kings, randomly shuffle them so that the
user can guess which card is the Ace.
a. Name three ranges on the spreadsheet for the three different positions.
b. Create three shapes (or use three images) for the Ace, King1, and King2.
c. Show the user all three card values, then fill the shapes a black color to hide their
values.
d. Prompt the user how many times he or she wants to shuffle the cards. Then call
a shuffling procedure this many times. The shuffling procedure should randomly
assign a position (from the three available positions) to each card. Ensure that no
two cards are assigned to the same position.
e. When the shuffling ends, ask the user which card he or she thinks is the Ace
(first, second, or third).
f. Reveal the card values (change fill black to white or none) and tell the use if he
or she won or lost.
15. Create a quality control program in which you are checking to see which, if any, items
from a production line should be rejected.
a. Prompt the user for the number of items produced.
b. Ask the user which distribution the production follows: Normal or Exponential.
c. Based on the answer, ask the user for the corresponding parameter values and
generate the given number of item values (for example, diameter sizes of x
widgets).
d. Find the mean and standard deviation of these values. Graph the data with a
mean line and +/- standard deviation lines.
e. Then review all of the data points and highlight the items that are above or below
the standard deviation. Count these items and display this value to the user.
16. Create a sub procedure that determines if a number is even or odd. The procedure
prompts the user to enter the number. Use the If statement to ensure that a numerical
value is entered. If the value entered is not numerical, prompt the user if s/he wants to
enter a new value.
17. Create a sub procedure that sorts a list of numbers. Prompt the user to enter the range
of numbers to sort. Use the Do While loop and If statement in your visual basic code. Do
not use the sorting function of Excel.
18. Create a sub procedure that prompts the user to enter a year and determines if this is a
leap year.
19. Create a sub procedure that asks the user to enter her/his birthday and returns user’s
age.
20. The following table presents information about flight reservations of an airline company.
Use Do While Loop and If statements to perform the following tasks:
a. Create a sub procedure that adds a new reservation in the table below. The
TktNo should be automatically assigned to each reservation, while the rest
of the information required (CustomerId, FlightNo, etc) should be entered
by the user. Check user’s input for the following errors: the length of
CustomerId should be exactly 4; FlightNr is an integer; Class should either
be Economy, Business or Class; TravelDate should be greater than or
29
Chapter 16: Programming Structures
30
Chapter # 17
Arrays
Chapter Overview
17.1 Introduction
17.2 When and Why to Use Arrays
17.3 Defining Arrays
17.3.1 Multi-Dimensional Arrays
17.3.2 Indexing
17.7 Summary
17.8 Exercises
Chapter 17: Arrays
17.1 Introduction
This chapter explains how to use arrays in VBA. Arrays are useful programming
structures for larger data storage using a single variable name and for manipulation of
several values using one entity. We show the reader how to define, resize, and sort
arrays in VBA. We use these structures in various DSS applications for storing data
values. One example of using arrays is for storing arrival and service times of customers
in the Queuing Simulation application. We store these values in arrays and then loop
through the values to execute the simulation. As arrays are very useful structures, they
will be commonly used in developing DSS applications.
Arrays can be very efficient tools for programming systems with multiple series of data.
However, you should also compare the benefit of using arrays with the option of printing
values to a spreadsheet for storage in a table. As we have seen earlier, in some cases
there is no need to store data in an array variable. However, arrays offer an alternative
that may be quite useful in many situations.
To insert values into this array, we can use the array function. The array function allows
us to list values for each array element. For example, to insert 10 values into the data
array, we type the following:
To set the value of a specific element of the array, we use an index. The default initial
index value is 0; however, to keep in line with our example, let’s assume the initial index
is 1. (We discuss indices in a later section.) For example, if we want to change the third
value in the above data array from 16.5 to 10.5, we type:
data(3) = 10.5
2
Chapter 17: Arrays
To enter multiple element values using indices, we usually use a For, Next loop with a
counter variable. For example, if we want to set each element in the data array as its
index number, we type:
For i = 1 to 10
data(i) = i
Next i
As illustrated in the above example, a single number is all that is necessary to specify
the size of a one-dimensional array. To define multi-dimensional arrays, we need to
specify the size of each dimension, separated by a comma. For example, if we want to
define a two-dimensional array of size 5 by 10 (number of rows by number of columns),
we type:
For i = 1 to 5
For j = 1 to 10
data(i, j) = i*j
Next j
Next i
17.3.2 Indexing
As previously mentioned, the default initial index value of arrays in VBA is 0. However, to
change the initial index value of all arrays in your module to 1, we simply type Option
Base 1 at the top of the module. If we want to keep the default initial index as 0 but have
a specific array start indexing at 1, we can specify the starting index value in the array
declaration as (1 to arraysize). As a comparison, in the example below, we use Option
Base 1.
Option Base 1
Here, both the data and results arrays have an initial index of 1 (as will any other array
that we later define in this module). Below we do not specify the Option Base setting, but
instead, we change the index of one of the arrays:
Now, the data array begins with an index of 1, but the results array begins with an index
of 0. We recommend that one general initial index be chosen for all arrays in the module
3
Chapter 17: Arrays
The results array is still of size12; it is just indexed starting with 2. In this case, we need
to coordinate the initial index value that we chose with the counter variable used in For,
Next loops. That is, if we want to insert a value into each element of the results array
indexed starting with 2, we need to set our initial counter variable equal to 2.
For i = 2 to 13
results(i) = value
Next i
Here, for example, results(1) would cause an error since there is no such index for the
array.
However, before we can use this array or refer to any of its elements, we need to know
its size. To set the size of a dynamic array at some later point in the code, we use the
ReDim statement. It can also be used to set or change the number of dimensions and
the indexing bounds.
Let’s suppose that we want to ask the user to insert some input values, which we will
store in an input array, but we are unsure how many values the user wants to insert. We
first assign a variable to an Input Box that asks for the number of values. Then, we insert
the value of each element using a For, Next loop. The code is as follows:
For i = 1 to size
input(i) = InputBox(“Please enter value for element “ & i )
Next i
Another important command that can be used with the ReDim statement is Preserve.
Using the complete statement ReDim Preserve with an array changes the array size, but
keeps any values that were previously assigned. For example, suppose we have
completed the code shown above in which a user specified the number of elements in
4
Chapter 17: Arrays
the input array and then provided a value for each element. Now, let’s prompt the user to
see if there are more elements he or she wants to add. If so, we want to keep, or
preserve, the array we used previously but increase its size to account for the new
elements. The next set of code is as follows:
We learn that the number of additional elements to add to the array as size2. We can
then increase the original size of the array, which is size, by the number of new
elements: size + size2. We use the Preserve statement in order to retain the values for
the initial elements. We then use a For, Next loop to assign values to only the new set of
elements; that is why we loop from size + 1 to size2. The ReDim Preserve statement is
for a valuable element of dynamic programming.
Option Base 1 Changes the initial index value of all arrays in a module
(from default of 0) to 1; declared at top of module.
Dynamic Arrays Size is not specified; the parentheses are left empty in
array declaration.
ReDim Preserve Used to reset the size of a previously defined array while
maintaining the values of the previously assigned
elements.
5
Chapter 17: Arrays
using the Excel spreadsheet. To perform a simple sort, we can take advantage of
Excel’s spreadsheet sorting function. Suppose we have the following array of size 10:
To sort this array in increasing order using the Excel sort function, we can simply output
this array to a range in the spreadsheet, sort it in this range, and then input it back into
the array. The code for this is as follows. (Here we use a range named “ArrayStart” as
the reference for our output.)
Sub ExcelSort()
For i = 1 to 10
Range(“ArrayStart”).Offset(i, 0).Value = data(i)
Next i
Range(Range(“ArrayStart”).Offset(1,0), Range(“ArrayStart”).Offset(10,0))
.Sort Key1:=Range(“ArrayStart”), Order1:=xlAscending
For i = 1 to 10
data(i) = Range(“ArrayStart”).Offset(i,0).Value
Next i
End Sub
Another way to sort an array is by using the Bubble Sort algorithm, which performs
passes over the elements to be sorted. In each pass, it considers an element, say p, in
order and compares it with the next element, say q. If p > q (and we are trying to sort the
list in ascending order), then the two elements are switched since they are out of order.
A pass ends when each element has been compared with the next element and
switched (if it is out of order). The algorithm terminates when in one complete pass no
two elements are out of order, i.e. when all elements have been sorted.
Let’s consider the data array defined above: (5, 2, 8, 3, 7, 1, 9, 6, 10, 4). In the first pass
of the Bubble Sort algorithm, the element 5 would be selected. We first compare 5 with 2
and find that 5 is larger, so we switch the two elements. We now compare 5 with 8 and
find that 5 is smaller, so we now select 8 and continue our comparisons. We compare 8
with 3 and find that 8 is bigger, so we switch the elements. We then compare 8 with 7
and find that 8 is bigger, so we again switch the elements. We compare 8 with 1 and
again switch the elements. Then, we compare 8 to 9 and find that 9 is bigger, so we now
select 9 and continue our comparisons. We compare 9 with 6 and find that 9 is bigger,
so we switch the elements. We compare 9 with 10, but 10 is bigger, so we now select
10. We compare 10 with 4 and switch the elements since 10 is bigger. We have now
reached the end of the array and have completed the first pass of the algorithm. The
current state of the array is as follows:
We repeat this process until no switches are performed. To code the Bubble Sort
algorithm in VBA, we want to use a Do, While loop. We define a Boolean variable sorted,
which we will use to determine when to stop running the algorithm. We know the array is
sorted when no switches have been made; therefore, we keep track of the number of
switches made in each loop with a counter variable, switches. In order to perform a
6
Chapter 17: Arrays
switch, we use a temporary variable to hold the place of the value being switched. Here
is the code:
Sub BubbleSort()
Dim sorted As Boolean, switches As Integer, temp As Double
For i = 1 To 10
If data(i) > data (i + 1) Then
temp = data (i)
data (i) = data (i + 1)
data (i + 1) = temp
switches = switches + 1
End If
Next i
If switches = 0 Then
sorted = True
End If
Loop
End Sub
Additional Note: Arrays can be used with function procedures as well. We can pass the
name of an array to a function procedure in order to manipulate some or all of its
elements. Also, we can pass elements of an array to a function procedure to manipulate.
If we are trying to create a new array using a function procedure, we cannot return a
value to assign to an array variable. That is, we cannot write the following:
NewArray = ArrayFunction()
ArrayFunction(NewArray)
In this case, the function procedure takes action on the new array variable passed to it.
We would not need to return anything from this function, as the actions taken on the
array would stay associated with the new array variable. It may additionally be useful to
create a function procedure for one of the sorting methods explained above. That is, we
could use Function BubbleSort(ArrayName) to pass an array to the BubbleSort
procedure and sort it. Refer to Chapter 15 for more on function procedures.
17.6 Applications
Let’s now develop a phonebook application that uses dynamic arrays.
7
Chapter 17: Arrays
17.6.1 Phonebook
This application allows a user to search, add new entries to, and view all listings in a
phonebook. The phonebook stores a name and number for each entry.
There are only two worksheets for this program: “Phonebook Welcome” and “Phone
Data.” The “Phonebook Welcome” sheet is the basic welcome sheet with a problem
description. The button options appear on this sheet as well (see Figure 17.1). (Refer to
Hands-On Exercise 9 to add a Start button to this sheet.) The “Phone Data” sheet
contains a table with the name and number of each entry in the phonebook. There is
also a button to return to the “Phonebook Welcome” sheet (see Figure 17.2).
Figure 17.1 This is the welcome sheet for the phonebook application.
8
Chapter 17: Arrays
Figure 17.2 This is the data sheet with all the phonebook entries.
In this case, we only need two main sub procedures. We need a Search procedure to
search for an entry in the phonebook by name and a NewEntry procedure to enter a
new name and number into the phonebook. There will also be two navigational
procedures, one to take the user from the welcome sheet to view all listings and one to
return the user to the welcome sheet or the phonebook menu. In the Search procedure,
we call a CreateArray procedure to put all the names and numbers from the “Phone
Data” sheet into two arrays from which to search.
We also need some counting variables, two array variables (one for the names and one
for the numbers), two range variables (for the two columns of data in the phonebook
table), and two variables to capture the name and number for searching or adding a new
entry. We declare these variables as follows:
Sub ViewBook()
Worksheets("Phone Data").Visible = True
Worksheets("Phonebook Welcome").Visible = False
End Sub
----------------------------------------------------------------------------
Sub ViewMenu()
Worksheets("Phonebook Welcome").Visible = True
Worksheets("Phone Data").Visible = False
End Sub
9
Chapter 17: Arrays
These procedures are assigned to the “View All Listings” and “Return to PhoneBook
Menu” buttons, respectively.
Let’s now discuss the Search sub procedure. The first action is to ask the user for the
name that he or she wants to search for in the phonebook. We assign the NewName
variable to the InputBox function and provide a default value. Next, we call the
CreateArray procedure to create an array of all current names, PhoneName, and
numbers, PhoneNumber, in the phonebook. (We will discuss this procedure later.) This
course of action makes our search easier, since now we can just use a For, Next loop to
check if any of the array elements are equal to the NewName value.
If a match is found, the corresponding element (i.e. the same index number) from the
PhoneNumber array is assigned to the NewNumber variable, and the result of the
search appears. If no match is found, we consider that the NewNumber variable was
never changed from its default value of 0. Therefore, using this check, we can tell the
user that no match was found. The code is as follows:
Sub Search()
NewName = InputBox("Please enter name you wish to search for using “ & _
& “ the following format: Last, First:", "Name Search", "Smith, John")
Call CreateArray
NewNumber = 0
For i = 1 To n
If PhoneName(i) = NewName Then
NewNumber = PhoneNumber(i)
MsgBox "The phone number for " & NewName & " is " & _
Format(NewNumber, "(###) ###-####")
End If
Next i
If NewNumber = 0 Then
MsgBox "There was no phonebook entry by that name."
End If
End Sub
We have used the Format function to display the NewNumber value as a standard 10-
digit phone number. This function performs the same actions as formatting a cell in
Excel. The structure for this function is:
Format(Expression, Format)
The expression is the value to which we want to apply the formatting; in this case, the
value is the NewNumber. The notation "(###) ###-####" specifies the custom format that
we want to apply. Please note that you may need to reference an Object Library in order
to use the Format function. This library can be referenced by choosing Tools >
References > Microsoft Office Object Library, for the latest edition found in your list.
We need the user to enter the phone number as a standard set of 10 digits since
NewNumber is defined as a double data type, and it can be easier to compare a double
10
Chapter 17: Arrays
value than a string. If we had not used the Format function, we may have wanted the
numbers to be defined as a string in order to use the parentheses and dashes. We also
format the cells on the phonebook list to this custom format using Excel format cells
options (on the spreadsheet, not in the code).
We can now discuss the CreateArray procedure. Notice in our variable declarations, the
two arrays, PhoneName and PhoneNumber, were defined as dynamic arrays. Now we
need to find the size of these arrays and use the ReDim statement. To find the size of
the arrays, we can use range variables and the End property to count the number of
values currently in the phonebook table. To count the number of values in this range, we
use the Rows property and the Count sub property.
[Note: If you use the Count property directly on the range instead of as a sub property to
Rows, you will count the total number of cells in the entire range. If there is only one
column or one row of data, this is inconsequential; however, if there are multiple rows or
columns, you will need to ensure what you are counting: the number of rows (using
Rows), number of columns (using Columns), or number of cells (using the range
directly).]
Next, we use a For, Next loop to enter each value in the table as an element in one of
our arrays. The code is:
Sub CreateArray()
With NameStart
n = Range(.Offset(1, 0), .End(xlDown)).Rows.Count
End With
ReDim PhoneName(n)
ReDim PhoneNumber(n)
For i = 1 To n
PhoneName(i) = NameStart.Offset(i, 0)
PhoneNumber(i) = NumStart.Offset(i, 0)
Next i
End Sub
(Note that the range variables are initialized in the Workbook_Open event procedure.
We also use the For Each, Next loop to hide and show necessary sheets. See Chapter
16 on applications.)
The last procedure that we need to create is NewEntry. In this procedure, we begin by
prompting the user for both a name and a number to add to the phonebook. We assign
Input Boxes to the NewName and NewNumber variables. We also adderror checking at
this point. To ensure that a non-empty string is entered as a name, we use an If, Then
statement with the condition: NewName = “”. If this condition is true, then the sub will be
exited and the user will be prompted to re-enter a name. (Another way to create an error
checking statement for the NewNumber variable is to temporarily use a variant variable
before assigning a value to NewNumber; see Chapter 16 on applications).
Using the Len function in VBA, we also add an error checking statement to ensure that
the phone number entered is 10-digits in length. . The Len function takes any string
11
Chapter 17: Arrays
expression or variable name as input and counts the number of characters or digits.
Therefore, to check if the number entered by the user is 10-digits, we can just check if
the Len function when applied to this value returns 10.
We then call the CreateArray procedure again to search all names in the current list and
avoid duplicate entries. If the NewName value is found in the PhoneName array, the
user is told that this entry already exists. If no match is found, the program proceeds by
adding the new entry to the table; we do this using range variables. Now, we select our
modified table and sort all of the values using the Sort method. (Notice that the
NameStart range variable can be used as the Key argument.)
We also use the ScreenUpdating property of the Application object to ensure that the
user does not see this background work on the “Phone Data” sheet. Once the new entry
is added, the user is told that the new name has been added. (Note that we assume
each name in the phonebook is unique.) The code for this procedure is:
Sub NewEntry()
Application.ScreenUpdating = False
Worksheets("Phone Data").Activate
NewName = InputBox("Please enter the new entry name using the “ & _
& “following format: Last, First", "New Name", "Smith, John")
If NewName = "" Then
MsgBox "Please enter a name."
Exit Sub
End If
NewNumber = InputBox("Please enter the 10-digit phone number for " & _
NewName & " using the following format: 1234567890", _
"New Number", 1234567890)
If Len(NewNumber) <> 10 Then
MsgBox "Please enter a 10-digit number."
Exit Sub
End If
Call CreateArray
For i = 1 To n
If PhoneName(i) = NewName Then
MsgBox "There is already an entry for this person in the phonebook."
Exit Sub
End If
Next i
Worksheets("Phonebook Welcome").Activate
Application.ScreenUpdating = True
12
Chapter 17: Arrays
The application is now complete. We assign the procedures to the respective buttons
and check the functionality of the program.
Len(Expression or variable)
Len(NewNumber)
17.7 Summary
¾ Arrays store series of data that can be manipulated or referred to later. The set of
values stored in an array must all be of the same data type. You can refer to the
array as a whole or you can refer to its individual elements by an index value.
¾ To define an array, use the Dim, Private, or Public variable declarations. The data
type will be assigned to all of the elements in that array. VBA will recognize that
the variable is an array and not a scalar variable because you must specify the
size of the array in the variable declaration.
¾ To insert values into an array, use the entire array variable and the array function.
The array function allows you to list values for each array element.
¾ To enter multiple element values using indices, use a For, Next loop with a
counter variable.
¾ For a one-dimensional array, you just need a single number to specify the size.
To define multi-dimensional arrays, you must specify the size of each dimension,
separated by a comma.
¾ To insert values into a multi-dimensional array or to search for a value, use
nested For, Next loops with different counter variables.
13
Chapter 17: Arrays
¾ The default initial index value of arrays in VBA is 0. However, to change the initial
index value of all arrays in your module to 1, simply type Option Base 1 at the top
of the module.
¾ If you want to keep the default initial index as 0 but have a specific array that you
want to index starting at 1, you can specify the starting index value in the array
declaration as (1 to arraysize).
¾ You can start the index at any value; just be aware of the size of your array (size
= upper index bound – lower index bound + 1).
¾ If you are not sure what size an array should be, or will be depending on the user
or other dynamic programming reasons, you can define a dynamic array. When
declaring a dynamic array, you do not specify the size; the parentheses are left
empty.
¾ To set the size of a dynamic array at some later point in the code, use the ReDim
statement. It can also be used to set or change the number of dimensions and the
indexing bounds. The ReDim Preserve statement accomplishes the same
function as the ReDim statement but retains any previously assigned element
values.
¾ Use the Format function to apply a custom format to an expression or variable
value.
¾ The Count property can be applied directly to a range to count the number of cells
in the range. You can also apply it to the Rows property of a range to count the
number of rows or the Columns property to count the number of columns.
¾ The Len function counts the number of characters or digits in a string expression
or variable value.
17.8 Exercises
17.8.1 Review Questions
1. What would happen if you assigned entry i equal to entry j in the same array?
For example, List() is an array of integer values. What does it mean to assign
List(4) = List(10)?
2. What is the difference between defining a variable and defining an array of
variables?
3. Why are multidimensional arrays beneficial? How many elements are in a
multidimensional array?
4. Using Option Base 0, what is the last index of a variable array of size N? Using
Option Base 1?
5. How do you define a public variable array with a size of integer variable N?
6. Is there a way to re-dimension an array without losing the data already in the
array? If so, explain and then discuss any restrictions.
7. How do you find the length of an expression or variable value? Can this function
be used on any variable data type?
14
Chapter 17: Arrays
1. Complete the following code so that the user’s input is used to define the size of
the array:
Sub DefineArraySize()
UserInput = InputBox("Enter the require array size: ")
End Sub
2. Based on the following data, what will the output be for the program below?
Data
9.25
0.72
6.11
4.89
7.5
8.22
5.21
6.92
4.42
3.97
7.68
Sub Review()
'copy and manipulate data to array
With Range("A1")
For i = 1 To 11
DataValue = .Offset(i, 0).Value
Data(i) = Int(DataValue)
Next i
End With
'print out the 5th entry
MsgBox Data(5)
End Sub
3. Write a small program that makes a copy of a defined array variable into another
array variable.
4. Write a program that first reads in the data below into a multi-dimensional array
and then sorts the array entries from smallest to largest based on the
measurement values. (Note: Do not use the sort function for this problem.
15
Chapter 17: Arrays
Instead, think about using a dummy variable to hold one value while copying the
other value from one position to another within the array.)
Sample Measurement
1 25.63
2 25.8
3 36.14
4 30.54
5 55.03
6 19.02
7 20.22
8 61.85
9 45.54
10 48.27
11 88.25
12 17.52
13 14.85
5. Write a visual basic code that calculates A + B and A x B, where A and B are two
dimensional arrays. In writing the code, consider the following:
a. Ask the user if s/he wants to use the data given below to test the code, or
use his/her own data. If the user chooses to read the data from the table,
copy this data in two two-dimensional arrays A and B. Otherwise, ask the
user to enter the size of each array and the corresponding elements.
b. Ask the user whether s/he wants to perform the A + B or A x B operation.
Prompt the user if the size of the arrays is not suitable for the chosen
operation.
2 3 5 7 3 7
A = 5 4 9 and B = 5 3 11
11 9 3 3 6 7
6. Write a program that asks the user to enter 5 numbers and then outputs these
values in reverse order on a spreadsheet.
7. Create a two-dimensional array that stores dates and prices for purchases made
by a user. Search through this array and sum the values of the prices for each
month. Report these prices to the user.
8. Using VBA functions (including the Application object) and arrays, perform the
following:
a. Define an array of size 20
b. Populate the array with a set of random numbers between 1 and 100
c. Find the maximum value of the array.
d. Find the minimum value of the array.
16
Chapter 17: Arrays
e. Create a new array of size 20 and populate it with random numbers from
a normal distribution with mean and standard deviations equal to mean
and standard deviation of the random numbers generated in part (b).
10. Creating subsets. Create an array of size 50 with random numbers from 1 to 20.
Loops through these values to check necessary conditions to find the following
subsets:
a. An array called Odd with only odd numbers.
b. An array called Even with only even numbers.
c. An array called Five with numbers that are multiples of five.
d. An array listing all the numbers that were repeated in the original array.
11. The sales of a product follow a normal distribution with different parameters for
each quarter. Create a program that computes sales figures for five years using
the distribution parameters for each quarter. The program should perform the
following actions:
a. Ask the user to enter the parameters for each quarter using Input Boxes.
b. Store the parameters in two-dimensional array, “parameters.”
c. Create a two-dimensional array, “sales,” that stores the sales of each
quarter for five years.
d. Use nested For loops and the “parameters” array to compute random
sales values in the “sales” array.
e. Use nested For loops and the “sales” array to list all sales values in order
on a worksheet in two columns. The first column contains the year and
quarter, such as “2003 Qtr 1,” and the second column contains the sales
values.
f. Display a line chart with the sales information.
12. Create an array named Birthdays() that contains randomly generated birthdays.
Present the number of pairs of matching birthdays in the array. The following
steps should be considered in building the visual basic code:
a. Prompt the user to enter the number of birthdays to generate and re-
dimension the Birthdays() array.
b. Prompt the user to enter a range of years within which the birthdays
should be created. Verify that the latter year is greater than the former
year. If not, inform the user that the latter year must be greater and repeat
part (b).
c. Randomly generate birthdays, assuming thirty days in a month. Note: use
the DateSerial() function in visual basic.
d. Compare and counts the number of pairs of matching birthdays. Note: if
there are three matching birthdays, then they should count for three pairs
of birthdays that match.
e. Displays the number of pairs of matching birthdays.
17
Chapter 17: Arrays
14. Playing the lottery. There are several different levels in winning the lottery. If your
ticket matches every number of the lottery ticket, you win the grand prize. If your
ticket has the same numbers, but in a different order, you win the second prize. If
your ticket has half of the matching numbers in the correct order, you win the
third prize. Create a program in which the winner can play the lottery and see
what he won.
a. Randomly generate each number of the lottery ticket between 1 and 10.
Store the lottery numbers in an array of size 10 called LotteryTicket.
b. Ask the user if he wants to buy a ticket with random numbers or if he
wants to choose his own. If he wants a random ticket, repeat part (a) in
an array called “UserTicket.” If he wants to choose his own, then prompt
him for ten different values and put them in the “UserTicket” array.
c. Check for each of the three winning conditions above and tell the user
what, if anything, he has won. Ask the user if he wants to play again.
15. Task manager: Create a program which allows a user to store and maintain a list
of tasks with a corresponding description and due date. Create the following sub
procedures and assign each to a command button.
a. Reset: Create an excel spreadsheet that presents names, description and
due date of five tasks
b. Create array: Copy the information stored in the excel spreadsheet to a
two dimensional array, called Tasks.
c. Add a new task: Prompt the user to enter the name, description, and due
date of a new task. Check to ensure this task was not previously in the
list. Update the spreadsheet and Tasks array.
d. Delete a completed task: Prompt the user for the name of the task, find it
in the Tasks array, delete it and update the array and the excel
spreadsheet.
e. List the tasks that are completed: Sort the Tasks array by the due date.
Use a message box to present the tasks that have already been
completed. When sorting the tasks, do not use the excel function.
18
Chapter 17: Arrays
a. Update the arrays as entries are added. In other words, new entries
should not only be added to the spreadsheet but also inserted into the
array.
b. Add error checking to ensure that avalue has been entered for the
NewNumber Input Box. (Hint: Refer to the application in Chapter 16.)
c. Create a button and sub procedure to Delete an entry. This should be
similar to the Search procedure. Make sure you update your arrays.
17. Help Herman, a vacuum salesman, to determine which route is best for him if
hevisits 10 cities.
a. Create a two dimensional array called “Distance” that stores random
distances between cities i and j. (Ensure that the distance between a city
and itself is 0.)
b. Create an array called “Order” that stores the order in which the cities
should be visited. (It can store either the city names or numbers.)
c. Create an array called “Visited” that stores Boolean values to determine if
a city has already been visited. (Herman does not wish to visit any city
twice.)
d. Create a For, Next loop that begins at city i = 1 and check the distances
from this city to the other cities in order to find the city with the minimum
distance. (Ensure that you do not check the distance to a city that has
already been visited. Also make sure that you do not check the distance
to the city itself, as that would be 0 and a false minimum.)
e. Place this city in the “Order” array and update its “Visited” array value to
True.
f. Continue this process and keep track of the total distance traveled.
g. Report to Herman the order in which he should visit the cities and the
total distance he can expect to travel.
18. Write a visual basic code that calculates: average, standard deviation, min and
maximum of a range of data. Build the program following these steps:
a. Prompt the user for the starting and ending cell of the range where the
data is saved.
b. Copy the data into an one dimension array called Data().
c. Calculate the average, standard deviation, min and maximum of the data
saved in the array. Do not use the excel functions. Use For Next loops
and If statements to calculate these statistics.
d. Use message box to present the results to the user.
19. The Northwest corner rule for the transportation problem. The transportation
problem is concerned with finding the minimum-cost distribution plan for shipping
products from plants to warehouses. We will describe the steps to be followed in
order to find a feasible solution to this problem.
The data required for this problem is: the unit transportation costs from each
plant to each warehouse, the available capacity at each plant and the demand at
each warehouse. The following table presents the unit transportation costs as
well as demand in each warehouse and available capacity in each plant for a
transportation problem with four plants and five warehouses.
W1 W2 W3 W4 W5 Capacity
19
Chapter 17: Arrays
Plant 1 5 1 5 7 4 150
Plant 2 9 7 8 3 5 200
Plant 3 3 4 3 8 6 175
Plant 4 4 5 6 2 7 100
Demand 80 110 150 100 150
a. Prompt the user whether s/he wants to solve the transportation problem using
the data given in the table above, or use her/his own data. If the user
chooses to use the data given in this problem, read the data from excel
spreadsheet to a two dimension array called Data(). Otherwise, prompt the
user to enter the number of warehouses, the number of plants. Use this
information to re-dimension the array Data(). Prompt the user to enter the unit
transportation costs, demands and available capacities.
b. A feasible solution to the transportation problem is found using this simple
heuristic: Start with the upper-left corner of the cost matrix corresponding to
Plant 1 and W 1. Ship as much as possible from the plant to the warehouse
(Min(Demand W1, Available Capacity Plant 1)). Reduce the demand at W1
and available capacity at Plant 1 by the amount shipped. If the available
capacity at W1 is zero and demand at Plant 1 is positive, move to W2. If the
available capacity at W1 is positive and demand at Plant 1 is zero, move to
Plant 2. Follow these steps until the demand in each warehouse is satisfied.
c. Create a two dimensional array that presents the amount shipped from the
plants to the warehouses. Print this array in an Excel spreadsheet. Print also
the remaining demand and available capacity.
d. Calculate the total transportation costs.
20. Improve the Northwest corner heuristic for the transportation problem described
on hands-on exercise 17.19 updating part (b) as follows: Start with the
combination (Plant, W) that has the minimum unit transportation cost. Ship as
much as possible from the plant to the warehouse (Min(Demand of Warehouse,
Available Capacity at Plant)). Reduce the demand at Warehouse and available
capacity at Plant by the amount shipped. If demand of the Warehouse is positive,
look for the next plant that has available capacity and has the best unit costs.
Continue this way until the demand in each warehouse is satisfied. Calculate the
total transportation cost. Compare this cost to the one calculated in hands-on
exercise 17.19.
20
Chapter 18
User Interface
Chapter Overview
18.1 Introduction
18.2 User Form Controls
18.2.1 Labels and Textboxes
18.2.2 Combo Boxes and List Boxes
18.2.3 Check Boxes, Option Buttons and Toggle Buttons
18.2.4 Command Buttons
18.2.5 Tab Strips and Multipage
18.2.6 Scroll Bar and Spin Buttons
18.2.7 Images and RefEdit
18.10 Applications
18.10.1 Real Estate Search Spreadsheet Application
18.10.2 Product Search Form
18.11 Summary
18.12 Exercises
Chapter 18: User Interface
18.1 Introduction
This chapter explains the user interface development in VBA. This interface includes
user forms, controls on the spreadsheet, and overall spreadsheet appearance. The user
interface is a very important part of a DSS. It is what guides the user through the
application and allows the user to communicate with the program. Good graphical user
interface (GUI) design principles are discussed in detail in Chapter 24. We have
developed a variety of user interface in all of our DSS applications.
To add a user form to your program, simply use Insert > User Form from the menu in the
VBE (you can also use the insert icon as seen when inserting modules). You will then
see a blank form and the Control Toolbox (as we saw earlier in Chapter 12). This time,
however, we will use the Control Toolbox to add necessary items to the user form (see
Figure 18.1). Each of these items is described in the following sections (see Figure 18.2
for a demonstration of how each of these items appears on the user form).
Figure 18.1 A new user form has been inserted. The Control Toolbox provides
available items which can be placed on the form.
After the design of the form is complete, we will need to create some code associated
with the user form which can capture the actions taken by the user. For example, if they
have checked a check box, we want to know which one and how that affects the next
actions of our program. We will use the Properties of these form items to name each
item and understand the values we can assign to them. By using public variables, we will
then be able to transfer actions performed on the user form to the main code of our
program.
2
Chapter 18: User Interface
Figure 18.2 Each of the possible user form items is labeled on this form.
The most important property which we will use for all user form items is the Name
property. The name of an item from a user form is treated as an object in the code. We
recommend that the beginning of your user form item name should identify which type of
item it is. User forms will also have name property values. For example, the name of a
form should begin with “frm” followed by some descriptive name. For example, a form
which gives the user some basic menu options might be called “frmMenu”. (Note that the
Name property can be set in the code for the form as we have seen with other Excel
objects. However, we usually recommend using the Properties window to enter the
name without coding.)
If you are writing code associated with the user form which contains the object you want
to manipulate, you only have to enter the name of the object directly and then use any
property or method available. To manipulate a user form object while in another user
form code or in any other module, just type the name of the form first followed by a
period and the name of the object: for example, frmMenu.lblQuantity.
To view the code of your current user form or to view the form design of your current
form code, use the View Code and View Object buttons, respectively (see Figure 18.3).
Figure 18.3 The View Code and View Object buttons allow you to view the code and
form design of the selected user form.
3
Chapter 18: User Interface
User form: the form which contains any item or control which receives user
input
Control Toolbox: set of all controls available to insert on the user form
Summary Properties Window: view properties associated with any selected control
Name property: used to identify controls in the code
Labels and textboxes will be the most commonly used form items.
A label is used to give a description to any item on the form (as used in Figure 18.2).
You can also use labels to give the form a general description or specify instructions to
the user. (Note that if you want to enter a new line of text in a label, press Shift+Enter.)
To insert a label on your form, use the icon from the Control Toolbox.
The name of a label should begin with “lbl” followed by some descriptive name. For
example, a label for a textbox which takes the value of some quantity might be
“lblQuantity”. (Note that we usually do not name labels unless we intend to manipulate
them later.)
The only manipulation of labels that we use in developing applications is with the
Caption property and the Visible property. The Caption property is used to change the
text displayed in the label. The Visible property takes True/False values and can be used
with all other user form controls as well. You can set the Visible property in the code for
the form; for example, to hide the “lblQuantity” label, you would type:
lblQuantity.Visible = False
A textbox is used to allow the user to enter some value. This value may be a string or
number; the label next to the textbox should specify the kind of value the user should
enter. Note than any non-numerical value entered by the user will be considered as a
string. Therefore, if you want the user to enter a date or other data type which you may
use later in your code, you may need to take advantage of the conversion functions
discussed in Chapter 14 (such as CDate). Textboxes are usually accompanied by a
label control to describe what value should be entered into the textbox.
To insert a textbox, use the icon from the Control Toolbox. The textbox should
appear as seen in Figure 18.4.
4
Chapter 18: User Interface
The name given to a textbox in the Properties window should begin with “txt” and be
followed by some descriptive name. For example, a textbox which will hold a quantity
value may be named “txtQuantity”.
We usually want to assign the value of a textbox object to some variable in our code; we
do this using the Value property. For example, if there was a quantity variable in our
code to which we wanted to assign the “txtQuantity” textbox value, we would type:
quantity = txtQuantity.Value
Combo boxes and list boxes are used to give the user a list of options from which they
can select one item. The main difference between combo boxes and list boxes is the
way in which the data is displayed.
A combo box will list the first entry next to a drop-down button. When the button is
pressed, all other items in the list are shown. The user can also enter a value into the
combo box as with a textbox if they do not wish to select an item from the list. To insert a
combo box in your form, use the icon from the Control Toolbox. The combo box
should appear as shown in Figure 18.5 (note that we also have a label control in this
figure).
5
Chapter 18: User Interface
The name given to a combo box should begin with “cmb” followed by a descriptive
name. For example, if we had a combo box which listed some items, it could be called
“cmbItems”.
There are several important properties associated with combo boxes. The main property
we use to capture the user’s selection is the Value property. Similar to textboxes, this
value can be assigned to some variable in our code. For example, if we have the
variable useritem which is associated with the user’s selection from the combo box of
items called “cmbItems”, we could use the following code to assign a value to our
variable:
useritem = cmbItems.Value
To specify the values to be listed in the combo box, we use the RowSource property.
This property can have a value equal to a specified range in a worksheet or a range
name (both should be entered as a string). For example, if the range A5:B10 has five
rows and two columns of data which we want to show in the combo box, we would set
the RowSource property as follows:
Worksheets(“Sheet1”).Range(“A5:B10”).Name = “Options”
cmbItmes.RowSource = “Options”
Related to the RowSource property is the ControlSource property. This property allows
you to output directly to the spreadsheet the selection made by the user. The value of
the ControlSource property must be a range name, just as with the RowSource property.
For example, to output the selection of the combo box cmbItems to the cell A20, we
would set the ControlSource property as follows:
cmbItems.ControlSource = “A20”
The combo box will be initialized to show the value in the ControlSource range and the
final value selected by the user will be displayed in this range. Even though this is a nice
feature, we usually assign the value of the combo box to a variable and therefore do not
use this feature often.
If the RowSource of a combo box has more than one column, several other properties
can be used. The first is the BoundColumn property. This property determines which
column of the multicolumn data will contain the value which will be captured with the
Value property. That is, for the above example, if we set the BoundColumn to 1, then
regardless of what row of data is selected, only the data from column A will be stored in
the Value property. (Note: the columns are indexed beginning with 1.) We would type the
following:
cmbItems.BoundColumn = 1
6
Chapter 18: User Interface
We may, however, want to show non-adjacent columns in the combo box. In our
example, the data in column A may have some ID numbers for items for sale while
column B may have item names; in this case, it may be more useful to only display the
item names to the user. We still have to set the ColumnCount property to 2 in order to
make both columns of data available; however, we use another property to hide the first
column: this is the ColumnWidths property. If we set the column width of column A to 0,
and set column B to some non-zero width value, then only column B will be displayed.
The column widths are entered in quotation marks for all columns; the width values can
be entered using a VBA point system or with any specified unit of measure. We would
type the following:
cmbItems.ColumnCount = 2
cmbItems.ColumnWidths = “0”, “1”
Another useful property is the ColumnHeads property. This property can be set to True
if there are column headings in the data which you want to display in the combo box.
The row which contains the column headings does not need to be included in the
RowSource data. These column headings cannot be selected by the user.
There are some formatting properties specific to combo boxes such as ListRows and
Style. ListRows is used to specify the number of rows that should appear in the combo
box. If there are more rows of data then are set by the ListRows property, a scroll bar will
automatically appear in the combo box. The Style property has two main options:
fmStyleDropDownCombo and fmStyleDropDownList. The Combo option allows the
user to type a value into the combo box, and the List option does not.
Two other useful properties of the combo box are ListIndex and List. The ListIndex
property will return the index value of the item that was selected (the index for combo
boxes begins at 1, the index for list boxes begins at 0). The List property can be used
with the index of an item to select a particular item from the list. For example, to initialize
a combo box to show the first item in the list to be selected, you would type:
cmbItems.Value = cmbQuantity.List(1)
7
Chapter 18: User Interface
A list box has basically the same functionality and several similar properties as a combo
box. A list box will list all items to be selected in a single box (that is, there is no drop-
down button as with combo boxes). The user cannot, therefore, enter a value into the list
box. To insert a list box in your form, use the icon from the Control Toolbox. The
lsit box should appear as shown in Figure 18.6 (note that a label control is also shown
with the list box).
The name given to a list box should begin with “lst” followed by a descriptive name. For
example, if we had a list box which listed some items, it could be called “lstItems”.
8
Chapter 18: User Interface
In addition to these properties, the list box has a unique property called MultiSelect. The
MultiSelect property determines if a user can select more than one item from the list box.
There are three possible values for this property: fmMultiSelectSingle,
fmMultiSelectMulti, and fmMultiSelectExtended. The Single option is the default and
allows the user to select only one item at a time. The Multi option allows the user to
select more than one item by clicking on several from the list. The Extended option
allows the user to select a section of items from the list box by clicking the first item in
the section they want and then pressing the Shift key and selecting the last item in the
section they want; this method requires the section of items to be adjacent. The user can
also select multiple, non-adjacent items by pressing the CTRL key and clicking from the
list.
Check boxes and option buttons are used to create Boolean selection options for the
user. Frames can be used to group these items or other related items.
Check boxes imply a positive selection (that is a yes, true, on, etc value) when checked;
the opposite is true if they are unchecked or de-checked. To insert a check box into your
form, use the icon from the Control Toolbox. The check boxes should appear as
shown in Figure 18.7.
9
Chapter 18: User Interface
The name given to a check box should begin with “chk” followed by a descriptive name.
For example, if we have two check boxes, one of which is associated with searching
some database of textbooks by author name and another by book title, we could name
them “chkAuthor” and “chkTitle”.
The Value property of check boxes can be used as it is with Boolean variables. That is,
you can state either of the following lines of code to see if one of the above check boxes
is true:
We also use the Caption property to give a brief description to each check box. This is
important as it will describe the selection being made if the check box is checked.
Option buttons imply a positive selection (that is, a yes, true, on value) when selected;
the opposite is true if they are de-selected. To insert an option button into your form, use
the icon from the Control Toolbox. The option buttons should appear as shown in
Figure 18.8 (even without the frame).
The name given to an option button should begin with “opt” followed by a descriptive
name. For example, if we have two option buttons, one of which is associated with
buying and another with selling, we could name them “optBuy” and “optSell”.
The Value property of option buttons can be used as it is with Boolean variables. That is,
you can state either of the following lines of code to see if one of the above option
buttons is true:
10
Chapter 18: User Interface
End If
We also use the Caption property to give a brief description to each option button. This
is important as it will describe the selection being made if the option button is selected.
We can also use option buttons with a frame. A frame groups similar items together. For
example, it can be used to group textboxes which contain related data or check boxes
which apply to the same category. To insert a frame into the user form, use the icon
from the Control Toolbox. A frame which groups two option buttons is shown in Figure
18.8.
The name of a frame should begin with “fra” followed by a descriptive name. For
example, if we have a frame which contains the buy/sell option buttons, we may name it
“fraBuySell”.
Frames primarily use the Caption property and Visible property. The Visible property of a
frame can be usedl to set the Visible property of a group of controls. For example, if we
have three textboxes in one frame, to make all of the textboxes not visible, we can just
set the Visible property of the frame to False.
Frames become more interesting when applied to option buttons as this makes the
option buttons mutually exclusive. That is, when option buttons are used without a
frame, more than one can be selected (as with check boxes). However, when option
buttons are placed inside a frame, only one can be selected at a time. This feature is
only true for option buttons (not for check boxes, or toggle buttons). This may be
applicable in our buy/sell example above; a user may only be allowed to buy or sell a
selected item, both actions cannot be simultaneously performed.
11
Chapter 18: User Interface
Toggle buttons are similar to check boxes and option buttons in that they imply a
positive selection (that is, a yes, true, on value) when clicked; the opposite is true if they
are un-clicked. To insert a toggle button, use the icon from the Control Toolbox. The
toggle button appears as shown in Figure 18.9.
A toggle button name should begin with “tgl” followed by a descriptive name. For
example, if we want to view data that is grouped by years, we may have several different
toggle buttons associated with each year of data; we could then name those toggle
buttons “tglYear1”, “tglYear2”, etc.
Toggle buttons also have values similar to Boolean variables. We can check the value of
a toggle button by typing either of the following codes:
Toggle buttons also use the Caption property to give a brief description of what the
toggle button will select. The caption appears directly on the button. Toggle buttons can
also be grouped in a frame; however, they do not become mutually exclusive.
12
Chapter 18: User Interface
Command buttons, unlike the controls we have learned so far, will be used for their
associated event procedures more than for their properties. The only property we will
use often with this control (aside from Visible possibly) is the Caption property (like
toggle buttons the caption will appear directly on the button). To insert a command
button, use the icon from the Control Toolbox. The command button should
appear as shown in Figure 18.10.
Command buttons should be named starting with “cmd” followed by some descriptive
name. The two command buttons we will use most often will be called “cmdOK” and
“cmdCancel”.
Tab strips and multipage items allow you to organize user input options.
Tab strips group data into different sections of this one control; all sections or tabs have
the same layout. That is one tab strip will have a set of controls which will appear on
each tab. Each tab can be associated with a group of data to which the tab strip
information belongs. For example, consider a customer contact database: each tab may
have the textbox values for name, phone number, and address, but each tab title may be
associated with a different customer.
13
Chapter 18: User Interface
To insert a tab strip, use the icon from the Control Toolbox. The tab strip should
appear as shown in Figure 18.11.
A tab strip should be named starting with “tab” followed by a descriptive name. For
example, if a tab strip contains tabs of information for different customers, it might be
called “tabCustomers”.
Since a tab strip is a collection of tab objects, different properties of tabs can be
considered sub properties of tab strip. To refer to a specific tab, say a specific customer
in the above tabCustomers example, we can type:
tabCustomers.Tab(customer1)
The main property used with tab strips to capture a selected value is SelectedItem. We
can find the number of tabs in the selected tab strip by using the Count method. To add
a new tab to the tab strip, just right-click on the tab strip in the design view of the user
form in the VBE and choose “new page” from the list of options.
The important characteristic to remember about tabs is that each tab contains the same
set of controls. These control values can be changed or recorded based on the tab index
which is selected by the user. Let us consider an example.
Suppose we have a form which displays the home, office, and cell phone numbers of a
contact using a tab strip (see Figure 18.12). Each tab has only one textbox; however, the
values of the textbox can change depending on which tab is selected. In the figure
below, a different phone number is displayed when the Home Phone tab is selected (a)
than when the Cell Phone tab is selected (b), but the same textbox control is used.
14
Chapter 18: User Interface
(a) (b)
Figure 18.12 The tabs have the same textbox control, but different values are shown for
different tabs.
To change the value of the textbox, we need to determine which tab has been selected
by the user. Each tab has a numerical index which is stored as the tab strip value. In this
example, the Home Phone tab is 0, Office Phone tab is 1, and Cell Phone tab is 2. We
use the Change event procedure of the tab strip and a Select Case statement to check
the value of the tab strip (see Figure 18.13). Depending on this value (0, 1, or 2), we
update the textbox value accordingly.
Figure 18.13 The Change event procedure for the tab strip.
A multipage control, on the other hand, can be considered a collection of individual form
objects, or pages. Each page can have a different layout and is treated as a separate
form. To insert a multipage, use the icon from the Control Toolbox. An example of a
multipage control is shown in Figure 18.14.
15
Chapter 18: User Interface
A multipage should be named starting with “mpg” followed by a descriptive name. The
Value property is used with multipages to denote the index of the particular page.
It is important to remember that multipages are different from tab strips in that they have
a unique set of controls per page. That is, in the tab strip example above, we had one
textbox named txtPhNum which appeared on each tab. However, if we had used
multipages in this example, the textbox on each page would be unique; for example,
txtPhNumHome, txtPhNumOffice, txtPhNumCell.
Let us consider an example of a multipage. Suppose we are now not only keeping track
of a contact’s phone numbers, but mailing address, email address, and other notes as
well (see Figure 18.15). In this case, we would want to use a multipage control instead of
a tab strip because each category of information, that is each page, requires a different
set of controls. For example, for the Mailing Address page, we use several textboxes (a)
where as for the Notes page we have use combo boxes and option buttons (b).
(a)
16
Chapter 18: User Interface
(b)
Figure 18.15 Each page of a multipage has a unique set of controls.
Since each control is unique, values can be changed or recorded for any control at any
time. You can also use the Change event with multipages if you want to change some
control values or properties when a page is selected.
Scroll bars and spin buttons are both used to update values of other controls using event
procedures.
Scroll bars can change text, numerical values, or other values of associated controls.
To insert a scroll bar, use the icon from the Control Toolbox. An example of a scroll
bar is shown in Figure 18.16.
A scroll bar should be named starting with “scr” followed by a descriptive name. For
example, if a scroll bar is associated with a textbox which displays some size value, it
may be called “scrSize”.
The value of the scroll bar, which is numerical, can be captured using the Value
property. There are also the Min and Max properties which can be set for the scroll bar
values, along with a SmallChange and LargeChange property to specify how much the
17
Chapter 18: User Interface
scroll bar value should change for each change in the scroll bar position. The
SmallChange value is added or subtracted from the scroll bar value when the arrows are
clicked, the LargeChange value is used when the bar is clicked. There is also an
Orientation property which can be used to set the scroll bar position to vertical or
horizontal.
Spin buttons are similar to scroll bars, except that they only increment or decrement
numerical values of other controls. To insert a spin button, use the icon from the
Control Toolbox. An example of a spin button is shown in Figure 18.17.
A spin button name should start with “spn” followed by a descriptive name. For example,
if we have a spin button associated with inserting the height of different subjects in some
research, it may be called “spnHeight”.
The Value property, and Min, Max, and SmallChange properties are also used. The
Orientation property can also be applied to spin buttons.
18
Chapter 18: User Interface
Images allow you to display a picture in the form. To insert an image, use the icon
from the Control Toolbox. An example of an image window is shown in Figure 18.18.
An image can be named starting with “img” and the picture name. To assign an image to
the image window, use the Picture property and Browse options. You can use the
PictureAlignment and PictureSizeMode properties to adjust the alignment and size of
your image (all other editing should be done outside Excel).
RefEdit is used to select or enter a worksheet range. There is a button, similar to what is
used in Excel dialog boxes, which allows the user to collapse the form while they select
a range directly from the worksheet. To insert a RefEdit control, use the icon from
the Control Toolbox. An example of a RefEdit control is shown in Figure 18.19.
The name for RefEdit should begin with “ref” followed by a descriptive name. For
example, we could name the RefEdit control “refOutput” if we plan to store some output
range in this control value. To capture the range the user has selected, use the Value
property; this value will be a string variable type and so can only be assigned to a string
variable.
19
Chapter 18: User Interface
Another property of the user form is ShowModal. When set to True, the form must be
completed (and/or some command button which closes the form should be clicked)
before the user can return to the program or use any worksheet. However, when this
property is set to False, the form becomes modeless, or a floating form. When the user
form is modeless, actions can occur on the worksheet and the program can continue
while the form is still showing. This can be useful for navigational purposes or to have a
constant set of options always available to the user. We use floating forms in some of
our case studies. [Note: There are some restrictions applied to modeless forms; for
example the RefEdit control cannot be used on modeless forms.]
There are some general properties that apply to the user form as well as to most
controls. These include general formatting properties such as: Font, BackColor,
ForeColor, etc. As we have mentioned already some other common properties found for
most if not all controls are Name, Caption, and Visible. There are three other important
common properties used with most/all controls, they are: TabIndex, TabStop, and
Locked.
TabIndex and TabStop control the tabbing order in which the user can move through
items on the user form with the Tab button of their keyboard. The TabIndex property
takes a numerical value from 1 to the number of controls on the user form; 1 being the
first item visited when Tab is pressed. The TabStop property can be set to True or False
depending on whether or not you want the user to be able to select a certain control.
Similar to TabStop, the Locked property takes True or False values and allows you to
prevent the user from entering or changing any values of a control. This may be useful if
you want to, for example, have sequential forms and show some of the data entered on
the first form in a form, or forms, which follow. The control will appear darkened and its
value, if any, will be displayed, but the user will not be able to modify the control value if
the Locked property is set to True.
20
Chapter 18: User Interface
Another useful property of the controls on a user form is ControlTipText. This property
allows you to enter some text to serve as brief instructions to the user to explain what a
particular control will do. For example, you could enter the ControlTipText “Click here
when you are done” for a command button. This text will appear when a user brings their
cursor over the control. This property is available on all form controls.
Misc Form
Properties
Font, BackColor, ForeColor
TabIndex Index of tab order
TabStop True/False to allow user to stop or not stop at this
control by tabbing
Locked True/False to allow user to be able to modify or not
modify a control value
ControlTipText Text displayed when user brings cursor over control
Figure 18.20 Select a control from the list in the code view of a created form.
21
Chapter 18: User Interface
Once you have selected a control, the drop-down list in the upper right-hand corner of
the code window will have a list of events, or actions, associated with that particular
control. For example, after selecting optChoice from the list of controls, we see the list of
associated events in Figure 18.21.
Although there are several events available for each control, we will explain a few of the
more common ones. The first event procedure that will commonly be used in a form, is
the Initialize procedure of the User Form. This procedure will run when the form is first
opened. It is a good idea to set default values for textboxes in this code. This is also the
procedure in which we would set the RowSource of a combo box or list box. We may
also set some controls to be visible or not visible in this code. It is important to have
default values for the user so that they are further informed on the type of input you want
from them. (It also helps to make trial runs quicker when debugging if there are already
some values in the form.) However, remember that many initial values can be set using
the properties window of each control (Value, Visible, RowSource, etc). We recommend
only coding the dynamic properties of the controls in this initialization procedure.
The main event procedure associated with command buttons is the Click event. For the
“cmdOK” button, for example, the even procedure associated with this button would be
where we may assign variables to textbox values or create an If, Then statement with
some option buttons.
We may want to show another form after the user has finished filling out the current
form. To do this, we use the Show method of the form object. We simply state the name
of the user form we want to show next followed by the Show method.
We will almost always close the current form at the end of this event procedure. To do
this, we use the Unload Me statement. Me refers to the current form object; Unload can
be used with any form object name. The event procedure associated with the
“cmdCancel” button will usually just have this line of code and the End statement.
Examples of these event procedures may be as follows:
Sub cmdOK_Click()
quantity = txtQuantity.Value
With fraBuySell
If .optBuyThen
actions1…
22
Chapter 18: User Interface
Unload Me
frmMenu.Show
End Sub
----------------------------------------------
Sub cmdCancel_Click()
Unload Me
End
End Sub
The Change, or Scroll, event procedures are most commonly used to associate the
change in the scroll bar values with an effect on another control value. We may have the
following code associated with the Change event of the “scrSize” scroll bar which affects
the value of a textbox called “txtSize”:
Sub scrSize_Change()
scrSize.Min = 1
scrSize.Max = 30
scrSize.SmallChange = 1
Spin buttons also use the Change event procedure primarily to associate its values with
the values of another control. An example of transferring the “spnHeight” values to a
textbox called “txtHeight” might be as follows:
Sub spnHeight_Change()
spnHeight.Min = 4.5
spnHeightMax = 7.0
spnHeight.SmallChange = 0.25
txtHeight.Value = spnHeight.Value
End Sub
One more common event procedure is the After Update procedure. This can be used
with textboxes to perform some actions immediately after the user enters a value. For
example, you may ask a user to enter a date in one textbox, called txtDate, and want to
23
Chapter 18: User Interface
display to them the day for this date in another textbox, called txtDay. You may therefore
write an event procedure as follows:
Sub txtDate_AfterUpdate()
txtDay.Value = FindDay(txtDate.Value)
End Sub
Here we have referred to a function called FindDay to find the corresponding day for the
date given by the user. (How would you write this function? Hint: use an Excel
spreadsheet function.)
For option buttons and check boxes, the Click and Change events are most commonly
used. If the Click event occurs then the user has clicked on the option button or check
box. This event does not imply the value of the option button or check box; that is, you
will need to check if it is true or false. The Change event implies that the value of the
option button or check box has changed. That is, if you know that the control was
initialized to be true, then if the Change event occurs, you know it is now false.
Sub cmdOK_Click()
UserInput = txtInput.Value
Unload Me
24
Chapter 18: User Interface
End Sub
Even though the integer variable UserInput has been declared as a public variable in the
form module, it can not be referred to directly in any other module. That is, if we wanted
to use the input value given by the user in the txtInput textbox on the form in another
module in the application, we could not use the UserInput variable as is.
In order to refer to a public form variable outside of the form module, you have to provide
the name of the form before the variable name. (Note, this is only true for public
variables.) That is, to refer to the UserInput variable outside of the frmMain code, we
must type:
frmMain.UserInput
In the same way, we can use the form name to refer to any of its controls outside of the
form module. That is, to change the property of a form control in another module, we
might type:
frmMain.lblQuantity.Visible = True
However, it is important to keep in mind that all form control values are reset when the
Unload Me statement is used. Therefore, if you are intendeing to refer to a control value,
that is any input a user has entered or selected on a form, in a later procedure in another
module, you must call that procedure before making the Unload Me statement in the
form code. For example, to use the value entered by a user for the textbox txtInput in a
later procedure, called Calculations, we would have to type the following:
Sub cmdOK_Click()
‘txtInput.Value has been entered by user
Call Calculations
Unload Me
End Sub
However, you must keep in mind that the Unload Me statement is also used to close the
form. That is, you do not want to bring the user through the rest of the application with
the initial input form still displayed. We therefore, generally recommend assigning form
control values to variables defined as public variables outside of the form module. This
allows you to make the Unload Me statement without losing the variable values. To
rewrite the code in the example above, we would type (assume we have declared
UserInput as a public varaible in another module):
Sub cmdOK_Click()
UserInput = txtInput.Value
Unload Me
Call Calculations
End Sub
25
Chapter 18: User Interface
As we discussed with Input Boxes in Chapter 14, any time we are receiving something
from the user, we have to check for errors. We showed several simple error checking
methods in Chapter 16 using If, Then statements and some functions to check if user
values are the same data type as the variables we are assigning to them. These
structures and functions can also be used to check the values given in forms. You can
simply check the control value’s data type before assigning it to a variable.
Another important thing to check in forms is if the user entered values for multiple
controls. For example, if you have three textboxes for input, you may want to make sure
that they completely fill the form. There are a few special statements that can be used to
loop through all of the structures in your form. Let us first provide you with the final code
and then explain each part. In the code below, we are checking all of the textboxes in
the forms to ensure that numerical values have been provided by the user:
The first thing we do in this code is define a variable as a Control data type. The Control
data type is another example of an Object data type; this is similar to our usage of the
Worksheet data type in the Workbook_Open procedure. We then use a For Each, Next
loop to check all of the controls in our form. The statement Me always refers to the form
itself; therefore Me.Controls refers to the set of controls in the current form. (This is
again similar to how we loop through all worksheets in the current workbook using the
statement ActiveWorkbook.Worksheets.) We now want to find out if the control is a
textbox or not; that is, we do not need to check the values of the command buttons or
other controls that may not interest us at this point. To determine the type of control, we
use the TypeName function with an If,Then statement. If we have found a “TextBox”,
then we check if the value of the textbox is empty or non-numeric. If this is the case, we
display a message to the user to clarify that all textboxes should have numeric values.
Before ending the procedure, as we do in most error checking codes, we make the
statement ctl.SetFocus. Setting the focus will select the control we found to cause an
error so that the user’s cursor is in the first place where an error was found.
This structure can be used to check if textboxes intended to hold dates actually have
date values, if at least one check box or option button has been selected, and if at least
one item from a list box or combo box has been selected. We usually place this simple
error checking code at the beginning of our cmdOK_Click procedures if there are
multiple inputs from the user.
To use a form template, or import a form, you can again select any form from the Project
Explorer window and right-click. This time choose Import File from the list of options.
(You can also go to File > Import File at any time.) Then, simply select the form you want
to import. Note, however, that you cannot import a form which is already in use in
another application. To avoid this error, simply change the name of the form that is
already in use and then import your template. For example, if you have a standard form
template which you named frmBasic, you may want to import it several times within the
same application. To do this without causing an error, simply change the name of the
form (using the Properties window) after each import.
18.8 Navigating
27
Chapter 18: User Interface
We have already discussed some navigational techniques to help the user move through
the program as needed. These techniques are all part of designing a good user
interface.
Buttons, or shapes, on the spreadsheet are usually a great tool for navigating the user
not only through different sheets of the program, but also through different steps or
actions. Some of these buttons might be labeled “Next”, “Back”, “Exit”, “Start”, or “View
Example”. They should be placed at the top of the worksheet in a visible spot. The code
for these macros can be identical or call an identical function (as seen in Chapter 15).
Thus these navigational tools can be beneficial to the user without adding too much
code to the program.
A useful property of these buttons, or shape objects, is the Visible property. By naming
these buttons appropriately, you can use the Shape object and Visible property to hide
and show different buttons. This is a good way to prevent your user from being confused
or overwhelmed by too many options. For example, you may initially only show some
instructions on a worksheet and a “Done” button. Then, after the “Done” button is
pressed, and the users actions have been recorded by an assigned macro, a “Next”
button can appear and the “Done” button can disappear. This lets the user know that
they should now move on to the next worksheet.
As we explained in Chapter 15, the index value of worksheets can also be used in
creating navigating procedures. To find the index value (or tab number) of a worksheet,
you can simply use its Index property. For example, if you write the code MsgBox
Worksheets(“Sheet 1”).Index, a Message Box with the value 1 would be shown. Once
you know a worksheets’ index value, you can simply refer to it by this index (instead of
by the worksheet name). For example, instead of writing Worksheets(“Sheet 1”).Visible =
True, you could write Worksheets(1).Visible = True.
To prevent your user from changing formatting or title values in any worksheet tables or
other features, you can protect the worksheet. To do this, we will use the Locked
property of a range of cells. For any cells which the user will need to enter values, or for
which the program may enter values, leave the Locked property at its False default
value. Then, set all other cells’ Locked properties to True. Any cell which is locked
cannot be modified.
After the appropriate cells have been locked and unlocked, select Tools > Protection >
Protect Worksheet from the menu. You will be prompted to enter a protection password
28
Chapter 18: User Interface
and then you can set the specific options which the user will no longer be able to
perform with locked cells.
This can be a very useful tool for creating the finished version of your program.
However, be aware that any cells whose values are also modified by the code (read
from or written to) will have to be unlocked in the code when necessary and locked again
if needed. For example, if a particular input range is locked, but you want to input new
data via some code (using input either from an Input Box or User Form), you might type
the following:
With Range(“Input”)
.Locked = False
.Value = UserInput
.Locked = True
End With
Another way to prevent users from modifying cell values is to hide rows or columns in
your worksheet. This can be done using the Hidden property; this property takes the
values True or False (see Chapter 13). Simply specify some cell or range of cells and
then use the Hidden property with the Rows or Columns property of the Range object.
For example, to hide the rows containing the range A1:B4, you would type:
Range(“A1:B4”).Rows.Hidden = True
Aside from hiding or protecting ranges in your spreadsheets, you may also want to
protect your VBA code. To do this, select the name of your project in the Project Window
and right-click. Select VBAProject Properties from the drop-down list. A window will
appear allowing you to enter General summary information for your project (name,
description, etc). If you click on the Protection tab, you can then choose to “Lock project
for viewing”. You can then specify a password to be used in order to unlock and view
your code.
There are some other options which can be set for each worksheet in your program to
finalize the version which the user will see. These are not necessary nor do they affect
the program performance; they can, however, make your program look more
professional.
To view these options, select Tools > Options from the menu. Some of the options we
usually change include: not viewing gridlines, hiding sheet tabs, and hiding column and
row headings. You can also hide several of your toolbars by clicking View > Toolbars
from the menu and unchecking some or all of the toolbars. We recommend that these
options not be selected until after all final debugging and testing has been performed so
that editing is easier.
18.10 Applications
18.10.1 Real Estate Search Spreadsheet Application
29
Chapter 18: User Interface
As we mentioned in Chapter 12, controls can also be placed on the spreadsheet. This
application uses the properties and events of the controls explained above in a small
spreadsheet application for real estate search. Note that when using controls on the
spreadsheet all of the code associated with the controls placed on the spreadsheet is
located in the code window for the spreadsheet object in the Project Window.
Consider a real estate search application in which a user can search for houses based
on certain criteria. This search is performed on an Excel database of real estate data, as
shown in Figure 18.23. The criteria for this search includes a maximum price, minimum
area (square feet), minimum number of bedrooms, minimum number of bathrooms, and
location in the city. Figure 18.24 shows the application with the result of one search
made using the selected criteria.
Let us first understand the design of the application by looking at the controls and their
properties. We use six different controls in the criteria section of the spreadsheet: a
scroll bar, a textbox, two spin buttons, a combo box, and two command buttons. These
controls, used in the spreadsheet, have almost exactly the same set of properties that
they do when used in form. We will specify different properties which are important.
30
Chapter 18: User Interface
We first use a scroll bar to determine the max price criteria. We placed the scroll bar on
the spreadsheet using the Control Toolbox and then viewed its Properties window (see
Figure 18.25). We begin by naming the control: scrPrice. As mentioned in above
sections, the most important properties of the scroll bar are its Max and Min values and
its SmallChange and LargeChange values. We set the Max value at 200,000 and the
Min value at 75,000 to model the price range of the houses available. We then set the
SmallChange and LargeChange values both to 1,000. As we explained above with event
procedures, we will usually connect a scroll bar to a textbox to display the value of the
scroll bar. However, when using controls on the spreadsheet, there is an extra property
which relieves the need for this procedure: this is the LinkedCell property. The
LinkedCell property holds the value of a cell name in which the value of the control will
be displayed. In this application, we set the LinkedCell property of the scroll bar to B6.
We then format this cell as currency so that whenever the scroll bar value changes, the
price is shown in the cell.
We then have a textbox for the user to specify the minimum area criteria. We name the
control, txtArea, and do not set any other property values.
The next two controls are spin buttons. We use these to determine the minimum number
of bedrooms and bathrooms in the criteria. We name them spnBed and spnBath
respectively. The most important properties for spin buttons are Max, Min, and
SmallChange. For spnBed, we set these equal to 5, 1, and 1 respectively. For spnBath
we set these equal to 3, 1, and 1 respectively. We will also use the LinkedCell property
for both of these controls in order to display the values of the spin buttons. For spnBed,
31
Chapter 18: User Interface
we set the LinkedCell property to B10. For spnBath, we set the LinkedCell property to
B12.
The last criteria control is a combo box which we use to specify the location of the
house. We name this combo box as cmbLocation. As explained above, the most
relevant property of a combo box or list box is the RowSource property. This property
sets the range of values which will be listed in the box. When using a combo box or list
box on the spreadsheet, the RowSource property is called the ListFillRange. In cells
I5:I8, we list the names of the four regions, or locations, where houses can be found:
Northwest, Southwest, Northeast, and Southeast. We also have All listed as an option to
view all locations. We name this range “Location” as shown in Figure 18.26. We then
hide the column which contains these cells so that the user does not see them when
running the application. To do this, you can just right click on the column heading and
choose Hide from the list of options.
Figure 18.26 Hidden columns contain the data for the combo box.
The last controls we place on the spreadsheet are the two command buttons. We first
create a “Search” button, named cmdSearch, which we will use to capture the current
values of all the controls as search criteria. We then create a “View All” button, named
cmdReset, which we will use to reset any filtering done and show the original database.
After completing the design and formatting of the criteria section of the spreadsheet, we
are ready to code. We will use the Click event procedure of the cmdSearch button to
filter the house information based on the criteria specified. In order to filter the house
data, we will be using the AutoFilter method (see Chapter 12). Since we will always be
filtering the same range of data, which is the original Excel database, we have named
this range “Houses” for easier reference. For the price and area criteria, we will also be
using a custom filter since we are not searching based on known values from the data,
but rather from user-provided values. The general structure of the filter method using a
custom filter is as follows:
Remember that when filtering data, each field refers to a column (see Chapter 10).The
field numbers we will use are based on the following matching: Address = 1, Agent = 2,
Price = 3, Area = 4, Bedrooms = 5, Bathrooms = 6, Location = 7. For the price criteria,
32
Chapter 18: User Interface
the inequality will be “<=”. For the area criteria, the inequality will be “>=”. For the
number of bedrooms and number of bathrooms, the inequality will also be “>=”. For the
location criteria, we can ignore the inequality and just give the value (we can also ignore
the Operator argument). The values for each criteria will be the value of the control
which is involved in the criteria. The code is shown in Figure 18.27.
Figure 18.27 Code window for all five Change event procedures.
We will now write code for the cmdReset button. This will also be a Click event
procedure. In this event we want to remove all filtering from the data. To remove any
filtering, we just simply type the AutoFilter method without any fields or criteria:
Range(“Houses”).AutoFilter
The event procedure for the cmdReset command button is shown in Figure 18.28.
We are almost finished at this point; however, we notice that if we run our code as is,
there is a slight problem with the filtering: whatever filtering is done first, remains as
multiple criteria are specified. This means that if the user first specifies a 2 bedroom
house and then clicks to see what 3 bathroom houses are available, we will filter for 2
bedroom houses with 3 bathrooms instead of all houses with 3 bathrooms. Therefore,
before each criteria’s filtering is done, we must first reset our data to be unfiltered. We
know this code from the cmdReset_Click event procedure above. We could simply enter
this line of code in each previously created event procedure, or we can create a small
function procedure to call multiple times. In Figure 18.29, we have created a function
procedure called ShowAll which we call from each event procedure, including the
cmdReset_Click event procedure.
33
Chapter 18: User Interface
There is one last event procedure to write which is equivalent to Userform_Initialize; this
is the Worksheet_Activate procedure (see Figure 18.30). This procedure could be used
to call the ShowAll function to ensure that the entire house data is displayed when the
user first opens the application. We can also ensure that the row source of the combo
box is initialized; again, we use the ListFillRange property of the spreadsheet combo box
to do this.
Figure 18.30 The Worksheet_Activate event procedure is used to reset the database
and set the row source of the combo box.
In this application, we are designing a search form for a product in a computer product
database. The program finds a product based on certain search priorities and search
criteria.
There are only two sheets needed for this application: “Products Search” and “Products
Database”. The “Products Search” sheet is the welcome sheet (see Figure 18.31). A
brief description of the application is given, and there is a Start button.
34
Chapter 18: User Interface
The “Products Database” sheet contains the Excel database through which we will
search for products which meet the user’s criteria. The user, however, will never see this
sheet (see Figure 18.32).
There will be two forms for this application: one to ask about the search priority, and one
to ask about the search criteria. The search priority form will determine if the user wants
to find their product based on lowest price or quality level. This form should have only
these options and only one of these options should be selected. We therefore use a
frame with two option buttons to give the user these options. A description label is given
along with the typical two command buttons: OK and Cancel (see Figure 18.33).
35
Chapter 18: User Interface
Figure 18.33 This form prompts the user to select their search priority.
When OK is pressed, we need to capture which option button has been selected. These
options will be associated with public Boolean variables which will determine how the
search is performed later in our code. In the form which follows, the search criteria form,
we will also have some frames related to the selected search priority which may or may
not be visible depending on what the user selects in this form. We will explain these
shortly. Below is the code associated with the OK button of this search priority form. The
only code associated with the Cancel button is to unload the form.
Figure 18.34 Based on the option button selected, Boolean variable values are given
and appropriate updates are made to the frames on the next form.
The second form, the search criteria form, will take more input from the user to
determine which product they are searching for and what other criteria they are
searching by (see Figures 18.35 and 18.36). This form uses frames to group some
controls together. We have named the OK button on this form cmdFindProduct. Some of
these labels and frames may not appear depending on the user’s choice in the search
priority form.
36
Chapter 18: User Interface
Figure 18.35 The search criteria form options when the search priority is lowest price.
The Products combo box lists the main product types in the database. We will only
search for products within this matching product category. To set the RowSource of the
combo box, we create a small table below our main database with the names of the
product categories. We name this range “ProdList” in Excel and assign this range name
to the RowSource property. The initialization code for this form is:
The Quality information will appear only if Quality is a search priority. These two check
boxes will determine which quality product the user wants. If both are checked either of
the two quality levels will be acceptable. The Price Limit option allows the user to limit
the search to be below a certain price level. This option is not provided if the user is
searching by lowest price. The spin button is associated with the adjacent textbox. The
code for this spin button is as follows:
37
Chapter 18: User Interface
Figure 18.36 The search criteria form options when the search priority is quality level.
If Quality was checked on the first form, then we want to capture which quality level
check box was marked. Since the price limit option is not required to be used, we need
to check if there is a value in the corresponding textbox before capturing its value; we do
this using the IsNull function. We also tell the user that if this value is set to 0, we will
ignore it. The final action to take in the code associated with the Find Product button is to
call the sub procedure which searches for the data. The code is as shown below:
If QualityLevel Then
If chkStandard And chkPremium Then
Quality = "Any"
ElseIf chkStandard Then
Quality = "Standard"
ElseIf chkPremium Then
Quality = "Premium"
End If
38
Chapter 18: User Interface
End If
Unload Me
Call FindProduct
End Sub
This FindProduct sub procedure will first sort the entire database depending on the
product selected and then by the search criteria. We name this database as “Database”
in Excel to make the code easier to write. We use a counting variable i to search each
row in the database and two variables called RowStart and RowEnd to capture the row
values which contains the item which meets the user’s criteria.
If the user has selected lowest price as their search priority, then once we find the
desired product in the database list, we check the price column to see if the price is
lower than the price limit specified in the search criteria. Once we find a price value
greater than this limit, called BestPrice, we record the final row which matched the
criteria so that we can copy the results later.
i=1
RowStart = 0
RowEnd = 0
If Price Then
'sort by product and then by price
Range("Database").Sort Key1:=Range("Product"), Order1:=xlAscending,
key2:=Range("Price"), order2:=xlAscending
With Range("Database")
Do While .Cells(i, 1) <> ""
If .Cells(i, 1) = Product Then
RowStart = i
Do While .Cells(i, 1).Value = Product
If .Cells(i, 6).Value > BestPrice Then
RowEnd = i - 1
Exit Do
End If
i=i+1
Loop
RowEnd = i - 1
Exit Do
End If
i=i+1
Loop
End With
39
Chapter 18: User Interface
If the user has selected quality level as their search priority, then once we find the
desired product in the database list, we check the quality column to see if it matches the
criteria specified by the user. Since the quality level criteria was given with check boxes,
the user could have selected both; therefore, we first check if the quality level was “Any”.
In that case, we select all of the listings for the product. Otherwise we check the quality
column until the value is no longer equal to the user’s selection. We again use the
RowStart and RowEnd variables to store the location of these values which we will copy
later.
We use a range variable and name another range on the spreadsheet to perform these
actions. The code is below:
40
Chapter 18: User Interface
Sub DisplayProduct()
Results.Value = "Search Results"
Results.Font.Bold = True
Range("Titles").Copy
Results.Offset(1, 0).PasteSpecial
With Range("Database")
If RowStart = 0 Then
Results.Offset(2, 0).Value = "No product in the database matches your
criteria."
Else
Range(.Cells(RowStart, 1), .Cells(RowEnd, 6)).Copy
Results.Offset(2, 0).PasteSpecial
End If
End With
The last procedure we need to make sure to write is the Main sub procedure. This code
will be associated with the Start button on the welcome sheet. This code simply
initializes the range variable, clears all previous search results, performs some
formatting, and then shows the first form.
Sub Main()
Set Results = Worksheets("Products Search").Range("B25")
frmSearchPriority.Show
End Sub
These are all the worksheets, forms, and procedures necessary to build this application.
18.11 Summary
¾ The most important property which we will use for all user form items is the name
property. The name of an item from a user form is treated as an object in the code.
¾ A label is used to give a description to any item on the form (as used in Figure 18.2).
A textbox is used to allow the user to enter some value. This value may be a string or
number.
¾ A combo box will list the first entry next to a drop-down button. When the button is
pressed, all other items in the list are shown. A list box has basically the same
functionality and several similar properties as a combo box. A list box will list all
items to be selected in a single box (that is, there is no drop-down button as with
combo boxes)..
¾ Check boxes imply a positive selection (that is a yes, true, on, etc value) when
checked; the opposite is true if they are unchecked or de-checked. Option buttons
imply a positive selection (that is a yes, true, on, etc value) when selected; the
41
Chapter 18: User Interface
opposite is true if they are de-selected. We can also use option buttons with a frame.
Toggle buttons are similar to check boxes and option buttons in that they imply a
positive selection (that is a yes, true, on, etc value) when clicked; the opposite is true
if they are un-clicked.
¾ Command buttons, unlike the controls we have learned so far, will be used for their
associated event procedures more than for their properties. The main event
procedure associated with command buttons is the Click event. To show another
form after the user has finished filling the current form use the Show method. To
close the current form at the end of this event procedure use the Unload Me
statement.
¾ Tab strips group data into different sections of this one control; all sections or tabs
have the same layout. That is one tab strip will have a set of controls which will
appear on each tab. Each tab can be associated with a group of data to which the
tab strip information belongs. A multipage control, on the other hand, can be
considered a collection of individual form objects, or pages. Each page can have a
different layout and is treated as a separate form.
¾ Scroll bars can change text, numerical values, or other values of associated controls.
The Change, or Scroll, event procedures are most commonly used to associate the
change in the scroll bar values with an effect on another control value. Spin buttons
are similar to scroll bars, except that they only increment or decrement numerical
values of other controls. Spin buttons also use the Change event procedure primarily
to associate its values with the values of another control.
¾ Images allow you to display a picture in the form. RefEdit is used to select or enter a
worksheet range.
¾ There are some properties associated with the user form itself which can be useful.
There are several Position properties that can be modified. To view these, use the
Categorized tab of the Properties Window. Another property of the user form is
ShowModal. When set to True, the form must be completed (and/or some command
button which closes the form should be clicked) before the user can return to the
program or use any worksheet. However, when this property is set to False, the form
becomes modeless, or a floating form.
¾ Buttons are usually a great tool for navigating the user not only through different
sheets of the program, but also through different steps or actions. The code for these
macros can be identical or call an identical function. A useful property of these
buttons, or shape objects, is the Visible property. By naming these buttons
appropriately, you can use the Shape object and Visible property to hide and show
different buttons.
¾ To prevent your user from changing formatting or title values in any worksheet tables
or other features, you can protect the worksheet. To do this, we will use the Locked
property of a range of cells.
¾ There are some other options which can be set for each worksheet in your program
to finalize the version which the user will see. To view these options, select Tools >
Options from the menu.
42
Chapter 18: User Interface
18.12 Exercises
18.12.1 Review Questions
1. The following User Form allows the user to input the current temp using either a
Scroll Bar or a Textbox as shown below:
The following code currently modifies the value of the Scroll Bar when a new value
is typed into the Textbox. Write a similar line of code which will update the value of
the Textbox if the Scroll Bar is used.
End Sub
2. Create a User Form that does not permit you to open a worksheet until you enter the
password go gators.
43
Chapter 18: User Interface
3. Create a User Form that allows the user to select a range of cells using the RefEdit
control. Allow the user to change the background color, font color, and the font style
of the selected range. Give the user at least three different color chooses. The
different font styles available should include: regular, bold, italic, and underline. Any
combination of bold, italic, and underline can be chosen.
5. Create a conversion program based on the following User Form example. Include at
least two units for each category.
7. Develop a program which allows a user to enter data which will be analyzed to
determine if any trends exist.
a. Create a form which asks the user for the size of the data.
44
Chapter 18: User Interface
b. Format a table in the spreadsheet where they can enter their data.
c. Also ask the user which trendline they would like to add. Use option buttons for
Normal, Exponential, and Linear curves.
d. Generate data from the distribution given and graph both data together.
e. Tell the user the mean error and ask if they would like to try another trendline. If
they do, store the mean error result and distribution type of the previous
trendline(s).
f. Make a comparison of which trendline was the most accurate.
8. Create a conference registration form. Your form should include the following:
a. Option Buttons to determine registrant type: student, professor, or retired.
(Each should have different registration fees.)
b. Check Boxes listing sessions to attend: A through F.
c. Check Boxes listing events and tours offered. (Each should have a different
price.)
d. Toggle Button for whether or not they are bringing a guest. (There should be an
additional guest fee.)
Based on these values, calculate the total price for the registration. Then provide
the user with the following (either on a different form or in a frame below which is
updated after the above information is entered).
Then create a final report on the spreadsheet summarizing the information given
by the user and creating a confirmation number.
9. Apartment guides allow users to search for an apartment in their area based on their
preferences. Create a User Form-based program that allows the user to search the
area for apartments based on the following spreadsheet:
Individual Roomate
Apartment Name Bedrooms Bathrooms Pets Pool Furnished Cable Lease Matching
Arbor Point 4 2 no yes yes yes yes yes
Banyon Bay 3 1 no yes yes yes yes yes
Charleston Creek 2 1 no yes no no yes no
Don Juan
Apartments 2 2 no yes no no yes no
Ivy Lane 3 3 no yes no no no no
Stone Valley 4 2 yes no no no no no
Tanor Lodge 3 1 yes yes yes no no no
West View 3 2 no yes no yes no yes
45
Chapter 18: User Interface
c. Once this button is pressed, show the user a new form with a combo box of
only the names from the database.
d. Once a name is selected, show the user a new form with all of the data for this
particular user only.
e. Then provide textboxes for the ending date and position. There should be an
Update button which will change the database only if a value(s) has been
entered in one of the textboxes.
f. Tell the user that the database has been updated and return them to part (a).
11. Loan Manager: Help a user determine what his monthly payments would be based
on the amount he needs from a loan and for how long he needs the loan. Assume
the user can choose among two loans: the first has 7.9% APR with an annual fee of
$20, the second has a 15% APR, but no annual fee. Create a user interface on the
spreadsheet with the following:
a. A frame with option buttons for the two loan choices.
b. A scroll bar to represent the amount of the loan.
c. A spin button to represent the time (this should be 12, 18, 24, …, 60 months).
d. Use Excel functions to calculate the different payment amounts depending on
the criteria specified by the user. This calculation should be done when a
button is pressed.
e. Repeat parts (a) through (d) with a user form and VBA functions instead.
13. Text Search: On a spreadsheet, list in a column some famous quotes or readings.
Enumerate these readings with chapter or section numbers.
a. Create a user form which asks the user to enter some text value(s) to search
for among these quotes/readings.
b. After they enter this information into a textbox on the form and press a Search
button, you will search for the text.
c. If you find the text, you will return all of the chapters/section numbers where the
text was found. Show these in a list box.
d. The user can then select one of these to see the full text of the corresponding
chapter/section number displayed in a label below.
To search for text in ranges, you can use the Find method. The Find method
requires one parameter which is the value for which you are searching. There are
46
Chapter 18: User Interface
several other optional parameters such as the starting position of the search and
whether or not to match the case. The format of the Find method is:
Range(“name”).Find(value)
This method returns a range where the value was found. If no value was found, the
range will be set to Nothing. You can use a loop such as what is shown below to
check if text has been found:
For i = 1 To 3
If Not Range("Text").Cells(i).Find("hello") Is Nothing Then
MsgBox "Text found in row " & i
End If
Next i
If you want to search for text using some wildcard symbol like *, use the Like
comparison statement. The format of this statement is shown in the example
below:
For i = 1 To 3
If Range("Text").Cells(i).Value Like "hel*" Then
MsgBox "Text found in row " & i & " using Like"
End if
Next i
14. Airlines use forms to search for flights that meet the travel needs of their customers.
Based on these online forms, generate a UserForm and back up with the supporting
data so that a user can search for a flight. Based on their inputs, display all flights
that meet their criteria on a worksheet. Use the following flight schedule: (Note:
Assume all flights depart daily)
47
Chapter 18: User Interface
48
Chapter 18: User Interface
15. Quality Control: A company keeps track of product deliveries made by various
suppliers. Each product has a different measure for quality from 1 to 10, 10 being
best. The promised delivery amount and date are also stored for each product along
with the actual delivery amount and date.
Quality Measure
Product Supplier (1-10) Promised Amount Promised Date Actual Amount Actual Date
A 5 100 10-Jan 90 15-Jan
staplers B 4 120 12-Jan 90 12-Jan
C 6 130 15-Jan 130 25-Jan
A 3 200 17-Feb 200 27-Feb
pencils B 7 130 17-Mar 140 22-Mar
D 8 120 20-Feb 110 1-Mar
A 3 130 10-Mar 130 10-Mar
paper clips
B 6 140 15-Mar 150 15-Mar
B 4 70 20-Mar 40 30-Mar
pens
C 8 80 29-Mar 80 8-Apr
A 9 90 2-Apr 100 7-Apr
paper pads B 5 80 14-Apr 80 14-Apr
C 7 100 10-Apr 70 20-Apr
scissors B 7 110 15-Apr 100 20-Apr
49
Chapter 19
The Solver Revisited
Chapter Overview
19.1 Introduction
19.2 Review of Chapter 8
19.2.1 Understanding the problem
19.2.2 Preparing the worksheet
19.2.3 Solving the Model
19.4 Application
19.4.1 Dynamic Production Problem
19.5 Summary
19.6 Exercises
Chapter 19: The Solver Revisited
19.1 Introduction
This chapter shows the user how to use the Solver with VBA commands. As many DSS
applications may involve optimization, it is important to know how to set up and run the
Solver from VBA. Using the Solver with VBA also allows you to involve the user in
setting the objective function as a minimization or maximization problem, changing the
number of variables, and including or ignoring any constraints. The Solver can also be
run as part of a loop, such as in an application in which optimization and simulation are
combined. We have developed several DSS applications which use the Solver
dynamically through VBA commands. Some examples are Portfolio Management and
Optimization, Beta of Stocks, Sales Force Allocation, and Option Pricing.
Using the Solver requires a short sequence of steps: 1) reading and interpreting the
problem, 2) preparing the spreadsheet, and 3) solving the model and reviewing the
results. In the interpretation of the problem, we define the mathematical model; that is,
we must define our decision variables, objective function, and constraints via
mathematical representation.
Mathematical models transform a word problem into a set of equations that clearly
define the values you are seeking given the limitations of the problem. There are three
main parts of a mathematical model: decision variables, objective function, and
constraints.
Decision variables are variables that are assigned to a quantity or response that you
must determine in the problem. They can be defined as negative, nonnegative, or
unrestricted variables. An unrestricted variable can be either negative or nonnegative.
These variables are used to represent all other relationships in the model, including the
objective function and constraints.
The objective function is an equation that states the goal, or objective, of the model.
Objective functions are either maximized or minimized; most applications involve
maximizing profit or minimizing cost.
The constraints are the limitations of the problem. In most realistic problems there are
certain limitations, or constraints, which we must satisfy. Constraints can be a limited
amount of resources, labor, or requirements for a particular demand. These constraints
are also written as equations in terms of the decision variables.
2
Chapter 19: The Solver Revisited
We saw an example in Chapter 8 of the Product Mix problem. A company produces six
different types of products. It wants to schedule their production to determine how much
of each product type should be produced in order to maximize its profits. Production of
each product type requires labor and raw materials; but the company is limited by the
amount of resources available. There is also a limited demand for each product, and no
more than this demand per product type can be produced. Input tables for the necessary
resources and the demand are given. This is a linear programming problem as the
constraints and objective function are linear with respect to the decision variables as well
will see below. We therefore use the Standard Solver.
We can perform the first step of using the solver by reading this problem description and
determining the model parts. The decision variables are the amounts to produce of each
product. There are three constraints: labor, raw materials, and demand. The objective
function is to maximize the total profit.
The second step is equally important: preparing the spreadsheet. We must translate and
clearly define each part of our model in the spreadsheet. The Solver will then interpret
our model according to how we have declared the decision variables, objective function,
and constraints in the spreadsheet. We use referencing and formulas to mathematically
represent the model in the spreadsheet cells.
To enter the decision variables, we list them in individual cells (some verbal description
such as x1, x2 or item1, item2) with an empty cell next to each one. The Solver will
place values in these cells for each decision variable as it solves the model. All other
equations (for the objective function and constraints) will reference these cells.
To enter the objective function, we place our objective function equation in a cell with an
adjacent description. This equation should be entered as a formula which references the
decision variable cells. As the Solver changes the decision variable values in the
decision variable cells, the objective function value will automatically be updated.
To enter the constraints, we place the constraint equations in the spreadsheet. We list
the equations separately with a description next to each constraint. The most important
part of setting up the constraint table is expressing the left side of our equations as
formulas. As each constraint is in terms of the decision variables, all of these formulas
must be in terms of the decision variable cells that Solver uses. These equations should
reference the decision variable cells so that as the Solver places values in these cells
the constraint values will automatically be calculated. Another important consideration
when laying out the constraints in preparation for Solver is that the RHS (right-hand side)
value of each constraint should be in individual cells to the right of these equations.
These values are used to enter constraints into the Solver. We should also place all
inequality signs in their own cells. This organization is necessary for the Solver to
interpret our model.
Another advantageous way to keep our constraints organized as we use the Solver is to
name our cells. Using the methods discussed in Chapter 3, we can name the ranges for
the decision variables and the cell which holds the objective function equation. We can
also name ranges of constraint equations which are in a similar category of constraints
3
Chapter 19: The Solver Revisited
or which have similar inequality signs. This makes inserting these model parts into the
Solver easier when using both Excel and VBA code.
The spreadsheet is now prepared for Solver with all three parts of the model clearly
displayed. Let us refer to the example used in Chapter 8 to show how the spreadsheet
should appear after this preparation step is complete. In Figure 19.1, we see the overall
spreadsheet layout. We have organized our cells by Input, Decision Variables,
Constraints, and Objective Function. The Input table was given. We calculated the Unit
Profit row by subtracting the Variable Cost from the Unit Price.
We have also named some ranges on the spreadsheet. We have named the Decision
Variable range as “PMDecVar”, the Labor resource requirement row as “PMLabor”, the
Raw Material resource requirement row as “PMRawMat”, and the Unit Profit row as
“PMUnitProfit”. These names will be helpful in writing the constraint and objective
function formulas as well as for inserting cell references in the Solver.
After appropriately preparing the cells, we use the Solver to find our solution. In Figure
19.2, we show the Solver dialog box. We have set the Target Cell to the location of the
objective function formula, the Changing Cells are set to the empty decision variable
cells which we named “PMDecVar”, and the Constraints box shows the left and right
sides of the constraint equations with the corresponding inequalities. Here we have
listed the labor and raw material constraints first, and then we listed the demand
constraint using the decision variable cells. We have also selected the two options:
Assume Linear Model and Assume Non-Negative.
4
Chapter 19: The Solver Revisited
The final results are shown in Figure 19.3. Notice that all constraints are met. The
company now knows how much to produce of each product type and what their
maximum profit will be.
5
Chapter 19: The Solver Revisited
[Note: Before using Solver commands in VBA, you must reference the Solver library in
the VBE. To do this, go to Tools > References and choose Solver from the list. If you do
not see Solver as an option, use the Browse button and go to C Drive > Program Files >
Microsoft Office > Office (or Office10) > Library > Solver and look for the “Solver32.dll”
file. Double-click this file; now you should find “SOLVER” in the list and check the box
next to it. If this does not work, copy the “Solver32.dll” file to the C > WINDOWS >
SYSTEM32 directory. You may also try to open the file “Solver.xla” directly and then
open the Excel file. If you do not find the “Solver32.dll” file, you may need to reinstall
Excel to your computer.]
Previously, when using the Solver from the Tools menu in Excel, we identified the cells
which contain the decision variables, objective function, and constraint equations to the
Solver using the Changing Cells, Target Cell, and Add Constraint inputs, respectively.
We now learn how to identify these parts of the model as Solver input using VBA code.
There are two main Solver functions used to input the Solver parameters in VBA. These
are SolverOK and SolverAdd. SolverOK is used to set the objective function (or Target
Cell) and decision variables (or Changing Cells). The format of this function and
argument titles are:
The SetCell argument is used to specify the range of the objective function. This cell
should contain the formula of the objective function which references the decision
variable cells. The MaxMinVal argument specifies if this objective function will be
maximized, minimized, or solved to a particular value. The values this argument can
take are 1 (= maximize), 2 (= minimize), and 3 (= value). If this argument value is 3, then
the ValueOf argument is used to set this value; if the objective function will be
maximized or minimized, then this argument is ignored. Some examples of using the
option of solving to a particular value would be solving a problem to a break-even point
or solving to achieve a particular profit value. The ByChange argument specifies the
range which contains the decision variables. This range of cells should not have any
values in it (there may be descriptive cells adjacent to the empty decision variable cells).
Using the above example, we would identify the decision variables and objective
function using this VBA function as follows:
Note that the appropriate worksheet should be activated if we want to specify the ranges
without a preceding worksheet name. [We should also use the
Application.ScreenUpdating statement before and after running the Solver.] As we know
from working with named ranges in VBA, we can modify these ranges elsewhere in the
6
Chapter 19: The Solver Revisited
code without having to update this function. The idea is to increase the efficiency of the
code by minimizing the amount of updates that would be needed if any changes were
made to the input. [Note that we could also use range variables instead of range names;
however, this is not useful when working with the Solver in Excel however.]
The second main Solver function will be used to input constraints; this is the SolverAdd
function. This function should be used to add each individual constraint or each group of
similar constraints. The SolverAdd function has three arguments:
The CellRef argument specifies the range which contains a constraint equation. This
equation should reference the decision variable cells. The Relation argument can take
one of five values which specify the inequality of the constraint: 1 is <=, 2 is =, 3 is >=, 4
is int (integer values), and 5 is bin (binary, 0/1, values). The FormulaText argument
specifies the range which contains the RHS value of the constraint. Using the above
example again, the SolverAdd function would be typed as follows:
There are two more functions which can be used to modify constraints: they are
SolverChange and SolverDelete. These functions will allow you to modify or delete a
constraints, respectively. They both have the same arguments as the SolverAdd
function. For example, to change the demand constraint from less than or equal to
greater than or equal, we would type:
Another function, which can be used before any input is entered, is the SolverReset
function. This function resets all Solver parameters. All input will be empty and all Solver
options will be set to their default values. It is generally a good idea to use this function
before any of the above input functions are used.
To set the Solver options in VBA, we use the SolverOptions function. This function has
many arguments for each of the options we have seen previously in the Solver Options
dialog box. There are two arguments which we will use more frequently, which are
AssumeLinear and AssumeNonNeg. Both of these arguments take True/False values;
True makes the corresponding assumption. For most of our models, we will set both of
these arguments to true as follows:
7
Chapter 19: The Solver Revisited
After the Solver input has been entered and any options have been set, we are ready to
run the Solver. To run the Solver in VBA, we use the function SolverSolve. This function
has two arguments and is written as follows:
SolverSolve(UserFinish, ShowRef)
The UserFinish argument uses a True/False value to determine whether to return the
Solver results with or without showing the Solver Results dialog box. We will usually set
this argument value to True; if the value is False then the Solver Results dialog box will
appear after the Solver has run the model. The ShowRef argument is used when the
StepThru option is set; hence, we will usually ignore this argument.
SolverSolve UserFinish:=True
The SolverSolve function also returns an integer value classifying the result. The values
0, 1, or 2 signify a successful run in which a solution has been found. The value 4
implies that there was no convergence, and the value 5 implies that no feasible solution
could be found. It can be useful to assign some variable to the SolverSolve function in
order to display an appropriate Message Box to the user if needed. For example:
[Note that when setting the SolverSolve command equal to a variable, you should use
parenthesis to specify the argument values.] We would like to note that this is the only
command needed to actually run the Solver. That is, if the Solver has already been set
up in the spreadsheet or in some initial part of the VBA code, at execution time, only the
SolverSolve command needs to be written. This is convenient for combining optimization
with simulation in which you may be performing several loops in which you are running
the Solver and storing the results. In such a scenario, only the SolverSolve command
would need to appear in a simulation loop. We discuss this more in Chapter 20 with
simulation in VBA.
8
Chapter 19: The Solver Revisited
When the Solver has finished running, we can decide whether or not we want to keep
the results and if we want to generate any reports. We specify this using the
SolverFinish function. This function has two arguments and is written as follows:
SolverFinish(KeepFinal, ReportArray)
The KeepFinal argument takes the value 1 if you want to keep the Solver solution and
the value 2 if you want the previous values to be kept (if this is the first time running the
Solver, then the previous values are null). The default value of this argument is 1, which
is the value which we usually want; therefore, this argument does not need to be
specified if we want to use the default value.
The ReportArray argument is used to specify which reports, if any, you want to
generate. The value of this argument is entered using the Array function. The array
values can be 1 (to generate an Answer Report), 2 (to generate a Sensitivity Analysis
Report), and/or 3 (to generate a Limits Report). Any or all of these values can be entered
in the array. For example, if we want to keep our final solution and generate a Sensitivity
Analysis Report and Limits Report for the above problem, we would type:
In this code, since we do not list 1 in the array, an Answer Report will not be generated.
Note that if we do not want to generate any reports and prefer to keep the Solver
solutions, we could just type the SolverFinish command without any arguments.
The final code for setting the Solver parameters for the above example can be written in
sub procedure as follows:
Sub UsingSolver()
Worksheets("Shipping").Activate
Application.ScreenUpdating = False
SolverReset
SolverOK SetCell:=Range("ObjFunc"), MaxMinVal:=1, ByChange:=Range("DecVar")
SolverSolve UserFinish:=True
We will now comment briefly on three other functions that correspond to saving a set of
Solver parameters. These are SolverSave, SolverLoad, and SolverGet. The SolverSave
function will save a certain set of Solver parameters that have been summarized in a
9
Chapter 19: The Solver Revisited
range on any worksheet. This range is the value of the one function argument
SaveArea. The SolverLoad function will load a set of Solver parameters that have been
saved. The argument for this function, LoadArea, would take the same value entered as
the SaveArea argument in the SolverSave function. The third function, SolverGet, can
be used to find information about a set of Solver parameters. More detailed information
on its two arguments, TypeNum and SheetName, can be found using the Help menu
option.
19.4 Applications
19.4.1 Dynamic Production Problem
However, we want this production problem to be dynamic. That is, we want the user to
decide how many items to consider in the problem and to provide the input for each
item. We limit these dynamic options to five possible items and prepare the spreadsheet
for the maximum number possible. This way, if the user provides input for all five items,
10
Chapter 19: The Solver Revisited
there is space allocated on the spreadsheet, but if they only choose to solve the problem
for three items, then the extra two rows will be left empty for the input and constraints.
To make this problem dynamic, we will develop a user interface. We have placed a
button on the spreadsheet called Solve Dynamic Problem which the user can press to
begin solving the problem (see Figure 19.4).
We will then bring the user to a form which asks them to specify the number of items, or
decision variables, for which they want to solve the problem (see Figure 19.5). We use a
spin button on this form to limit the value of this input from 1 to 5.
Figure 19.5 The Parameters form asks the user for the number of decision variables.
11
Chapter 19: The Solver Revisited
We will then bring the user to another form which allows them to enter the total weight
and space limits for the final production as well as all input values for each item (see
Figure 19.6). This is a dynamic form. Depending on the value entered in the first form,
the user should only enter input values for the same number of items on this form. To
accomplish this, for each item, we have created a frame containing text boxes for each
input value. We will then use the visible property of these frames to only show the
frames for the number of items specified by the user.
Figure 19.6 The Input form is dynamic in that it allows the user to enter input values
for the number of items they specified in the Parameters form.
Let us now discuss the necessary code to show these forms to the user, record the
dynamic information, and solve the problem. We will have a main procedure associated
with the Solve Dynamic Problem button called SetParameters. In this procedure, we will
initialize some variables, clear previous values, show the forms to the user, and respond
to the dynamic values.
12
Chapter 19: The Solver Revisited
The important issue to consider here is how to make this program dynamic without
making it too complicated. To do this, we will take advantage of using range names. Our
first dynamic range is for the decision variables: “ProdDecVar”. After the user specifies
the number of items to solve the problem for, we will begin at cell C11 and select the
range of cells below it accordingly. We will use a range variable DVRg to set to cell C11.
The next set of dynamic ranges are for the input values: “Weight”, “Space”, “Profit”,
“Quota”, and “Limit”. We will also use range variables to accordingly select the range of
cells equal in size to the number of items specified by the user. We use the variables
WtRg, SpRg, PrRg, QuRg, and LmRg respectively and set them equal to the cells in the
first row of the input table: C4 through G4. Redefining these four ranges plus the
decision variable range will be the only action necessary in the code to solve this
problem dynamically.
For our formulas in the spreadsheet, for the constraints and objective function, we have
used these range names.
Objective Function:
= SUMPRODUCT(Profit,ProdDecVar)
In order to prevent major changes in the worksheet, all we have to do is redefine these
range names in our code to make the problem dynamic; no change to these formulas is
necessary. We have also given range names to refer to the cells containing these
formulas for the constraints and objective function defined in the code: “WeightC”,
“WeightRHS”, “SpaceC”, “SpaceRHS”, and “ProdObjFunc”. These ranges are not
dynamic as they do not change size in respect to the number of items being solved in
the problem.
In Figure 19.7, we show the variable declarations and code for the SetParameters
procedure and a ClearPrev procedure. In the SetParameters procedure, we initialize our
range variables, call the ClearPrev procedure, show the user the first form (the second
form will be shown from the first form code), and then redefine our dynamic ranges. We
have set the value for the number of decision variables NumDV in the code of the first
form. At the end of this procedure, we call the SolveProb procedure in which we will use
the Solver code statements.
You may notice here that we have also defined and initialized some array variables.
Since our Input form involves five frames each with parallel text boxes, we decided to
declare an array of Control data type for these controls in order to reduce coding (refer
to Figure 19.6). We define Weight(5) as an array of Controls for the weight text boxes,
Space(5) as an array of Controls for the space text boxes, and similarly Profit(5),
Quota(5), and Limit(5) for the corresponding set of text boxes. The advantage of using
these arrays will become clear in the form code procedures. We also define an array
Frames(5) of Controls for each frame on the Input form. We initialize this array in the
13
Chapter 19: The Solver Revisited
SetParameters code by setting each index to a frame from the Input form (for example:
Set Frames(1) = frmInput.fraItem1).
Figure 19.7 SetParameters code, ClearPrev code, and variable declarations and
initializations.
Let us now discuss the code used in the two forms. In the first form, the Parameters
form, our main objective is to record the number of decision variables specified by the
user. We do this by setting our NumDV variable equal to the text box control value (this
could also be done using the spin button control value). The only other task is to display
the next form, the Input form. However, this second form is dynamic in that we only want
to show the frames, with the corresponding text boxes, for the number of items the user
specified. We therefore loop through our Frame array to set the Visible property to True
for the number of decision variables specified by the user. Note that we have initialized
14
Chapter 19: The Solver Revisited
the Visible property of each frame to be False in the UserForm_Initialize code. The
complete code for this form is shown in Figure 19.8.
[Note: We could have also made the spreadsheet more dynamic by hiding the rows of
the unused items. How would this be done? Hint: see Chapter 13.]
The code for the Input form is now fairly simple. We just need to record the values given
in the text boxes for each visible frame. We therefore again loop through our Frame
array. We check if the frame is visible; if it is then we can record the value of each text
box onto the corresponding range in the spreadsheet. Notice that here we use the arrays
of text box controls to record the values. This has become a very simple procedure
because of our arrays
15
Chapter 19: The Solver Revisited
Before using the arrays of text box controls, we have to set them to the appropriate text
boxes; we do this in the UserForm_Initialize procedure. We also give some default
values to each text box for all frames when we open this form. After initializing the text
box arrays, it is easy to set these default values simply by looping through each array
(see Figure 19.10).
Let us now return to our module to discuss the final code which takes place in the
SolveProb procedure. In this procedure, we need to input all model parts into the Solver
using the statements defined in this chapter. We begin with the SolverReset and
16
Chapter 19: The Solver Revisited
SolverOk statements. We then use the SolverAdd statement four times to specify the
quota and limit constraints of each item as well as the total weight and space
constraints. Notice that we use the range names previously defined in both the SolverOk
and SolverAdd statements. There is no extra code necessary in the Solver statements to
make this problem dynamic, since we have already redefined the range names.
We then set the SolverOptions and use the result variable with the SolverSolve
statement as explained above. The final code for this procedure is shown in Figure
19.11.
Figure 19.11 The SolveProb code inserts input to the Solver and solves the problem.
We can now solve this problem multiple times using the Solve Dynamic Problem button
and varying the number of items for which the problem is solved. If the result is
infeasible, we can simply modify the input values and solve it again.
19.5 Summary
¾ There are three main parts of a mathematical model: decision variables,
objective function, and constraints.
¾ Decision variables are variables that are assigned to a quantity or response that
you must determine in the problem. These variables are used to represent all
other relationships in the model, including the objective function and constraints.
¾ The objective function is an equation that states the goal, or objective, of the
model. Objective functions are either maximized or minimized.
¾ The constraints are the limitations of the problem. These constraints are also
written as equations in terms of the decision variables.
¾ Using the Solver requires a short sequence of steps: 1) reading and interpreting
the problem, 2) preparing the spreadsheet, 3) solving the model and reviewing
the results.
17
Chapter 19: The Solver Revisited
¾ In the interpretation of the problem, we define the mathematical model; that is,
we must define our decision variables, objective function, and constraints via
mathematical representation.
¾ In preparing the spreadsheet, we must translate and clearly define each part of
our model in the spreadsheet. We use referencing and formulas to
mathematically represent the model in the spreadsheet cells.
¾ To enter the decision variables, we list them in individual cells with an empty cell
next to each one. The objective function and constraints are then entered in
different ranges with formulas which reference these decision variable cells.
¾ Remember that it is a good idea to name ranges of the model parts in the
spreadsheet to help with coding efficiency later.
¾ There are two main Solver functions used to input the Solver parameters in VBA.
These are SolverOK and SolverAdd.The SolverOK function has four parameters:
SetCell, MaxMinVal, ValueOf, and ByChange. The SolverAdd function has three
arguments: CellRef, Relation, and FormulaText.
¾ The SolverChange and SolverDelete functions can be used to modify or delete
constraints.
¾ Before entering new input, use the SolverReset function. This function resets all
Solver parameters.
¾ To set Solver options, use the SolverOptions function. This function has several
arguments: AssumeLinear, AssumeNonNeg, MaxTime, Iterations, Precision,
StepThru, Estimates, Derivatives, Search, IntTol, Scaling, and Convergence.
¾ To run the Solver, use the SolverSolve function. There are two arguments for this
function: UserFinish and ShowRef.
¾ Use the SolverFinish function to keep or ignore the Solver results and specify
any reports to generate. This function uses two arguments: KeepFinal and
ReportArray.
¾ The SolverSave, SolverLoad, and SolverGet functions are uses to save, load,
and find information about a set of Solver parameters.
19.6 Exercises
19.6.1 Review Questions
1. What must be referenced by the VBE in order to use Solver functions in a program?
2. Assume that the code below is part of a program that is correctly implemented. What
are the possible values of Result after running the code? What do the possible
values tell you about the solution of running Solver?
18
Chapter 19: The Solver Revisited
1. Add the optional code to the frmParam form cmdOK_Click procedure in the Dynamic
Production Problem application. This code should hide rows on the spreadsheet
based on the number of decision variables chosen by the user.
2. A farmer has 100 acres of land for crops and must decide how many acres to devote
to three different crops: cotton, peanuts, and soybean. The number of acres to devote
must be a multiple of four. Information about cost per acre and revenue per acre is
provided in the table below. Set up the model of this problem and use Solver in VBA
to find the number of acres to devote to each crop that maximizes profit.
3. Imagine that you are a Production Manager for an automobile manufacturing facility,
and you must choose the best combination of paints that minimizes cost without
requiring more time for painting than is available. Each car must be painted with
primer, then a base coat, and finally a clear coat. The cost and time per car is given
for the available paints below. The cheaper paints require more drying time.
4. As the manager of a retail store, you must decide the number of each product to
order so that you maximize expected total profit. For each week, you forecast the
demand for each product for the following week. Each product must be ordered in
batches of five products. For example, if you forecast demand to be six, then you
19
Chapter 19: The Solver Revisited
must order two batches to meet the expected demand. After meeting the expected
demand, any remaining products have an inventory cost of $6 per unit. The purchase
cost per unit for each product depends on the quantity purchased and decreases as
quantity increases. Information for each product is listed in the table below. Assume
that the beginning inventory level is zero for all products. Setup the model of this
problem in a worksheet to use Solver for optimization. Create a program that
performs the following actions:
a. Asks the user to enter the expected demand for each product.
b. Displays the current inventory in a user form and allows the user to modify the
inventory if necessary.
b. Optimizes the quantity of each product to order using Solver commands.
c. Displays a message giving the optimal quantity of each product and the expected
profit.
Distribution Centers
1 2 3 4 Supply
1 20 25 20 65 10
Manufacturing
Facilities 2 75 10 40 35 5
3 50 30 25 40 15
Demand 5 3 13 9
6. Using the following spreadsheet, create the VBA code required to optimize the
production schedule. The situation is as follows: The data within the chart are
referred to as the “Hours Required Per Unit Made.” This means, for product 1, that in
order to make one unit, Dan must work 1 hour, Kathy must work 4 hours, and Ashley
must work 2 hours. The “Hours Available” column lists the maximum number of
hours each worker can be scheduled to work. The “Profit per Unit” row provides
information on the company’s profits earned for each unit of product produced. Write
a program which maximizes company profits given the hourly constraints of the
workers and the time required to produce one unit of each product.
Products
Hours Required Per Unit Made Hours Hours
Product 1 2 3 Scheduled Available
20
Chapter 19: The Solver Revisited
Dan 1 0 3 0 ≤ 15
Worker Kathy 4 6 1 0 ≤ 20
Allison 0 0 5 0 ≤ 35
Ashley 2 4 0 0 ≤ 40
Profit per Unit $695.00 $715.50 $525.75 $0.00
Schedule 0 0 0
Products
Production Cost Per Unit Machine
1 2 3 4 5 Capacity
1 $5.00 $3.00 $7.00 $4.00 - 150
Machines 2 $7.00 - $6.00 $3.00 $8.00 100
3 $5.00 $6.00 $7.00 $5.00 $3.00 150
Daily Production 65 70 70 50 65
Demand (Units)
Products
Production Cost Per Day Machine
1 2 3 4 5 Capacity
1 $325 $210 $490 $200 - 2
Machines 2 $455 - $420 $150 $520 1
3 $325 $420 $490 $250 $195 2
Daily Production 1 1 1 1 1
Demand (Units)
8. Read the following scenario and determine the objective, constraints and modifying
variables in the problem:
A coffee shop would like to order 5lbs of coffee beans this week and 9lbs of
coffee beans next week to keep up with customer demand. Two suppliers have
been approved to sell the beans. The first supplier has an inventory of 7lbs and
is willing to charge $5.70/lb this week and $7.20/lb next week. The second
supplier has 12lbs of beans in inventory and their prices for this week and next
week are $7.35 and $7.90, respectively. The coffee shop needs to determine
how many lbs of coffee to order from each supplier to minimize costs.
21
Chapter 19: The Solver Revisited
Now use the Solver commands to create a program to solve the problem.
9. The state police department needs 12, 8, 6, and 15 officers to work each of the 6-
hour periods in a day, respectively. Officers are hired to work in 12-hour or 18-hour
shifts. They are paid $5/hour for the first 12 hours they work and then $7/hour for
each hour over that.
a. Formulate a scheduling LP problem to minimize the cost of the police
department.
b. Prepare the spreadsheet for this problem and then use VBA Solver
commands to solve it.
c. Make this problem dynamic by allowing the user to modify the input
values for
i. number of officers needed per 6-hour period
ii. pay for first 12 hours
iii. pay for each hour over 12 hours
10. A young investor can invest up to $2000. She wants to invest her money in stocks
and loans. Each dollar invested yields 15 cent profit and each invested in loans yields
a 20 cent profit. At least 25% of all the money invested must be in stocks and at least
$350 must be invested in loans.
a. Formulate a capital budgeting LP problem to maximize her profits.
b. Prepare a spreadsheet and use VBA Solver commands to solve this
problem.
c. Design a user form that:
i. allows the user to change her available investment amount in a
text box
ii. has a scroll bar to determine percent that can be invested in
stocks and another scroll bar for percent to be invested in loans
11. A company knows its demands and production costs for the next three periods
(see table below). It also has a holding cost of $2/unit for all units not sold at the end
of each period. At the beginning of the first period, they have 5 units on hand.
Assume that only one half of the units produced during a period can be used to meet
the current period’s demand.
a. Formulate a multi-period financial LP problem to minimize the cost of
meeting demands for these three periods.
b. Prepare a spreadsheet and use VBA Solver commands to solve this
problem.
c. Design a user form which allows the user to enter another period worth of
data (demand and production cost). Make your program adjustable so
that necessary constraints are added and the objective function is
updated.
22
Chapter 19: The Solver Revisited
is in short supply, however, so the hospital is limited to buying at most 5 gallons from
each company. The companies charge the prices shown in the table below.
a. Formulate a transportation problem to minimize the cost of purchasing the
medicines.
b. Prepare a spreadsheet and use VBA Solver commands to solve this
problem.
c. Make the problem dynamic by allowing the user to do the following:
i. Change the medicine demand per month for the hospital
ii. Change the limited amount per company (these limits to not have
to be the same for each company)
iii. Change the number of companies
iv. Change the number of months (and give demand for new months
and company prices for new months)
13. A manufacturer can sell product 1 at a profit of $3/unit and product 2 at a profit of
$7/unit. Three units of raw material are needed to manufacture 1 unit of product 1 and
6 units of raw material are needed to manufacture 1 unit of product 2. A total of 120
units of raw material are available. If any of product 1 is produced, a setup cost of $10
is incurred, and if any of product 2 is produced, a setup cost of $20 is incurred.
a. Formulate an IP problem to maximize profits.
b. Prepare a spreadsheet and use VBA Solver commands to solve this
problem.
c. Allow the user to modify the input (profit, raw materials, setup cost) per
product
d. Allow the user to change the number of products
14. A product can be produced on four different machines. Each machine has a fixed
setup cost, variable production costs, and a production capacity (see table below). A
total of 2,000 units of the product must be produced.
a. Formulate an IP problem to minimize total costs.
b. Prepare a spreadsheet and use VBA Solver commands to solve this
problem.
c. Allow the user to change the number of machine and the requirements
per machine.
15. Susan is the publisher of a new magazine. The variable cost of printing and
distributing each weekly copy of the magazine is $0.25. She is thinking of charging
between $0.50 and $1.30 per week for the magazine. The estimated numbers of
subscribers for weekly prices of $0.50, $0.80, and $1.30 are given in the table below.
23
Chapter 19: The Solver Revisited
24
Chapter 20
Simulation Revisited
Chapter Overview
20.1 Introduction
20.2 Review of Chapter 9
20.2.1 Defining Simulation
20.2.2 Working with Distributions
20.4 Applications
20.4.1 Game of Craps Revisited
20.5 Summary
20.6 Exercises
Chapter 20: Simulation Revisited
20.1 Introduction
This chapter illustrates to the reader how simulation can be performed, and enhanced, in
Excel using VBA. The reader will learn how to generate random numbers in VBA, make
runs and collect data using loop structures, add animation to their simulation, and
perform and display an analysis of the simulation. Many DSS applications may involve
simulation. We use simulation in our Reliability Analysis, Queuing Simulation, and
Capital Budgeting.
Simulation can handle variables or parameters that are stochastic in nature. It models
stochasticity by generating random numbers from different distribution functions. We
have already seen the RAND function in Excel along with other distribution functions.
The general distribution function has the following format:
For these parameters, the x-value is the number for which we want the distribution and
the cumulative value is TRUE if we use the cumulative distribution function (cdf), or
FALSE if we use the probability mass function (pmf). However, to generate a random
number within a given distribution, we must use the inverse functions of these
distribution functions. These inverse functions return the inverse of the cumulative
probability functions. That is, if probability = DIST(x,...), then DISTINV(probability,...) = x.
Some of the inverse functions of the more common distributions are BETAINV,
BINOMINV, LN or LOGINV (the Exponential inverse), and NORMINV. You can also find
these functions in the Statistical category of the list of functions when we choose Insert >
Function from the menu. The format of these inverse functions is:
=DISTINV(probability, distribution_parameters)
2
Chapter 20: Simulation Revisited
The probability parameter is a number between 0 and 1 associated with the given
distribution. We will use the RAND function as our value for this parameter to generate
some number between 0 and 1. For example, to generate random numbers from the
Normal distribution, we would follow the format:
We can use these functions to generate data which might be common in a given
scenario. We then analyze sets of this data to understand how our problem behaves.
We discussed using the Scenario program in Excel to analyze varying data values in
Chapter 9. However, in this chapter we will focus more on generating random data and
analyzing it.
Summary Excel simulation: use RAND function and inverse functions, DISTINV, to
generate random numbers from a particular distribution
To generate random numbers in VBA, we will use the Rnd function as discussed in
Chapter 14. This function can be manipulated to generate a random number between a
lower bound and upper bound using the formula below (here the value of this random
number is assigned to an x variable):
This formula can also be used to generate a random number from the Uniform
distribution. You can use this formula with the Int function to ensure that this random
number is an integer.
To generate a random number from the Exponential distribution, you can use the Log
function. To do this we just take the logarithm of a random number, using the Rnd
3
Chapter 20: Simulation Revisited
-Mean * Log(Rnd())
This mean value can also be represented by a variable. For example, we may set a
mean variable equal to an InputBox function which prompts the user for a mean of an
Exponential distribution which models their system. We can then use this mean value to
generate the appropriate data. For example, the code below generates 10 random
numbers from an Exponential distribution with a user-specified mean and stores the
values in an array:
For i = 1 to 10
ExpValues(i) = -mean * Log(Rnd())
Next i
There is also a set of VBA functions available for generating random numbers from a
distribution which can be found among the Application.WorksheetFunctions. In this set of
worksheet functions are several of the inverse functions we used in Excel, such as:
NormInv, Ln, BetaInv, ChiInv, FInv, and GammaInv. The general format for using
these functions in VBA is as follows:
Application.WorksheetFunction.DistInv(probability, distribution_parameters)
The probability parameter can be replaced with the VBA Rnd function in order to
generate a random number from the distribution. One of the most common distributions
we will generate random numbers from is the Normal distribution. This would be done
using the NormInv function with the Application object:
We can also prompt the user for the mean and standard deviation values to create the
following dynamic code:
MsgBox "A random number from the normal distribution with mean " _
& mean & " and standard deviation " & stdev & _
"is " & Application.WorksheetFunction.NormInv(Rnd(), mean, stdev)
Normal Application.WorksheetFunction.NormInv
Beta Application.WorksheetFunction.BetaInv
4
Chi-Squared Application.WorksheetFunction.ChiInv
F Application.WorksheetFunction.FInv
Chapter 20: Simulation Revisited
As seen in the code above, we can use For, Next loops to generate multiple data values.
Instead of generating only 10 data values, we may ask the user to specify how much
data they want to use for the simulation. We may also perform some calculations with
the random data we generate.
For example, suppose there is a system which takes as input a number from an
Exponential distribution with lambda 5, and outputs the square of that number. In our
For, Next loop we would need to generate a series of random numbers and also
calculate their square. The user may then want to analyze the distribution of this output.
To simulate this system, we would first prompt the user for the number of runs they want
to perform. A run is a single execution of a series of actions which model the system. We
then need to determine if we will be storing this data and the results of the specified
calculations in arrays or in a spreadsheet. When multiple calculations of data are
needed, which do not require Excel functions, arrays can be easier structures to work
with. They are easier in the sense that range names do not need to be defined and extra
worksheet space does not need to be used. If, however, some other Excel functions,
such as distribution functions, will need to be used with the generated data, it may be
better to store this data in a worksheet.
For this example, we can use the Log and Rnd VBA functions to generate the data in the
Exponential distribution and we can perform the squaring calculation in VBA as well. We
will therefore use arrays. We may perform the simulation for this example using the
following code (we assume all arrays to be Option Base 1):
ReDim Input(runs)
ReDim Output(runs)
For i = 1 to runs
Input(i) = 5 * Log(Rnd())
Output(i) = (Input(i)) ^ 2
Range(“A1”).Offset(i,0).Value = Output(i)
Next i
The input and/or output of a simulation can then be stored in a spreadsheet for the user
to see. For some simulation models, it will be unnecessary to store or show the user the
input values; analysis of the output is usually of most importance to the user.
5
Chapter 20: Simulation Revisited
20.3.3 Animation
Two other VBA methods should be used when creating a simulation program are the
ScreenUpdating and Wait methods of the Application object (as discussed in Chapter
13). The ScreenUpdating method should be set to False before the simulation runs
begin. This reduces screen flickering while the program runs and increase efficiency
(due to decreased running time) if the screen is not updated after every action in the
code. It should be set to True when the runs have been completed.
The Wait method can be used to create some animation of the simulation. For example,
if we are simulating a production line, we may have several parts being received and
shipped from different work stations. Each of these work stations may have a different
distribution for processing time. We can use the functions discussed above to create a
list of generated time values at which a part is finished at each station. That is, if Work
Station 1 processes parts at an Exponential rate of 2, then we could use the following
code to generate times at which parts would leave Work Station 1:
For i = 1 to runs
WorkStation1(i) = 2 * Log(Rnd())
Next i
Here, runs could be equal to the number of parts which pass through the system in one
work day. We can consider each value of our WorkStation1 array to be a time value. We
can then create a cumulative time array using the following:
WorkStation(1) = 0
For i = 1 to runs
CumWork1(i) = WorkStation1(i) + WorkStation1(i – 1)
Next i
Now, we can run a loop to show that a product leaves Work Station 1 at each time value.
To do this, we may highlight some cell which changes position, or disappears and
reappears, every time this action occurs. To create this action, we would pick some
range to highlight (by changing the color with the Interior property) and each time we
loop through a run, we un-highlight this cell and highlight the next cell (using the Offset
property). However, to ensure that the user sees a delay between these actions, we use
the Wait method. [Note that we will now need to move the ScreenUpdating statement
inside the loop so that the user sees the highlighted cell each iteration.] It may be a good
idea to display the time at each event as well. The corresponding code would be as
follows:
For i = 1 to runs
Application.ScreenUpdating = False
Range(“Time”).Value = CumWork1(i)
Range(“A1”).Offset(i-1, 0).Interior.ColorIndex = 0
Range(“A1”).Offset(i, 0).Interior.ColorIndex = 5
Application.Wait(Now + TimeValue(“0:0:03”))
6
Chapter 20: Simulation Revisited
Application.ScreenUpdating = True
Next i
Running this code will create an animated simulation which appears to move a product
from one cell to the next at each iteration. This idea can be modified to create an
animation that better reflects any particular system.
20.3.4 Analysis
The motivation for using simulation is of course to perform some analysis on a system.
There are several ways to analyze the results of a simulation. For the output produced, a
graph can be created, the maximum or minimum can be found, etc. These actions can
be accomplished using the Chart object as seen in Chapter 13, or any of the
mathematical functions shown in Chapter 14.
Another powerful analysis tool available in Excel is the Data Anlaysis Toolpack. We
discussed some of the analysis tools available in this Add-In in Chapter 9; the tool we
discussed in most detail was the histogram. Histograms calculate the number of
occurrences, or frequency, which values in a data set fall into various intervals. When we
created histograms in Excel using the Data Analysis Toolpack, we had to provide
several parameters: an input range of data, a range for bin values (optional), and an
output range for the histogram results. We also had to check if we wanted to show data
labels, create a pareto chart, calculate cumulative values, and display a chart of the
histogram results. The bin range is used to specify the location of the bin values, where
bins are the intervals into which values can fall. If no bin range is specified, Excel
automatically calculates a set of evenly distributed bins.
To create a histogram using VBA, we will use the Application object and the Run
method. The Run method is used to specify the Add-In file from which we will be running
the analysis. The arguments for this method include all of the parameters we specified
when creating a histogram in Excel:
The values for the InputRange, OutputRange, and BinRange parameters should be
ranges. These ranges can be named in Excel or they can be range variables in VBA.
The Labels, Pareto, Cumulative, and Chart parameters all take True/False values.
This simple line of code can be used on dynamic input ranges to create a histogram for
different sets of user data. Note, however, that if you run this code multiple times with
the Chart parameter set to True, you will have multiple charts created on the
spreadsheet. For better code efficiency, we recommend creating a histogram in Excel
first (as we have seen with some Chart applications) and then modifying your code so
that all of the chart option parameters are set to False. As you repeat this code to run a
histogram analysis on different sets of input values, you may see a warning message
that your output range will be overwritten. We therefore recommend adding a line of
code to clear this output range of cells before creating the histogram multiple times.
7
Chapter 20: Simulation Revisited
Other analysis from the Analysis Toolpack may be useful to your application. To
discover the particular code and parameters for each analysis tool, record a macro first
in Excel (most analysis tools will still run an extension of the ATPVBAEN.XLA file).
20.4 Applications
20.4.1 Game of Craps Revisited
We will now rebuild this simulation using VBA to allow us to perform more runs and
include some animation. We will show the user the value of the dice they roll on each
run; each run is defined to be one play of the game with one to five rolls of the dice. After
each roll of the dice, we evaluate the value of the sum to determine if they continue
rolling or stop the game by winning or losing (again, we are assuming a maximum of five
rolls possible for now). We will also display to them whether they won, “Won!”, lost,
“Lost”, or need to continue rolling past the fifth roll, “—“. (see Figure 20.1).
8
Chapter 20: Simulation Revisited
Figure 20.1 The new Craps Simulation has images of the dice and the sum of their
values for each roll.
For each run, we will keep statistics of the run number, if the player won or lost within the
five rolls, and how many rolls they played until this win or loss occurred. We will also
store if they rolled five dice and still needed to continue rolling; that is, no win or loss
occurred in five rolls. We denote this by recording a “—“ instead of “Win” or “Lose” and
we record the number of rolls as 6 (any number greater than 5). In Figure 20.2, we show
the statistics for 20 runs.
When the simulation is completed, we will use these statistics to create two histograms:
the frequency with which a player wins in 1, 2, 3, 4, or 5 rolls and the frequency with
which they lose in the same number of rolls. Notice in the figure that we have copied the
statistics and sorted them by wins and losses to separate the data for the two
histograms. We have also forced the bins to be 1, 2, 3, 4, and 5 by specifying a bin
range when creating the histograms (this range is hidden on the spreadsheet).
9
Chapter 20: Simulation Revisited
Figure 20.2 Statistics are kept for each run and two histograms are created.
Let us now take a look at the initial code. We will begin with a Start procedure. This
procedure will initialize necessary variables, clear previous values, prompt the user for
the number of runs to perform, and call a loop to repeat the actions for this given number
of times. When the simulation is complete, we will also call a procedure to create the
histograms (see Figure 20.3).
We have some range variables we will use for each dice roll. We use these ranges to
paste the dice images, display the sum values, and clear previous data. As an
alternative to some of the range variables we have used in the past, for this application
we have created an array of ranges for our variables. We define the array Rolls() to
contain the five ranges we will use to identify each dice roll. We have chosen to use an
array in this case so that we can refer to the rolls by an index number in later code. That
is, instead of referring to FirstRoll, SecondRoll, etc. as range variable names, it will be
easier for us to refer to Rolls(1), Rolls(2), etc. This way, we can use some loops to
repeat actions that will occur for each roll and to each roll range. We therefore begin our
Start procedure by setting the ranges in the Rolls() array to ranges on the spreadsheet
(offset by 2) using a For, Next loop. We also set references for two more range
variables: Stats and Hist, referring to the beginning of the statistics and histogram tables,
respectively.
10
Chapter 20: Simulation Revisited
We then prompt the user for the number of runs, which we set equal to an integer
variable NRuns. We then use this variable in a For, Next loop to call repeatedly the
procedure for playing the game in each run. This procedure is called PerformSim. We
will also clear the images from the previous run in this loop and cause a slight delay to
enhance the animation. We cause the delay using the Application.Wait method. When
the loop has finished and the simulation is complete, we create the histograms by calling
the CreateHist procedure.
The ClearPrev procedure clears all images, statistics, and histogram data. The
ClearImages procedure clears all previous dice images and sum values. Clearing
images can be somewhat tricky in VBA. We have used the Delete method and
xlShiftToLeft arguments to move all of the images to the first column in the spreadsheet.
We then use the Cut method to remove these images completely and paste them on to
another sheet containing the dice images.
11
Chapter 20: Simulation Revisited
Let us now look at the main simulation procedure, PerformSim (see Figure 20.4). In this
procedure we want to roll the dice and evaluate the sum of the values to determine if
more rolls are necessary. Our criteria for determining a win or loss is different for the first
roll than for the other rolls, so we will separate the two checks. We use an If, Then
statement to determine if the result of the first roll is a win or loss. If this is the case, we
display the result to the user and record the run number and roll number in the statistics.
We then use the Exit Sub statement since no more rolls are needed after a win or loss is
encountered. However, if no win or loss is found after the first roll, we can move on to
checking the second set of criteria for all subsequent rolls. This second criteria will use
the sum value from the first roll as a check for winning. If a win or loss is found in any of
these rolls, the result is displayed, the statistics are recorded, and the run ends just as in
the first roll. If all five rolls are completed without finding a win or loss, then statistics are
recorded as a “—“ with roll number 6.
12
Chapter 20: Simulation Revisited
Figure 20.4 The PerformSim procedure checks for a win or loss after each roll.
For each roll, we will need to perform a repeated set of actions. We will need to generate
a random number between 1 and 6 for each dice, copy and paste their corresponding
images, and find the sum of their values. We have created a function, RollDice to do this
(see Figure 20.5). The only input we need for this function is the roll number. We need
this value in order to paste the dice images and display the sum value in the correct roll
range.
13
Chapter 20: Simulation Revisited
Figure 20.5 The RollDice and GetImages functions are called for each roll.
To copy and paste the images, we will use another function called GetImages. We have
stored some dice images on another sheet which we will copy and paste in the
appropriate spot for each roll (see Figure 20.6). We have arranged these images so that
we can use the offset property with the title range “Images” to find the dice image
matching the number rolled. That is, if the roll was a 1 and 3, then the images we need
can be found by:
Range(“Images”).Offset(0,1)
Range(“Images”).Offset(0,3)
Figure 20.6 The images of all six dice are stored to use for the animation.
Therefore, we need to pass to the GetImages function the value of each dice and the roll
number. We use the dice values to find the images and the roll value to paste the
images. Note that we have used the ActiveSheet.Paste method instead of the
Range.PasteSpecial method since we are working with images.
The loop for checking the criteria of the second through fifth rolls has been simplified by
using the Rolls() range array. We have created the RollDice and GetImages functions
such that all we need is the index of the Rolls() array to modify the appropriate roll
range.
After the rolls are made and statistics recorded for each run, the simulation is complete.
We are now ready to analyze the statistics by creating histograms. The first thing we do
in the CreateHist procedure is copy and sort the statistics data (see Figure 20.7). We
want to define a range for the winning data and one for the losing data to use as input for
the two different histograms. We sort the data and then scan through the list to separate
the status values “Win” and “Lose”. We name the separated ranges “Wins” and “Losses”
respectively.
14
Chapter 20: Simulation Revisited
We can now create the two histograms by using the Application.Run “ATPVBAEN.XLA
!Histogram” statement. The arguments for this statement are: input range, output range,
bin range, and some options for creating labels, charts, pareto, and cumulative values.
The input ranges have just been named, “Wins” and “Losses”, and we use the Hist
range variable to locate the output range. We have created a range called “Bin”, hidden
on the spreadsheet, which contains the bin values 1, 2, 3, 4 and 5 in a column. We will
use this bin range to force the frequency calculations done by the histogram program. As
we recommend when using charts, we will not recreate the histogram charts in the code.
We have already run the histogram program once in the spreadsheet to create the initial
charts. We have formatted them and given them appropriate titles. We can therefore set
the histogram options to all be false, as the histogram output range will not change and
so the charts will be updated each time.
Figure 20.7 The CreateHist procedure manipulates the statistics data and creates two
histograms.
The application is now complete. We can start the code by assigning the Start procedure
to the START button. (Note: Can you extend this application so that it is not limited to
five rolls?)
20.5 Summary
¾ Simulation is a modeling tool which is used to imitate a real-world process in order
to understand system behavior.
15
Chapter 20: Simulation Revisited
20.6 Exercises
20.6.1 Review Questions
16
Chapter 20: Simulation Revisited
1. Extend the Craps Simulation application so that it is not limited to five rolls.
2. Create an interactive program where the computer “flips a coin” and the user must
choose heads or tails. The program should keep track of how many times the
game was played, what the user’s choice was each game, and the game’s
outcome.
3. A coffee shop has three workers preparing coffee orders for a single register.
Customers arrive exponentially with a mean inter-arrival time of 1 minute. There is
a single queue to order a drink, and the workers can complete an order according
to a exponential distribution with mean of 5. The shop is open 16 hours per day.
Simulate the system for a 16-hour day and count the number of orders processed.
4. Companies often want to track their sales data in order to assist them in
forecasting future sales of similar products. Using the following table of “sales
data,” create a forecasting tool that allows the user to input new data (which
should be added to the historical data) and simulate a forecast for a future period
using the data provided.
sales data
1
3
3
5
6
9
5. Create a simulation program in which the computer and user play the card game
of War. Be sure that the user is informed of the status of play throughout the
game. The rules for the game are as follows:
17
Chapter 20: Simulation Revisited
• If both players present a card of the same value, each player places 4 cards
face down in front of them and plays the 5th card to win all 12 cards. This is
called a war.
• Play ends when one player has all 52 cards.
[Hints: Use arrays to set up each player’s deck of cards. The size of each array
will vary throughout the course of the game. Also, when dealing the cards, don’t
forget to shuffle the deck! For ease of coding, call the Jack, Queen, King and
Ace cards by the numerical values 11, 12, 13, and 14.]
c. Moves the object according to the user’s input until finished or the object
cannot move in the given direction. Use a one second delay between
characters.
d. Display an appropriate message.
8. Create a program that allows a user to simulate buying and selling shares of
Finch Mutual Fund. A user may choose to buy or sell shares or to simulate the
next day without any transaction. When making a decision to buy or sell shares,
the user provides the number of shares and then pushes either a “Buy” button or
a “Sell” button. The transaction takes place before the next close of trading. The
share price at the time of transaction differs from the previous closing price
according to a Normal distribution with an average of $0.50 and a standard
deviation of $0.20. The share price has an equal chance of either increasing or
decreasing. The closing price for each day is calculated the same way. The
volume traded each day follows a Normal distribution with an average of 250
shares with a standard deviation of 50 shares. Implement two charts for the user
to view the closing share price and volume traded for the past 30 business days
as each day is simulated.
Use one table to keep information on the closing share price and volume for the
past 30 business days. Use another table to record transactions: shares traded,
18
Chapter 20: Simulation Revisited
bought or sold, total shares held after transaction, any profits or losses. The initial
share price is $42.
9. Create a program that allows a user to simulate a production facility for Micro
Circuits, Inc. which produces circuit boards for computers. The program will
simulate 12-hour work days. The probability of a production failure occurring
during a workday increases each day according to an Exponential distribution with
an average of 0.75%. When a failure occurs, required maintenance is performed
the same day. Preventative maintenance is performed at the request of the user.
On the worksheet, place a command button to allow the user to simulate the next
day and another command button to perform preventative maintenance before the
next day.
Use a worksheet to keep records for each day: day number, number of failures,
maintenance costs, maintenance time, number of hours in production, number of
circuit boards produced, predicted profit for circuit boards produced. Use a chart
to plot the number of failures, number of circuit boards produced, or predicted
profit depending on the command button selected by the user. Assume a
production rate of 30 circuit boards per hour and an expected profit of $35.00 per
circuit board.
10. Six months before its regional conference, the Institute of Industrial Engineers
(IIE) must determine how many rooms to reserve for students in the university
hotel. At this time, IIE can reserve rooms at a cost of $50/room. IIE must pay the
$50 room cost even if the room is not occupied. IIE believes that the number of
students attending the conference will be normally distributed with a mean of
5,000 and a standard deviation of 1,000. If the number of people attending the
conference exceeds the number of rooms reserved, extra rooms must be
reserved at a cost of $80/room.
a. Use simulation to determine the number of rooms that should be reserved
to minimize the expected cost to IIE.
b. Allow the user to resolve the problem with different parameters for the
normal distribution.
c. Allow the user to resolve the problem with different room costs.
11. A drug company ABC is trying to take over another drug company XYZ. The
worth of XYZ depends on the success or failure of several drugs under
development. ABC does not know the actual worth of XYZ, but the current owners
of XYZ do know their actual worth per share. ABC assumes that XYZ’s actual
worth is equally likely to be between $0 and $100 per share. XYZ will accept
ABC’s offer if it exceeds the true worth of the company. If XYZ accepts ABC’s bid,
ABC’s corporate strengths immediately increase XYZ’s market value by 50%. Use
simulation to determine how much ABC should bid.
19
Chapter 20: Simulation Revisited
12. The game Try Your Luck is played as follows: the user picks a number between
1 and 6 and tosses three dice. If the number does not appear, the user loses $1.
If the number appears x times, the user wins $x.
a. Develop an animated program for this game. Create a small user form with
a spin button to allow the user to choose his number. Use dice images (as
in the Craps Simulation) to illustrate the random toss of three dice.
b. Determine, on the average, how much money will the user win or lose on
each play of the game.
13. A soda company, KOOLOFF, is giving away a prize by selling bottles with letters
under the bottle caps. To win the prize, you must collect caps with letters which
spell the company name. In production, letters are randomly selected and placed
on the bottle caps. Create a program which does the following:
a. Animate the selection of bottle cap letters. Keep selecting bottle caps until
all six of the necessary letters have been collected. (Letters are randomly
and independently selected from the alphabet to be inserted into the bottle
caps.)
b. Perform this simulation several times (without the animation) to determine,
on the average, how many sodas need to be bought in order to win the
prize.
c. How much would the prize need to be worth in order to play the game
without loosing money on all the sodas bought? (Assume that each soday
costs $1.00.)
15. Two leading pharmacy stores, EZ and WD, are trying to be top in the market.
Each week, a usual pharmacy store shopper goes to EZ once or WD once. If a
shopper’s last visit was to EZ, then this shopper’s next purchase will be at EZ with
probability 0.9; otherwise they will go to WD. Similarly, if a shopper’s last visit was
to WD, then this shopper’s next purchase will be at WD with probability 0.8;
otherwise they will go to EZ. Currently, half of the pharmacy store shopping
population visit EZ and half visit WD.
a. Simulate one year of sales an estimate each store’s average weekly market
share. Assume that the total market size is 1,000 customers. (Hint: use the
Binomial distribution.)
b. Animate this simulation by having an image for each store and keeping
track of the number of weekly visitors to the store below it.
20
Chapter 21
Chapter Overview
21.1 Introduction
21.2 Creating Pivot Tables with VBA
21.3 Using External Data
21.3.1 Importing Data
21.3.1.1 Text Files and Webpages
21.3.1.2 Databases
21.3.2 Performing Queries with SQL
21.5 Applications
21.5.1 Transcript Query
21.6 Summary
21.7 Exercises
Chapter 21: Working with Large Data Using VBA
21.1 Introduction
This chapter explains how large amounts of data can be imported or exported using
VBA. This is a very useful tool for developing DSS applications in industry settings as
most of the data is large and stored in text files or databases. It is also useful for
performing dynamic web queries for a live portfolio DSS or other similar business
applications. Although working with databases may be useful for developing a DSS, we
do not teach databases in this text. We do give a review of some SQL commands that
are used in database queries, but we expect the reader to have some previous
knowledge of databases or to refer to another text. We have several DSS applications
which allow the user to import text files for historical data such as Stochastic Customer
Forecasting and Inventory Management. We also have DSS applications which use
databases such as the University Information Systems DSS.
First, let us review the pivot table example used in Chapter 6 to use as a reference. In
this example, we had the shipping costs for varying maximum weights and days to arrive
for two different shipping companies (see Figure 21.1).
2
Chapter 21: Working with Large Data Using VBA
Figure 21.1 The shipping pivot table data is in the “Data-Shipping” worksheet.
In the pivot table we created, the row fields were “Days to Arrive” and “Max Weight, lbs”,
the column field was “Shipping Companies”, and the data field was “Cost”. In this pivot
table, we also added a calculated field for minimum costs (see Figure 21.1).
Figure 21.2 The pivot table for the shipping data is in the “Pivot-Shipping” worksheet.
3
Chapter 21: Working with Large Data Using VBA
Now, let us discuss the pivot table objects used in VBA. The main pivot table object is
PivotTables. Pivot tables are used similar to the Chart and Shape objects in that we
must use the ActiveSheet object before specifying a PivotTables object. For example, to
refer to the above pivot table named “PivotTable1”, we would use:
ActiveSheet.PivotTables(“PivotTable1”)
From this PivotTables object, we can further specify PivotFields, and from PivotFields
we can specify PivotItems. For example, in the pivot table above, to refer to the “Days
to Arrive” field, we would type:
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Days to Arrive”)
Likewise, to refer to the item “4” in that particular field, we would type:
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Days to Arrive”).PivotItems(“4”)
Note that instead of using the ActiveSheet object, you can also use the Worksheets
object to specify different sheets. For example:
Worksheets(“Report”).PivotTables(“PivotTable1”)
Each of these objects, ActiveSheet, PivotTables, PivotFields, and PivotItems, have their
own set of properties and methods which can be used to create or modify a pivot table.
We have selected a few properties and methods which we feel are most important.
We will begin with the ActiveSheet object. For this object, we primarily want to use the
PivotTableWizard method. This method is what we will use to create our pivot tables.
The PivotTableWizard method has several arguments: SourceType, SourceData,
TableDestination, and TableName. The SourceType argument specifies if our pivot
table data is from the spreadsheet (xlDatabase), an external source (xlExternal), multiple
ranges (xlConsolidation), or another pivot table (xlPivotTable). We will primarily use data
from spreadsheets and external databases. The SourceData argument is used to specify
the specific data from this source type. For example, if we are using data from a
spreadsheet, the SourceData argument value would be the range of values from the
spreadsheet we are using. The TableDestination argument specifies a range where the
table should be placed. The TableName argument can be used to give a name to this
table, such as “PivotTable1”. There are some other optional arguments for this method,
such as RowGrand and ColumnGrand which we will discuss later. To use the
PivotTableWizard method to produce the pivot table example above, we would type:
ActiveSheet.PivotTableWizard SourceType:=xlDatabase,
SourceData:=Worksheets(“Data-Shipping”).Range(“B3:E27”),
TableDestination:=Worksheets(“Pivot-Shipping”).Range(“A3”),
TableName:=“PivotTable1”
Once the pivot table has been created, we must specify its layout; that is, the row fields,
column fields, and data field. The method used to do this is AddFields. This method is
used to set row fields, column fields, and page fields of the pivot table. Each field is an
optional argument of the method: RowFields, ColumnFields, and PageFields. To enter
4
Chapter 21: Working with Large Data Using VBA
these argument values, simply type the name of the field. If there is more than one field
for a particular argument, then use the Array function to specify them. There is another
argument of this method called AddToTable which takes True/False values to
determine if these new fields should be appended to the current table or replace existing
fields, respectively. We will usually keep this value as True (the default). For example,
after creating the above pivot table using the PivotTableWizard method, we would then
type the following to specify our fields:
For the PivotTables object, there are several other properties and methods to discuss.
We will begin with the RowGrand and ColumnGrand properties which were mentioned
above. These properties specify whether or not grand totals should be calculated for row
or column fields, respectively. The possible values for these properties are True or
False. For example, in the above pivot table, to add cost totals for the row fields “Max
Weight, lbs” and “Days to Arrive”, we would type:
ActiveSheet.PivotTables(“PivotTable1”).RowGrand = True
Another property that may be used often is the Format property. This can be used to
apply a pre-defined report or table format to your pivot table. The values for this property
can be an xlReport value or xlTable value. We suggest recording a macro which
applies the format of your choice to your pivot table in order to find the number of the
report or table format you want. Two examples are:
ActiveSheet.PivotTables(“PivotTable1”).Format xlReport10
ActiveSheet.PivotTables(“PivotTable1”).Format xlTable2
A useful method of the PivotTables object is the RefreshData method. This method is
equivalent to pressing the exclamation point icon on the Pivot Table Toolbar. If any
changes are made to the data from which the pivot table was created, refreshing the
data will update the pivot table data. There are no arguments for this method; just simply
type:
ActiveSheet.PivotTables(“PivotTable1”).RefreshData
One last useful method of the PivotTables object is GetPivotData. This method has the
same functionality as the GETPIVOTDATA function defined in Chapter 6. For a specific
item in a given row or column field, this method will find the corresponding value from
the data field. The format of this method is as follows:
ActiveSheet.PivotTables(“PivotTable1”).GetPivotData(“DataFieldName”,
“RoworColumnFieldName”, “ItemName”)
If only one row or column field is given with a paired item name, then this method will
return a grand or sub total value from the data field. However, if more than one row or
column field is given, then the method will narrow down the search as much as possible
to return the specific value from the data field. For example, in the above pivot table, to
find the minimum cost for shipping with the International Route company for a package
whose maximum weight is 5 pounds, we would type:
5
Chapter 21: Working with Large Data Using VBA
ActiveSheet.PivotTables(“PivotTable1”).GetPivotData(“Costs”, “Shipping
Companies”, “International Route”, “Max Weight, lbs”, “5”)
Since our pivot table calculated fields are currently set to find the minimum of the costs,
the value returned from this statement would be $0.50 (the minimum cost in the
International Route column for a Max Weight of 5 pounds).
Let us now discuss some useful properties of the PivotFields object. We will start with
the Orientation property. We have set the row fields, column fields, and page fields
using the AddFields method of the PivotTables object; however to set the data fields of a
pivot table, we must use the Orientation property of the PivotFields object. We can
actually not only set the data field with the Orientation property, but the other fields as
well. This property takes the values xlDataField, xlRowField, xlColumnField, and
xlPageField or the respective fields. For example, to set the data field of the above pivot
table, we would type:
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Cost”).Orientation = xlDataField
There is another possible value for the Orientation property which is xlHidden. This will
hide all of the values of the specified field. This property can be useful not only to set the
data field, but also to change any previously set fields to be different field types or to be
removed from the pivot table all together.
There are two properties which can be used to make calculations (sum, average, min,
max, etc). These are the Function property and SubTotals property. The Function
property is used for data fields. To use this property simply specify the type of calculation
you want to be made on the named field. For example, to create the Minimum Cost
calculated field in the above pivot table, which shows the minimum of each row of costs,
we would type:
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Cost”).Function = xlMin
The SubTotals property is used for non-data fields. With this property you must specify
an index number, or numbers, which represent the type of sub totals you want to show
for the given field. These index values are 2 = sum, 3 = count, 4 = average, 5 = max, 6 =
min, and others. For example, to show the minimum cost for each “Max Weight, lbs”
value, we would type:
These properties are very useful for identifying important values in your pivot table.
There is one more object which we would like to discuss: PivotItems. There is one main
property that is used often with this object which is the Visible property. Using this
property is similar to clicking on the drop-down list of values for a field in a pivot table
and checking or un-checking the values which you want to be displayed. The values for
this property are True and False, much like we have seen in uses of the Visible property
with other objects. For example, in the above pivot table, if we only want to see the costs
for “Days To Arrive” values 1 and 8, we would type:
6
Chapter 21: Working with Large Data Using VBA
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Days To Arrive”)
.PivotItems(“1”).Visible = True
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Days To Arrive”)
.PivotItems(“8”).Visible = True
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Days To Arrive”)
.PivotItems(“2”).Visible = False
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Days To Arrive”)
.PivotItems(“4”).Visible = False
One last useful property is the ShowPivotTableFieldList property which is used with a
Workbook object. This property has True or False values which can be set to show or
hide the pivot table field list of the pivot tables in the workbook. We may type the
following for example:
ActiveWorkbook.ShowPivotTableFieldList = True
A complete procedure for creating the shipping costs pivot table is shown in Figure 21.3.
In this procedure, we have created the pivot table, specified the layout (row fields,
column fields, and data field), opted to show row grand totals, applied formatting, added
a min cost sub total, used the GetPivotData function, and hidden the fields list. Other
options may have been set using the properties and methods explained above.
7
Chapter 21: Working with Large Data Using VBA
Again there are several other properties and methods for these pivot table objects,
however we feel the ones we have discussed here are the most useful for developing
applications.
Charts.Add
ActiveChart.SetSourceData Source:=Worksheets(“Pivot-Shipping”).Range(“A3:E21”)
Pivot charts are very useful and are easily created in VBA. You can then use previously
discussed Chart properties and methods to further edit the pivot chart.
8
Chapter 21: Working with Large Data Using VBA
We will first describe how to import data from text files and webpages in VBA. We will
use an object called QueryTables. This object is referred to using a Worksheet object.
For example:
ActiveSheet.QueryTables
To import data, we will simply add a QueryTable object using the Add method. The Add
method has two arguments: Connection and Destination. The Connection argument
requires the type of data being imported and the actual location of the data. It is using
this argument that we will clarify if we are importing data from a text file or a webpage. If
we are importing data from a text file, we would define the Connection argument as
follows:
Here, the path is the actual location of the text file on your computer given by some
string value. For example, the path could be C:\MyDocuments\textfile.txt. The path value
can also be given dynamically by prompting the user for the path value and storing the
path name in a string variable. This path value would have to be concatenated with the
TEXT specification. For example, you may use the following code to specify a dynamic
value for the Connection argument:
In creating dynamic imports, you may prefer to let the user browse for a file rather than
enter the path. To display an explorer browse window, we use the GetOpenFilename
method associated with the Application object. This method presents the user with a
browse window and allows them to select a file. The name of the file is returned as a
string value. This method has the following format:
The FileFilter argument gives you the option of limiting the type of file the user can
select. For example, since we may be using this method to allow the user to select a text
file to import, we may want to limit their browsing to text files only. To do this, we would
set the FileFilter value to “Text Files (*.txt), *.txt”. We will ignore the FilterIndex and
ButtonText arguments. The Title argument allows you to give a title to the browse
window that will appear. For example, you may want to title the window “Select a file to
import.” The MultiSelect has the values True or False to determine if a user can select
more than one or only one value, respectively. Consider now the code below as an
alternative to finding the path value for a user text file to import.
9
Chapter 21: Working with Large Data Using VBA
If we are importing data from a webpage, we would define the Connection argument as
follows:
Here, the actual URL is the URL of the website. For example, the URL might be
http://www.ise.ufl.edu. Again, this value could be taken from the user dynamically.
The Destination argument is the location on the spreadsheet where you would like to
place the imported data; the value for this argument is simply a range. Columns and
rows will be created for the data appropriately. The output range for the entire table of
data will begin in the Destination range. For example, to import and output a text file to a
range beginning at cell A1, we would define the Destination argument as follows:
Destination:=Range(“A1”)
When using the Add method for importing text files and webpages, we must also define
a few properties for the import to be complete. These properties differ for importing a text
file and a webpage.
The necessary properties for importing a text file basically describe how the text is
organized in the file so that the values are imported correctly. The first property to set is
the Name property; this simply gives a name to your imported file so that you can refer
to it for future modification if necessary. The next two properties are FieldNames and
RowNumbers. These properties take True/False values to determine whether or not
there are field names in the first row or enumerations in the first column which should be
ignored when the data is imported; these values will be ignored if the value of these
properties is set to True.
The next four properties determine how the text data is organized. The first of these is
the TextFileStartRow property. This value should be equivalent to the row number on
which the data you want to import begins. The next property is TextFileParseType. The
value for this property is either xlDelimited or xlFixedWidth. We will normally work with
delimited files. When working with delimited files, the next two properties apply:
TextFileTextQualifier and TextFile__Delimiter. We will usually set the text qualifier to
xlTextQualifierDoubleQuote. The TextFile__Delimiter property can be one of the
following properties: TextFileConsecutiveDelimiter, TextFileSemicolonDelimiter,
TextFileCommaDelimiter, TextFileSpaceDelimiter, TextFileTabDelimiter. Each of
these properties takes True/False values. The default values for these properties is
False, so it is only necessary to specify the property which you want to set to True; that
is, you only need to specify which delimiter you are using.
There is also one method that must be defined to complete the import. This is the
Refresh method. This method has one required argument called BackgroundQuery
which takes the values True/False. We will usually set this argument to False implying
that the background query (in this case, the text file) does not need to be refreshed when
changes are made to the imported data. Below is the complete code necessary to import
a text file. We have used a comma delimiter for this example.
With ActiveSheet.QueryTables.Add
10
Chapter 21: Working with Large Data Using VBA
(Connection:=“TEXT;C:\MyDocuments\textfile.txt", Destination:=Range("A1"))
.Name = "ImportTextFile"
.FieldNames = True
.RowNumbers = False
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With
Let us consider a simple example. Suppose we have a text file of numerical data
separated by commas (see Figure 21.4). To import this text to Excel, we can use the
code given above. We have specified the path appropriately and noted that we intend to
use commas as the delimiter (see Figure 21.5).
11
Chapter 21: Working with Large Data Using VBA
The imported text is then separated into columns respective of the comma locations in
the original text file. The resulting table begins in the range specified in the Destination
argument; here, cell A1.
Property/Method Values/Arguments
Summary Add Connection:=”TEXT; path”, Destination
Name name of query
FieldNames True/False
RowNumbers True/False
TextFileStartRow row number of text file from which to start importing
TextFileParseType xlDelimited
TextFileTextQualifier xlTextQualifierDoubleQuote
TextFile___Delimiter delimiter type, True/False
Refresh BackgroundQuery:=False
The basic properties needed for importing a webpage also include Name, FieldNames,
and RowNumbers as described above. There is then a set of five properties specific to
importing data from a webpage. The first of these is WebSelectionType. This property
is used to determine how the data is organized on the webpage. We usually use the
value xlSpecifiedTables. The next property is WebFormatting. We usually set this
property value to xlWebFormattingNone so that the formatting from the webpage is not
transferred to the spreadsheet. You can ignore this property if you want the webpage
formatting to be imported with the data.
12
Chapter 21: Working with Large Data Using VBA
The next property is WebTables. This property is the most important to specify as it
determines which table of data from the webpage will be imported. As we saw in
Chapter 10, when importing data from a webpage, several areas are blocked as possible
“tables” for importing; these areas are marked with yellow arrows. In VBA, we have to
specify which arrow we want to click, that is which area we want to select for importing.
You will have to research this ahead of time by either recording a macro or counting the
areas shown in the wizard for importing webpages. This may seem to hinder the
functionality of importing a webpage in VBA; however, that is not necessarily the case.
The VBA code for importing webpages can be used to refresh a previously made import
with the latest data. That is, you may create the first webpage import, check the
WebTable value, set the formatting for the import area, and make other preparation
steps. Then, we you run your VBA procedure for refreshing the import, you can add a
line of code which clears the contents of the initial area and imports the most current
data from the same webpage table.
With ActiveSheet.QueryTables.Add
(Connection:= "URL;http://www.webpage.com",
Destination:=Range("C1"))
.Name = “WebpageQuery1"
.FieldNames = True
.RowNumbers = False
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "1"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.Refresh BackgroundQuery:=False
End With
Let us consider an example. Suppose we wish to import into Excel the most recent stock
quotes for MSFT. We use the “Quicken” webpage shown in Figure 21.7.
13
Chapter 21: Working with Large Data Using VBA
We write a procedure using the code given above. We specify the WebTables value to
be “30” based on the desired area of information we wish to import (again, we find this
by running a temporary query using the import wizards in Excel). See Figure 21.8 for the
importing procedure.
The imported data is separated into columns according to the delimiters used on the
webpage (see Figure 21.9). This table begins in the range specified as the Destination
argument value; here, this is cell C1. This query can be run multiple times to retrieve the
most current stock values from this webpage.
Figure 21.8 Code to import webpage data using the QueryTables object.
Property/Method Values/Arguments
Summar Add Connection:=”URL; actual URL”, Destination
y
Name name of query
14
FieldNames True/False
RowNumbers True/False
Chapter 21: Working with Large Data Using VBA
21.3.1.2 Databases
Let us now discuss how to import data from a database. There are two main systems
used in VBA for communicating with databases as external data sources. One such
system is called Data Access Object (DAO). This set of objects is used to import and
manipulate data, primarily from databases. To use DAO, it must first be referenced in the
VBE using the Tools > References menu options. The second system for using external
data is called ActiveX Data Objects (ADO), which also imports and manipulates data
from databases. Both ADO and DAO use Object Database Connectivity (ODBC) to
securely access data from databases. We have found that ADO objects are much
simpler to use than DAO objects; therefore, we only discuss ADO in detail.
There are two main ADO objects used to import data: Connection and Recordset. The
Connection object establishes the communication to a particular database. There are
two main methods used with this object: Open and Close. The Open method uses a
ConnectionString argument to define the path to the database. The Close method does
not have any arguments. A Connection should be opened and closed every time a query
or import is made from the database.
Note: Similar to working with User Form objects, we recommend using some initial
variable name letters to denote the type of object the variable represents; here we name
our Connections starting with “cnt”. Now, we need to define the data provider, or
15
Chapter 21: Working with Large Data Using VBA
database type, and data source, or filename, of this connection. These values will be
given to the ConnectionString argument of the Open method. The data provider we will
usually use can be defined as: “Microsoft.Jet.OLEDB.4.0”. The data source should be
the filename of the database plus the path of the file. You can either type the entire path
as a string value, for example “C:\MyDocuments\Databases\MyDatabase.mdb”, or you
can use a string filename value and the Path property of the Workbook object, more
specifically ThisWorkbook object. For example, here we define a String variable and
create the entire data source value:
Notice again that we use some variable name which denotes that a database path is
represented: “db”. Now we have the data provider and data source; we can either assign
these values directly to the ConnectionString argument or we can use a String variable.
The ConnectionString argument value has two sub arguments named Provider and
Data Source for the data provider and data source, respectively. We will use these sub
arguments in the value of our String variable which we will then use in the
ConnectionString argument; notice that we are using the data source variable we
defined above:
Now we have defined our Connection and ConnectionString argument for the Open
method using data source and connection string variables. We can now apply the Open
method as follows:
cntMyConnection.Open ConnectionString:=CnctSource
To close a Connection, simply use the Close method with your Connection variable. We
would close the Connection we defined as follows:
cntMyConnection.Close
We use the Set statement again after closing a Connection to clear the Connection
value; this time however, we set the Connection to Nothing.
16
Chapter 21: Working with Large Data Using VBA
cntMyConnection.Open ConnectionString:=CnctSource
At the end of each procedure in which we have used a database connection, we should
type the following lines of code:
cntMyConnection.Close
Set cntMyConnection = Nothing
cntMyConnection.Close
Set cntMyConnection = Nothing
We can now discuss the second main object in ADO, the Recordset object. The
Recordset object is used to define a particular selection of data from the database that
we are importing or manipulating. We will again use a variable to represent this object
throughout the code; to define Recordset object variables, we use the
ADODB.Recordset data type. We again use the Set statement to assign the value to
this variable as a New Recordset.
Notice that we use the letters “rst” for the Recordset variable name. Similar to the
Connection object, the Recordset should be opened before it is used; we do this using
the Open method. The arguments for the Open method of the Recordset object are
Source and ActiveConnection. The Source argument defines the data that should be
imported. The Source value is a string which contains some SQL commands; we define
and discuss SQL in the next section. Similar to the data source value and
ConnectionString value discussed above, we can use a String variable to define these
SQL commands to use as the value of the Source argument. For example, we may type
the following:
Again, we will discuss the SQL commands in the next section. The ActiveConnection
argument value is the name of the open Connection object you have previously defined.
To open the Recordset we defined above using the previously defined Connection, we
would type:
17
Chapter 21: Working with Large Data Using VBA
Using the Open method of the Recordset object only selects the data that should be
imported (or queried upon importing using SQL). To copy this data to the Excel
spreadsheet, we use the Range object and a new method: CopyFromRecordset. This
method only needs to be followed by the name of the Recordset variable you have just
opened. For example, to copy the data from the Recordset defined above to a range in
our spreadsheet, we can type:
Range(“A1”).CopyFromRecordset rstFirstRecordset
When we are done using this Recordset, we should clear its values; we do this using the
Set statement with the value Nothing. For example, to clear the above Recordset values,
we would type:
You may use different Recordset variable names in each procedure, even though the
Connection variable names can remain the same for all procedures if you are using the
same database (cntMyConnection, dbMyDatabase). The string variable values can also
remain the same throughout the module (CnctSource, Src).
18
Chapter 21: Working with Large Data Using VBA
the function; that is, in every procedure with a query, we will define the Src variable
value and then call the function. We can also use a string variable or value for the output
range. We have defined this function, which we call QueryData, below:
Structured Query Language (SQL) is the code used to perform queries, or filter the
data which is imported. As mentioned above, SQL commands are used to define the
Source argument of the Open method with the Recordset object. You can define the
Source to be all values in a particular database table or pre-defined query (that is
basically a direct import of data), or you can create a query as the value of the Source
argument.
SQL is a complex language. You may find many books on using this language to
perform advanced queries and manipulating Access objects (see Developing Web-
Enabled Decision Support Systems by Pol and Ahuja). In this section, we aim to give
only a brief overview of how SQL can be used to create queries.
For the definitions and examples in this section, we will use a table from a University
System database. This table, called tblStudents, contains student names, IDs, and
GPAs.
19
Chapter 21: Working with Large Data Using VBA
Figure 21.10 The table tblStudents contains student IDs, names, and GPAs.
The basic structure of SQL commands is: a statement which specifies an action to
perform, a statement which specifies the location of the data on which to perform the
action, and a statement which specifies the criteria the data must meet in order for the
action to be performed. Some basic action statements are SELECT, CREATE, and
INSERT. There are others which we have included in a table below. For now, we will just
discuss the SELECT statement.
The SELECT statement selects a specific group of data items from a table or query in
the database. The phrase appearing immediately after the SELECT statement is the
name or names of the fields which should be selected. For example, if we want to select
the names of all the students in the table above, we would use the SELECT statement
followed by the field in which the names are stored, in this case “StudentName”. We
would type:
This query would return all of the student names shown in the table (refer to Figure
21.10). Note: To select everything in a table, that is all fields, use the asterisks mark (*)
after the SELECT statement. We must also specify the location of this field, that is the
table or query title from the database. We do this using the FROM statement. In our
example, we are selecting the student names from tblStudents.
Note also that no quotation marks are used for the field and table names. We can also
include a criteria filtering in the query. The most common criteria statement is WHERE.
The WHERE statement can use sub statements such as <, >, = for value evaluations, or
BETWEEN, LIKE, AND, OR, and NOT for other comparisons (see table below for
examples). In the same example above, suppose we only want the names of students
who have GPAs greater than 3.5. We can therefore specify our criteria using the
WHERE statement and > sub statement as follows:
20
Chapter 21: Working with Large Data Using VBA
Here GPA is the name of the field which contains GPA values. This query would return
only the student names which match the criteria (from the figure shown, these names
would be: A. Berkely, K. Lennox, V. Walace, and Y. Zaals).
Other criteria statements include GROUP BY and ORDER BY. ORDER BY can be used
with the WHERE statement to sort the selected data; this data can be sorted in
ascending or descending order using the statements ASC or DESC respectively. For
example, if we had selected both the name field and GPA field of the “tblStudent” table,
we may want to sort the final data by names alphabetically or by GPA values. Below, we
type the statement to sort the data by GPA values in descending order:
SELECT StudentName, GPA FROM tblStudents WHERE GPA > 3.0 ORDER BY
GPA DESC
In a SELECT statement, we can also perform a simple aggregate function. Simply type
the name of the function after the SELECT statement and list the field names which
apply to the function statement in parenthesis. One common function statement is
COUNT. Using SELECT COUNT will return the number of items (matching any given
criteria) instead of the items themselves. For example, the following command would
return the number of students with a GPA greater than 3.5:
For the table shown in Figure 21.10, the result of this query would be 4. Other functions,
such as MIN, MAX, and AVG, work in a similar manner. For example, to find the
average GPA of the list of students in the given table¸ we would type:
The result of this query would be 3.34. There are many other SQL statements that can
be used to filter or manipulate your data. Again, we do not discuss these in detail in this
book. We will discuss next how these SQL statements can be used to make our queries
dynamic. This is accomplished by incorporating variables into the SQL statements.
In VBA, SQL statements always appear as a string; that is, they are enclosed by
quotation marks. If your criteria checks for a particular string value, you must use single
quotation marks to state that value. For example, say we wanted to find the social
security number of a student from the “tblStudents” table if we know their name. If the
field name of the social security number is SSN and the name of the student is “John
Doe”, we would type:
Now suppose instead of specifying our own criteria, we want the user to determine
which name to search for. We can use an Input Box and a variable, in this example
called StudName, to prompt the user for this value. Then we can include this variable in
place of the criteria value in the SQL statement. The new statement would be:
Note that we have to include the single quotation marks around the criteria value;
therefore, we have concatenated the variable name followed by the ending single
quotation mark.
Now let us incorporate these SQL statements into our database query code. As
mentioned in the previous section, we will use a string variable to assign the value of the
SQL commands. We will then use this variable in the Source argument of the Open
method of the Recordset object. For example, let us search for John Doe’s social
security number as the value of the Recordset defined previously.
The value in range A1 would now be the GPA value of O. Peterson from the tblStudents
table in the database used in our Connection. To make this query dynamic, we can use
the variable described above with an Input Box as follows:
22
Chapter 21: Working with Large Data Using VBA
Range(“A1”).CopyFromRecordset rstFirstRecordset
Variables can be used to make queries dynamic with Input Boxes, User Forms, or simply
by taking values the user has entered to a spreadsheet. These variables can then be
placed in the statements of SQL commands in the code to search for the user’s criteria.
Even though we mention the AccessLinks Add-In for exporting data to a database in
Chapter 10, there does not seem to be corresponding VBA code for this Add-In. Even
after recording a macro, the Add-In functionality is not coded. However, we can place
data into a previously created Access database using the CREATE and INSERT SQL
commands.
The CREATE statement can be used to create a new table in the database. The
corresponding location statement for the CREATE command is TABLE. The name of the
new table is given after the TABLE statement. The name of the table is followed by the
name of the fields for the new table; these are listed in parenthesis with a description of
the data type the field should hold. You must also include a CONSTRAINT command to
specify the primary key of the table. You would give a name to this key, specify that it is
the PRIMARY KEY, and then list the selected field. For example, to create a new table
for course information, called tblCourses, we may write the following SQL statement:
Once you have created a table, you can use the INSERT statement to enter values for
each field. The INSERT statement is always followed by the INTO location statement.
The name of the table into which you are entering values is listed after the INTO
statement. The field names for which you are entering values should then be listed in
parenthesis; that is, you may not want to enter values for all fields. Then the values are
listed after a VALUES statement in the same order in which the corresponding fields
were listed. For example, if we wanted to enter new values into the table tblCourses
created above, we may type:
Using the INSERT command, you can loop over several user values from a spreadsheet
or from an array value and insert each set of values into the fields of a new table or
previously created table. This is the best way to export data to a database from Excel.
Note: You can also use the UPDATE statement to change values in a previously created
table. The UPDATE statement uses the SET location statement and the same criteria
statements used with the SELECT command. For example, to update the GPA value for
the student Y. Zaals, you could type the following:
A chart can be exported as an image file using VBA. There is a method of the Chart
object call Export which allows this to happen. The Export method has three arguments:
FileName, FilterName, and Interactive. The FileName argument is the name you wish
to give to the exported image file. The FilterName is the type of image file: “GIF”, “JPG”,
etc. The Interactive argument decides whether or not to display a dialog box in which
filter options can be specified. The values for this argument are True or False; the
default value is False. For example to export a chart created in Excel as an image file,
we would type:
Worksheets("Sheet1").ChartObjects(1).Chart.Export FileName:="Chart1",
FilterName:="GIF"
21.4 Applications
21.4.1 Transcript Query
We will develop an application which performs dynamic database queries using a pre-
developed Access database. This database contains information on students, faculty,
courses, sections, and grades; there are six tables and one query (see Figure 21.3).
Figure 21.11 The Access database “UniversityInformationSystem” has six tables and
one query.
In this application, we will allow the user to query the database to retrieve transcript data
for a particular student. This transcript data will include every course the student has
24
Chapter 21: Working with Large Data Using VBA
taken with the details of the course and section as well as the grade they earned. We will
then evaluate all grades to calculate the selected student’s overall GPA. An example of
a result of this query is shown in Figure 21.4.
Figure 21.12 The transcript query shows the course and grade information for a
selected student.
We will use the query function described above in section 21.1.2.2 to define the
Connection and Recordset objects used in the query; we call this function QueryData
(see Figure 21.5). We have added two more parameters, i and j, to this funciton which
are used with the Offset method for the output range. The need for these extra
parameters will become clear when we discuss the central transcript query code.
Figure 21.13 The QueryData function is used to define the Connection and Recordset
for each query.
Let us now look at our Main sub procedure (see Figure 21.6). In this procedure we will
clear previous data, show the user a form from which they can select a student, and then
call another procedure which will find the transcript data and calculate the GPA for the
selected student. Before we can show the user a form with a list of students, we need to
make a query to the database to find the name of all the students. We do this using the
CreateStudentList procedure (see Figure 21.6).
25
Chapter 21: Working with Large Data Using VBA
We will query the database in the tblStudent table which contains the complete list of
student names (see Figure 21.7).
Figure 21.15 The table “tblStudent” contains the list of the names of each student.
We will output the result of this query to a temporary sheet called “Temp” in the first cell,
A1. We therefore define the Src and OutputRange variables accordingly and then call
our QueryData function; the i and j parameters are set to 0 for this query. Once the
query has output the list of names to the “Temp” sheet, we will select the entire list,
26
Chapter 21: Working with Large Data Using VBA
using the End property, and name it “StudList”. We can then use this range name to
define the RowSource of the combo box on the user form (see Figure 21.8).
Figure 21.16 This form contains a combo box with a list of all students in the database.
In the code for the form, we use the Initialize event of the UserForm to set the
RowSource to “StudList” (see Figure 21.9). For the cmdOK_Click procedure, we do a
simple error check to ensure the user has made a selection, and then we set the
selected student name to the string variable StudName.
The combo box will now contain the current list of students from the database (see
Figure 21.10). This query is important to make each time the user wants to see a new
transcript to ensure that the list of students reflects the current data in the database. This
way, if any changes are made to the database, the form will always have the most up-to-
date list of students.
27
Chapter 21: Working with Large Data Using VBA
Figure 21.18 The combo box list of students is the most current list from the database.
The only code remaining now is the main transcript query, which we have placed in a
procedure called Transcript (see Figure 21.14). The first set of values we will collect from
the database are the students grades and section number for each course they have
taken, these are found in qryCourseID (see Figure 21.11).
Figure 21.19 The query “qryCourseID” has the course ID and section number for all the
courses each student has taken.
28
Chapter 21: Working with Large Data Using VBA
This is a dynamic query in that we are using criteria dependent on the user’s input. That
is, we are searching for the grade, section number, and course information from the
qryCourseID in each case where the student name value is equal to the name selected
by the user from the form.
We will output this query to first section of the result table, in a cell we have named
“TransTBL” (see Figure 21.4). We again perform this query using our QueryData
function; the i and j parameters are again set to 0.
Now that we have recorded all of the section numbers and course numbers taken by the
student, we can query for the remaining information. We will find the course name, credit
hours, department, and course type from tblCourse by matching the course numbers
recorded (see Figure 21.12); and, we will find the year and semester from tblSection by
matching the section numbers (see Figure 21.13).
Figure 21.20 The table “tblCourse” has descriptive information on each course.
29
Chapter 21: Working with Large Data Using VBA
Figure 21.21 The table “tblSection” has detailed information on each section.
However, these queries will be repeated multiple times since we want to find this
information for each course in our initial query result. We will therefore use a Do Until
loop will the following basic structure:
Since we are repeating these queries for each course in our initial query result, our
output range will have to shift down one row each time. This is why we have added the i
and j parameters to the QueryData function. The output range will be offset by the same
i value used in the loop. (In this case, the j value will always be 0; however, for later
applications it may be used.)
Here the criteria is the course number which can be found from the initial query results in
“TransTBL”. The output range for this query will be in the second section of the result
table, the first cell of which we have named “CourseTBL”.
30
Chapter 21: Working with Large Data Using VBA
(Notice here that we do not end this SQL statement with another & “’”. This is because
the section number value is numeric and therefore does not need to be included in
single quotation marks.) The output range for this query will be in the third section of the
result table, the first cell of which we have named “SectTBL”.
As we loop through the courses taken by the student to perform these two queries, we
also keep track of the sum of the grades so that we can calculate the GPA when the
loop has ended. We do this using a GPA variable as follows:
When the loop has ended, we find the GPA value by dividing the GPA variable by the
last value of i (i was initialized to be 0). We then sort the entire result table by the year
value. The final code is shown in Figure 21.14.
Figure 21.22 The Transcript procedure contains the main query code.
21.5 Summary
31
Chapter 21: Working with Large Data Using VBA
¾ The main pivot table object is PivotTables. You must use the ActiveSheet object
before specifying a PivotTables object.
¾ From this PivotTables object we can further specify PivotFields, and from PivotFields
we can specify PivotItems.
¾ For the ActiveSheet object, we primarily want to use the PivotTableWizard method to
create our pivot tables. The PivotTableWizard method has several arguments:
SoruceType, SourdeData, TableDestination, and TableName.
¾ Two properties of the PivotTables object are the RowGrand and ColumnGrand
properties. The possible values for these properties are True or False. Another
property that may be used often is the Format property. This can be used to apply a
pre-defined report or table format to your pivot table. The values for this property can
be an xlReport value or xlTable value.
¾ An important method of the PivotTables object is AddFields. This method is used to
set row fields, column fields, and page fields of the pivot table. Each field is an
optional argument of the method: RowFields, ColumnFields, and PageFields. To
enter these argument values, simply type the name of the field. If there is more than
one field for a particular argument, then use the Array function to specify them. There
is another argument of this method called AddToTable which takes True/False
values. Another useful method of the PivotTables object is the RefreshData method.
¾ A property of the PivotFields object is the Orientation property. This property takes
the values xlDataField, xlRowField, xlColumnField, and xlPageField for the
respective fields. There is another possible value for the Orientation property which
is xlHidden
¾ Two other properties of the PivotFields object are the Function property and
SubTotals property. The Function property is used for data fields. The SubTotals
property is used for non-data fields.
¾ There is one main property that is used often with the PivotItems object which is the
Visible property. This hides or shows different values of the items in a selected pivot
field.
¾ To create a pivot chart in VBA simply use the Chart object. After adding a new chart,
set the source data to the created pivot table instead of the original data used to
create the pivot table.
¾ There are two main systems used in VBA for communicating with external data
sources: DAO and ADO. We use ADO as we feel it is less complicated.
¾ There are two main ADO objects used to import data: Connection and Recordset.
¾ The Connection object establishes the communication to a particular database. To
define a Connection object variable, we use a data type called ADODB.Connection.
There are two main methods used with this object: Open and Close. The Open
method uses a ConnectionString argument to define the path to the database. To
define the data source value of this argument we use the string filename value and
the Path property of the ThisWorkbook object.
¾ To define Recordset object variables, we use the ADODB.Recordset data type. The
arguments for the Open method of the Recordset object are Source and
ActiveConnection. The Source argument defines the data that should be imported.
The ActiveConnection argument value is the name of the open Connection object
you have previously defined
¾ To copy this Recordset-defined data to the Excel spreadsheet, use the Range object
and the method: CopyFromRecordset.
¾ Structured Query Language (SQL) is the code used to perform queries, or filter the
data which is imported. The basic structure of SQL commands is: an action to
32
Chapter 21: Working with Large Data Using VBA
perform, the location of the data on which to perform the action, and the criteria the
data must meet in order for the action to be performed.
¾ The SELECT statement selects a specific group of data items from a table or query
in the database. The phrase appearing immediately after the SELECT statement is
the name or names of the fields which should be selected.
¾ The FROM statement is followed by the name of the table or query in the database
from which the information is being selected.
¾ The most common statement to implement the filtering step of the query is WHERE.
The WHERE statement can use sub statements such as <, >, = for value
evaluations, or BETWEEN, LIKE, IN, AND, OR, and NOT for other comparisons.
¾ ORDER BY is another statement; it can be used with the WHERE statement to sort
the selected data; this data can be sorted in ascending or descending order using
the statements ASC or DESC respectively.
¾ Variables can be used to make queries dynamic with Input Boxes, User Forms, or
simply taking values the user has entered to a spreadsheet. These variables can
then be placed in the statements of SQL commands in the code to search for the
user’s criteria.
¾ To export data from Excel to a database, you have to use an Add-In called
AccessLinks. This Add-In must be downloaded and installed from the Microsoft
webpage. There are three exporting methods using this Add-In: MS Access Form,
MS Access Report, or Convert to MS Access.
¾ The only other way to export data from Excel is with Charts. Use the Export method
with the Chart object. The Export method has three arguments: FileName,
FilterName, and Interactive.
21.6 Exercises
21.6.1 Review Questions
33
Chapter 21: Working with Large Data Using VBA
c. Ask the user to specify the range on the spreadsheet where they want the
date to be imported. They should select this range using a RefEdit control.
d. Import the text or webpage to this location using VBA commands.
Use the tables and queries (shown above) from the Access database
“UniversityInformationSystem” to answer the following three exercises.
5. Suppose you want to create a small user form which lists all department names
in a combo box (refer to the course list form in Figures 21.8 and 21.10 from the
application). Write a procedure to do this. (You may use the QueryData function
developed in the chapter.)
6. Create a procedure called Audit which lists all the courses affiliated with a
selected department. Each course should have its ID and name listed. The credit
hours should also be listed for each course along with whether it is “required” or
an “elective”. The department should be selected from a small user form with a
combo box listing all departments from the database (refer to exercise 5 above).
34
Chapter 21: Working with Large Data Using VBA
description of the information stored in each table (all field names are
given in parenthesis).
Table “tblAircraft” gives information for each plane, or aircraft, including: plane
ID (CraftID), maximum number of seats (MaxSeats), manufacturing company
(ManfCompany), and plane type (Type).
35
Chapter 21: Working with Large Data Using VBA
36
Chapter 21: Working with Large Data Using VBA
37
Chapter 21: Working with Large Data Using VBA
8. Create a user form which will list the different plane manufacturers in a combo
box. Based on a selection from this list, show another form which lists all the
planes made by that manufacturer (again in a combo box).
9. Create a user form in which a Passenger Name is selected from a list. Based on
this selection, display the passenger’s ID, phone number, and flight reservation.
10. Create a procedure which will report detailed information for each flight. The user
should select a flight from a combo box in a user form. After the flight has been
selected, check the Leg Instance table to find each plane assigned to that flight.
Report the maximum seats on this type of plane and the seats available on this
particular flight instance. Report to and from which city this flight is traveling. Also
report the date of the flight instance.
38
Chapter # 22
Debugging and
Error Checking
Chapter Overview
22.1 Introduction
22.2 Types of Errors
22.2.1 The Debug Toolbar
22.2.2 Step-Into
22.2.3 Step-Over
22.2.4 Step-Out
22.2.5 Run To Cursor
22.2.6 Breakpoints
22.6 Summary
22.7 Exercises
Chapter 22: Debugging and Error Checking
22.1 Introduction
In this chapter, we offer help to the reader in understanding possible errors that may be
encountered in VBA. We describe the types of errors that may be found and some tools
to identify the cause of these errors. We also give some suggestions for avoiding errors
in the VBA code.
Some of the more complicated errors may be logical errors, improper use of properties
or methods, or user-created errors. Property and Method misuse requires you to
review your knowledge of various properties and methods; the Help file can be useful for
this. However, logical errors and user-created errors will require the use of some extra
debugging tools and error checking commands.
Logical errors are usually found in loop structures, especially nested loops. For these
errors, the code runs without a problem, but the results are inaccurate. These errors are
complicated in that they are usually harder to identify. Since the code will not display an
error and bring you to the problem point in the code, you will have to use the debugging
techniques described below to “walk through” your code and determine where the logical
error may be. Once the error has been identified, you will have to return to any
mathematical modeling or outline of your application to ensure that the appropriate
calculations or actions are being performed in the correct order.
User-created errors can also be complicated and hard to determine. Users may delete
cells whose names are referenced in the code, input data incorrectly, or press buttons in
a different order than you intended. These errors will not be found in your code, but
should be prevented by placing extra commands or structures in your code. These error
checking, or preventing, methods are discussed below.
Logical errors: to identify these use the debugging tools described below
User-created errors: to prevent these use the error checking commands below
Summary
2
Chapter 22: Debugging and Error Checking
There is a breakpoint tool icon and three main stepping tool icons which will be used to
debug your code. Stepping through a program allows you to execute the code in steps;
these steps can be one line at a time, one loop at a time, one procedure at a time, etc.
These tools can be used throughout the entire code, or in a particular procedure or
segment of code. There are also some debugging window options available on the
toolbar which we will discuss in a later section.
You can also find these tools, plus others listed under the Debug menu in the VBE (see
Figure 22.2). The first option in this menu is Compile VBAProject. This tool checks your
code for any main errors, usually simple errors, without executing the code. There is one
extra stepping tool, called Run To Cursor, which we describe below. There is also an
extra breakpoint tool given here; again we describe this in more detail below.
3
Chapter 22: Debugging and Error Checking
22.3.1 Step-Into
The Step-Into stepping code is used to execute every line of code. To use Step-Into,
use the icon from the Debug Toolbar, Step Into option from the Debug menu, or the
shortcut key F8. This tool is the most common stepping tool; it is used to initiate stepping
through code in which all stepping tools can be used. It is also used to find the exact line
in which a bug exists. That is, the other stepping tools can be used to step through larger
segments of code, but once you arrive at an area in the code where you guess or know
the bug is, you will always use the Step-Into tool to find the exact line or exact moment
when the bug occurs.
22.3.2 Step-Over
The Step-Over tool will skip over code called procedures. That is, if you are stepping
through code in a sub procedure which calls another sub procedure or a function
procedure, when the Call statement is read, the Step-Into tool would move to the called
procedure and continue executing code one line at a time whereas the Step-Over tool
will skip the called procedure (executing it in normal run time not one line at a time) and
move on to the next line of code in the main procedure. To use Step-Over, use the
icon from the Debug Toolbar, Step Over option from the Debug menu, or the shortcut
key Shift+F8.
22.3.3 Step-Out
The Step-Out tool is also used primarily with called procedures. If you are stepping
through a procedure in which another procedure is called, and instead of using the Step-
Over tool to skip this called procedure code you continue using Step-Into, you may find
yourself in a long loop or sequence of actions in this called procedure in which you do
not believe a bug will be found. To finish executing all of the code in this called
procedure at once and return to the main procedure, use the Step-Out tool. To use Step-
Out, use the icon from the Debug Toolbar, Step Out option from the Debug menu,
or the shortcut key Ctl+Shift+F8.
The Run To Cursor tool allows you to execute code in normal run time until the point in
which your cursor is located in the code; at this point the code is paused and you can
step through the code using the other stepping tools. The Run To Cursor tool is found in
the Debug menu, not the Debug Toolbar; it has the shortcut key Ctl+F8.
22.3.5 Breakpoints
4
Chapter 22: Debugging and Error Checking
Breakpoints are used like the Run To Cursor tool. A breakpoint is set on a particular
line of code, and when the program is run, execution occurs in normal run time for every
line of code before the breakpoint and pauses at the breakpoint. Debugging a particular
part of a large program may be easier if all previous lines of code are executed quickly
and then the step through tool is used to slowly narrow the search for the bug. The main
difference between Run To Cursor and breakpoints is that multiple breakpoints can be
placed in your code. This can be advantageous if there is more than one place where
you suspect the bug to be. You can execute code to the first breakpoint, use a few step
through tools to analyze some lines of code, then execute until the next breakpoint and
so on; use the Run option to execute code in normal run time until each breakpoint.
To place breakpoints in the code, select a line of code and use the Toggle Breakpoint
tool. To use this tool, choose the icon from the Debug Toolbar, Toggle Breakpoint
option from the Debug menu, or the shortcut key F9; you can also simply click on the
left-hand margin of the code window next to the line where you want to insert the
breakpoint. To remove a breakpoint from your code, use the Toggle Breakpoint again on
the same line of code where the breakpoint currently is. You can also use the Clear All
Breakpoints option from the Debug menu to clear all breakpoints added to the code; the
shortcut for this tool is Ctl+Shift+F9.
There are other debugging tools available, but these are the ones we feel you will use
most often.
In the Watch window, variable names are typed to be “watched” by VBE and displayed
whenever their values change. To view the Watch Window, choose View > Watch
Window in the VBE or use the icon from the Debug Toolbar.
5
Chapter 22: Debugging and Error Checking
To enter a variable into the Watch Window, use the Add Watch option from the Debug
menu; you can also right click in the Watch Window and select Add Watch from the
drop-down options or simply highlight a variable in the Code Window and drag it to the
Watch Window. If you use the Add Watch tool, you will see the window shown in Figure
22.3; here you can enter the name of the variable, and the location of the variable:
procedure and module names.
Note that the location of the variable is very important. If you choose a private variable
for example, you will not be able to view its value once you have moved to another
procedure. Likewise, if you want to watch a public variable which is used in different
modules, you will need to add a watch for each variable location. Anytime the code is
executed in a location different from the watched variable location, you will see an <Out
of Context> message instead of the variable value.
There are also three Watch Types available: Watch Expression, Break When Value
Is True, and Break When Value Changes. The Watch Expression will be the most
commonly used Watch Type; this just allows you to view the variable value as it
changes. The break options will pause the code when the specified action occurs.
The Watch Window is very useful for checking for logical errors. It can also help you
keep track of counting variables in loop structures and input values from the user (see
Figure 22.4).
Figure 22.4 The Watch Window is tracking the value of the counting variable i.
6
Chapter 22: Debugging and Error Checking
To check the current value of a variable while using the step through tools, you can use
the Quick Watch tool. To do this, highlight a variable in your code and use the icon
from the Debug Toolbar or select the Quick Watch option from the Debug menu. The
Quick Watch window will tell you the value and location of the selected variable (see
Figure 22.5). You can press the Add button in the Quick Watch dialog box to add this
variable to the Watch Window. You can also hold your cursor over a particular variable
in the Code Window to see its value. This is a useful tool when you do not wish to
populate the Watch Window with too many variables, but rather just check some other
variables while running the code which are not necessarily as much of a concern as
those in the Watch Window.
Figure 22.5 Quick Watch shows the value and location of the selected variable.
use the icon from the Debug Toolbar. You can use this window to check particular
actions before entering them into your code, to test certain procedures which may be
called in the code, or other sets of actions. To use the Immediate Window, just type
directly in it as you would in the Code Window (see Figure 22.6). This can be a useful
tool for checking if code will work before you insert it into your program and for narrowing
the bug search to a particular set of actions in the middle of your code.
Figure 22.6 The Immediate Window executes a line of code as soon as it is entered.
7
Chapter 22: Debugging and Error Checking
Another important idea to remember is making sure that necessary References have
been added to your project in the VBE. For example, if you are trying to use Solver
functions, you must reference the Solver first using Tools > References in the VBE.
Similarly, if you are trying to call functions from the Analysis Toolpack, you will need to
ensure that the necessary Add-Ins have been installed.
We have discussed some error checking techniques in previous chapters. For example,
to check if a user has entered a numerical value with and Input Box, we use a Variant
data type temporary variable and the IsNumeric function (see Chapter 16). Another error
checking technique we saw was with User Form objects. To ensure that all text box
values were filled when an OK command button was clicked, we used the Control data
type, TypeName function and SetFocus method (see Chapter 18). We also used error
checking techniques with the Solver functions. To prevent VBA error messages for
infeasibility, we use the SolverSolve function assigned to some variable (see Chapter
19). With all of these techniques, we always use an If, Then statement to check if an
error condition is true or not. We then use Message Boxes to communicate possible
errors to the user.
Communication to the user is very important and can prevent several errors from
happening. It is important to make Input Box statements clear as well as User Form
statements. You should also specify any necessary instructions on the spreadsheets in
the program to help guide the user. We have seen in previous chapters how to hide and
show various buttons, or shapes, on the spreadsheet at different times during the
8
Chapter 22: Debugging and Error Checking
program. This is another good preventative method to keep the user from choosing
options or actions at an inappropriate time.
There are some other helpful techniques to prevent user-created errors and keep the
program running if these errors occur.
22.6.2 Is Functions
Aside from the IsNumeric function used previously, there are some other Is Functions
available for error checking. The IsNull function checks whether or not a variable has a
valid value. This is useful with Input Boxes or Text Boxes to ensure that the user has
entered some valid value. Similar to this function is the IsEmpty function. This function
checks whether or not a variable has been initialized or whether or not a String variable
contains any value or is an empty string “”. This can be important if you are depending
on the user to give some value to variables which will be used in later references or
calculations.
There are two useful Is Functions used to check data types: IsArray and IsDate. The
IsArray function checks whether or not a particular variable is being used as an array.
For example, if a variable is declared with the intention of becoming an array, it may not
be given a particular size or data type but be declared as a Variant. Then, later in the
code, you may assign this variable some user-defined values using an Array function. To
check whether this assignment was performed correctly, you would use the IsArray
function with this variable. The IsDate function checks whether or not a variable has a
value which can be converted to a date. If the variable was declared as a Date data
type, it would check that the values assigned are valid. If the variable was declared as a
String, it would check that the format of the string value can be converted to a date. This
is useful since most users would enter dates using a variety of string formats and not
Date data type values.
Is Functions
IsNumeric checks whether or not a variable has a numeric value
Summary
IsNullchecks whether or not a variable has a valid value
IsEmpty checks whether or not a variable has been initialized
IsArray checks whether or not a particular variable is being used as
an array
IsDate checks whether or not a variable has a value which can be
converted to a date
The On Error Resume Next statement is used to skip an error and continue running the
program. This statement should be placed before a line of code in which an error may
occur; to identify these areas, consider sections where users are affecting the variable
values or actions of the code. Note that if you resume running the code after an error
has possibly occurred, there may be some incorrect calculations or reports made if the
user has provided bad input. Therefore, we recommend using this statement when there
is a possible user-created error which may generate a VBA error message that you want
9
Chapter 22: Debugging and Error Checking
to avoid, but will not directly affect the value of any variables or other insignificant object
properties. For example, in the following code we have initialized an integer variable
value so that if the user enters a non-integer input value, we can ignore the error and
continue with the code.
Sub CopyPaste()
Dim size As Integer
size = 1
Range(“A1”).Offset(size,0).Copy
Range(“B1”).PasteSpecial
End Sub
The On Error GoTo statement is also used to avoid seeing the VBA error messages.
This statement should also be placed before a line of code in which an error may occur.
The difference with this statement is that there should be a line number or procedure
name after the statement GoTo. Line numbers are not automatically given in VBA code,
rather you have to insert your own for specific areas of code if desired. To insert a line
number simply enter any number in the first column of the VBE Code Window (do not
tab) and place a colon (:) at the end of the line. These numbers do not have to be
relative to the location of the procedure in your module; that is, the line numbers are
independent for each procedure. For example, in the following code, we have numbered
the lines 1 through 4. In this example, if the user does not enter a positive number for the
integer variable, the Log function cannot be performed. Therefore, we return to the Input
Box statement on line 2 of the code.
Sub Division()
1: Dim UserVal As Integer
3: On Error GoTo 2
4: MsgBox "The log base 2 of your number is: " & Log(UserVal)
End Sub
You can also use line labels; these are entered into the code in the same manner as
line numbers but they can be any word instead of a number. (Note that you can enter
line numbers and line labels either on the same line of code or on the line above the
relative code.) In the following example, we use the On Error GoTo statement to ensure
that we do not try to take the square root of a negative number.
Sub CalcSqRt()
Dim UserVal As Integer
InputLine:
10
Chapter 22: Debugging and Error Checking
End Sub
Note that you can only “go to” a line within the current procedure. For example, you may
want to redirect the user to perform a different set of actions or you may simply move to
a new loop or line of statements in the code if an error occurs. You can also use the
GoTo statement by itself to return to any line of code after you have found an error and
reacted to it.
There is also an Err object which can be used to understand what error is occurring and
to redirect the user accordingly. The Err object will be used most often with the Number
property. Each VBA error has a number associated with it; notice the number at the top
of the VBA error message when an error occurs (see Figure 22.7).
This can be a useful error checking technique in that you can immediately redirect the
user in a very specific manner. For example, if you are using range names in your code
and the user has deleted the cell which contained the range name, you can identify this
error by the Err Number property and specifically instruct the user to rename the cell in
Excel. This check is usually done with an If, Then statement in combination with the On
Error Resume Next statement. You will need to ignore the error first, and then analyze it.
The code below would generate the error message shown in Figure 22.7 if there were
no such ranges named in Excel:
Sub Test3()
Range("Input").Copy
Range("Result").PasteSpecial
End Sub
If you use the error checking techniques below, however, then the user will only see your
Message Boxes and no VBA error messages:
Sub Test3()
11
Chapter 22: Debugging and Error Checking
There are other properties of the Err object; however, we feel the Number property will
be the most useful.
On Error Resume Next used to skip an error and continue running the
program
On Error GoTo used to skip to a line number or procedure name
Summary after the words Go To
Err.Number used to understand what error is occurring and
to redirect the user accordingly
22.7 Summary
¾ Some simple errors may include misspelling words in variables or range
references, misnaming User Form objects, and forgetting to declare variables,
dimension arrays before using them, or initialize counting variable values or
range variables.
¾ Logical errors are usually found in loop structures, especially nested loops. For
these errors, the code runs without a problem, but the results are inaccurate. Use
debugging tools to identify these errors.
¾ User-created errors can also be complicated and hard to determine. Users may
delete cells whose names are referenced in the code, input data incorrectly, or
press buttons in a different order than you intended. Use error checking
techniques to identify and prevent these errors.
¾ The Debug Toolbar and Debug menu provide debugging tools to search for
simple errors or logical errors.
¾ The Step-Into stepping code is used to execute every line of code. It is used to
initiate stepping through code in which all stepping tools can be used. It is also
used to find the exact line in which a bug exists.
¾ The Step-Over tool will skip over code called procedures (executing it in normal
run time not one line at a time) and move on to the next line of code in the main
procedure.
¾ The Step-Out tool is also used primarily to finish executing all of the code in a
called procedure at once and return to the main procedure.
12
Chapter 22: Debugging and Error Checking
¾ The Run To Cursor tool allows you to execute code in normal run time until the
point in which your cursor is located in the code; at this point the code is paused
and you can step through the code using the other stepping tools.
¾ A breakpoint is set on a particular line of code, and when the program is run,
execution occurs in normal run time for every line of code before the breakpoint
and pauses at the breakpoint. The main difference between Run To Cursor and
breakpoints is that multiple breakpoints can be placed in your code. Use the tools
Toggle Breakpoint and Clear All Breakpoints.
¾ There are two debugging windows: Watch Window and Immediate Window.
¾ In the Watch window, variable names are typed to be “watched” by VBE and
displayed whenever their values change. Use Add Watch to add a variable to the
watch window.
¾ Whatever is typed in the Immediate Window is executed as soon as it is entered.
¾ Error Checking techniques from past chapters include using the Variant data
type, IsNumeric function, Control data type, TypeName function, and SetFocus
method.
¾ Other useful Is Functions include: IsNull, IsEmpty, IsArray, and IsDate.
¾ Two useful error checking statements are On Error Resume Next and On Error
Go To. The On Error, Resume Next statement is used to skip an error and
continue running the program. The difference with this statement and the On
Error Go To statement is that there should be a line number or procedure name
after the words Go To.
¾ Line numbers and line labels can be added to your code to use with Go To
statements.
¾ The Err object can be used to understand what error is occurring and to redirect
the user accordingly. The Err object will be used most often with the Number
property.
22.8 Exercises
22.8.1 Review Questions
1. What are syntax, runtime, and logic errors? Briefly explain each.
2. What are step into, step over, and step out tools? Explain each.
3. What is the difference between stepping over a procedure/loop and stepping out
of a procedure/loop?
4. What are breakpoints? Why would breakpoints be used?
5. Which Is Function is used to check whether or not a variable has been
initialized?
6. What is the difference between the IsNull and IsEmpty functions?
7. What command is used to skip part of a code and go to a specific procedure?
8. What is a common error number? What error does this number signify?
9. What is a line label? How to you add one to your code?
10. Do On Error statements go before or after the line of code which may cause an
error?
13
Chapter 22: Debugging and Error Checking
Sub ReceiveInput()
Range("A1").Value = InputBox("Please enter a two-digit number")
What would be a better way to check if the number entered was really two digits?
What other error-checking could be used on the input?
2. Correct the code below. Add an On Error statement to this code where you fill it
may be most needed. Add more code to the procedure which could be used to
correct the error. Use line labels.
Sub CountDown()
Dim CountStart As Integer
Sub Formatting()
Dim FormatMe As Range
With FormatMe
.Interior.Color = vbRed
.Cut Destination:=Range("FormatMe").Offset(2, 0)
End With
End Sub
4. A programmer wants to create a list of even numbers between 0 and 20. What is
he doing wrong in this code?
Sub Looping()
Do While i < 20
If i Mod 2 = 0 Then
Range("A1").Offset(i, 0).Value = i
End If
Loop
End Sub
5. Correct the code below so that the array of values is filled correctly.
Sub FindError()
14
Chapter 22: Debugging and Error Checking
For j = 1 To 10
On Error GoTo CorrectNum
NumArray(i) = i / 2
Next j
CorrectNum: i = i * -1
End Sub
15
Part III: Case Studies
EXCEL BASICS
In this part of the book, we have developed several case studies of fully developed DSS
applications. This applications appear in IE/OR, Business, and in other general
engineering fields.
Topics Overview
These steps have been our guidelines in developing decision support systems. We do
not claim that they are necessary to follow, but rather consider them helpful guidelines
when developing a DSS application. In this chapter, we provide several examples from
case studies that we have developed using these proposed steps. The following
chapters offer a more detailed explanation of each case study’s development using
these five steps. We wish to illustrate the variety and consistencies that are possible
when developing DSS applications.
The user should then encounter a method for providing input, such as a form or a set of
forms. Or the user can be brought to a new sheet where further instructions are
provided. Deciding which method or methods to use is significant and depends on the
application. For example, if we only need one or two pieces of information from a user,
we may not even need a form or an entire sheet for input; instead, we can use an Input
Box. In some applications, we may need large sets of data for our analysis. In that case,
we may just prompt the user to import data from a text file or a database to a
spreadsheet. Once we have decided which method is most appropriate for the
application, we may need to spend more time designing the interface. (We will return to
this task in a later step.) It is important to complete the Application Overview before
designing the interface so that we have a clear idea of what the entire application will
incorporate.
After receiving the input, the model should be ready and calculations can now be
performed. It is a good idea at this point to have an overview of what is required for our
model calculations. We may need to determine the model formulation before we can
finish deciding what the user input will be. First we must decide if the DSS will compute
simple calculations, perform an optimization, or run a simulation. The details of these
models can be outlined in a later step, but for the purpose of the Application Overview,
we should have an idea of what is involved. This general model outline will help us to
determine the details of our spreadsheet design and procedures later.
Once input is received and the model calculations are performed, we need to determine
what output is displayed to the user: charts, graphs, histograms or tables? Does some of
the input need to be re-displayed to the user? Again, these options depend on the
application. It is important to consider the output, as it is a driving force in establishing
why the user is using the DSS. It is a worthwhile to check if we are computing everything
that the user may be interested in.
The last part of the Application Overview is adding resolve options. In Chapter 1, we
defined in detail what comprises a DSS. These include the model base and user
interface discussed above. However, a DSS should also provide resolve options for the
user. The user should be able to change some of his or her initial input values and
resolve the problem. The user may also want to add some constraints to an optimization
or redefine the objective function. We suggest that these resolve options be made
available on the output sheet. We will give more examples of these resolve options in
the following sections.
23.3 Spreadsheets
There may be two to several sheets in a DSS application. The first sheet should always
be the “Welcome” sheet, as we discussed above. For example, in Figure 23.1, we
display the “Welcome” sheet for a case study that we developed for a “Portfolio
Management and Optimization” DSS. In it, we describe the DSS and the model
assumptions. We also reference the source of our model formulation and include some
images related to portfolios. Then, we introduce a “Start” button, which the user can
press to begin the application.
The remaining sheets are for input, calculations, and output. Each can be a separate
sheet, or some elements can be combined onto fewer sheets. Suppose we need a sheet
for input. We can prepare the sheet by placing appropriate labels for tables or input
locations. We may also name some ranges at this point; this step will help us later when
coding. Below is an example from a case study that we developed for the “Critical Path
Method” (see Figure 23.2). In this application, we take the user through several input
sheets. In each sheet, a table for a set of input values appears. In some cases, a
spreadsheet may be a better user interface than forms for receiving input; we discuss
this in more detail in the next section.
We may also have an application that requires a large set of data. This data can be
imported from a text file or a database, or it can be input by the user. Figure 23.3
displays an input sheet from a “Stochastic Customer Forecasting” case study. This sheet
contains the historical data that is used to make future forecasts. In this application, we
give the user the option to enter this data manually or import it from a text file.
Figure 23.3 An example of a large set of data imported from a text file.
We may not require an input sheet for every application. Let’s consider the case in which
the input sheet can be combined with the calculations sheet or output sheet. For
example, the figure below displays a sheet from a “Technical Analysis” case study in
which the input, calculations, and output are all on one sheet (see Figure 23.4). Here,
the user can modify the input using spreadsheet controls and press the “Resolve” button
to update the calculations in the table. The output is summarized in a small table on the
right of the screen.
Figure 23.4 An example of input, calculations, and output on the same sheet.
Again, we may not even use a sheet at all for the input. We may simply obtain input from
a user form and then use that directly in a calculations sheet or in some calculations
procedures and take the user directly to the output sheet. Since we are developing
spreadsheet-based DSS applications, we usually take advantage of the spreadsheet
features to aid us in performing calculations. For this reason, we will usually work with a
calculations sheet. This sheet may be viewed by the user or, in most cases, hidden from
the user. A calculations sheet should be hidden if the intended user may not be familiar
with the details of the calculations but is solely interested in the results.
It is possible to have some additional hidden sheets related to the calculations. For
example, in simulation we usually store the results of the runs to user for creating
histograms or other summary reports. This detailed sheet should be hidden from the
user in the application flow, but can be made available for viewing if the user is
interested. With the Application.ScreenUpdating method and the Worksheets.Visible
property, we can prevent the user from seeing these calculation sheets while they are
operating for the model calculations.
Probably the most important sheet for the user is the output sheet. This sheet should
clearly summarize the results of the calculations so the user can understand the
behavior of whatever system he or she was modeling or analyzing. It is usually a good
idea to include some graphical results as part of the output sheet. For example, in Figure
23.6, the results sheet from an “Inventory Management” case study illustrates the
ordering strategy found by the model calculations. There are also some tables that
summarize the numerical results of the solution.
In some cases, we may have several charts or larger summary tables that may not fit
into one output sheet. In that case, we recommend making a navigational output sheet
that will allow the user to view these individual reports. For example, In Figure 23.8, we
show a navigational output sheet from a “Supply Chain Management” case study. This
sheet allows the user to view several different summary pivot tables (see Figure 23.9).
From these pivot tables, the user can also view corresponding pivot charts displayed as
separate chart sheets (see Figure 23.10). The user can always return to the navigational
output sheet from any of these reports.
Figure 23.9 A pivot table report sheet is one of the output sheets.
Whichever results are relevant to our application, we should ensure that they are clearly
presented in the output sheet. “End” and “Resolve” options should also be included in
the output sheet, as well as options to “View” the input or the calculation sheets.
Summary Input Sheet User input; large data input; can be combined with
other sheets.
Let’s begin by discussing navigational buttons. As we have already mentioned, the first
button we should create is the “Start” button, which is located on the “Welcome” sheet
(see Figure 23.1). This button should be assigned to a macro that brings the user to the
input interface. On all other sheets (input, calculation, and output sheets) there should at
least be an “End” button. The user should always have the option to “End” or “Exit” the
application. (Note: Whether you use “End” or “Exit” or any other phrase for this action, be
sure that you are consistent across all sheets in the application. We will discuss
consistency in user interface design more in the GUI design section.)
We may also include such navigational buttons such as “Next,” “Continue,” or “Back” if
we intend for the user to be able to step through the sheets or revisit sheets. These
buttons are especially important if we have hidden the sheet tabs or are only making one
sheet visible at a time, which we recommend for a more professional presentation. In the
case in which input, calculations, and/or output are combined, we may also include
some functional buttons on the spreadsheet, such as “Solve.” For example, in Figure
23.11, one such sheet is in a case study on the “Animation of the Kruskal Algorithm.” In
this case, we take the user directly from the “Welcome” sheet to the sheet shown in the
figure. We highlight the “Create Table” button as it is the next button that the user should
press (see Figure 23.11(a)). When the user presses this button, he or she is prompted to
give the dimensions of the network; then, a table with a corresponding number of rows is
created.
After the table is created, we now make visible a new button called “Solve” (see Figure
23.11(b)). We un-highlight the “Create Table” button and highlight the “Solve” button,
since it is the next button the user should click. This button runs the procedure that
animates Kruskal’s algorithms and determines the minimum spanning tree solution.
(a)
(b)
Figure 23.11 An example of spreadsheet buttons to work with input and calculations.
Form controls on the spreadsheet are useful when there are many resolve options in the
application. In this case, we want to provide the user with easy access to the input so he
or she can change it multiple times. It is important to retain the clarity of the spreadsheet
layout when placing form controls adjacent to other input cells, calculations, or output.
We will return to these interface design issues in a later section.
(a)
(b)
Figure 23.12 Dynamic form controls on a spreadsheet.
In some cases, there may be an even tradeoff between applying functional buttons or
form controls on a spreadsheet and creating a user form. For example, in Figure
23.13(a), we have used two functional buttons to allow a user to “Add” and “Remove”
stocks to and from his or her portfolio in the “Portfolio Management and Optimization”
case study. In this particular case study, we have put this functionality on the
spreadsheet because it is a feature that the user may use often. The user may go to a
new sheet to view stock comparisons and then return to edit the portfolio; he or she may
also go to an optimization sheet to view investment strategy results and then return to
edit the portfolio and resolve.
However, if the user does not need to create or edit his or her portfolio multiple times, we
may choose to create a user form to perform this functionality. In Figure 23.13(b), we
provide an example of such a form for a “Beta of Stocks” case study. In this case, the
user only selects his or her portfolio once.
(a)
(b)
Figure 23.13 An example of controls on a form and spreadsheet.
In most DSS applications, if there is a large enough set of input required from the user,
we suggest creating user forms. User forms can be advantageous in that there are many
options for placing and manipulating controls on a user form. The controls can also be
more clearly displayed, as they do not interfere with other cells on the spreadsheet.
Another advantage of user forms is that they can be displayed to the user at any time;
that is, they are not attached to a specific spreadsheet. This feature can be especially
useful for resolve options. If a user wishes to resolve the problem and presses a
“Resolve” button on an output sheet, the input form can be directly redisplayed without
even moving to a new sheet.
Figure 23.14 displays a user form from a “Retirement Planning” case study. This form is
dynamic in that the first frame below the text boxes may change depending on a
previously selected option. In Figure 23.14(a), the user is providing values for “Desired
Savings at Retirement” and “Confidence Interval for Returns,” whereas in Figure
23.14(b), this frame changes to prompt the user for the “Age to Retire.” The second
frame on this form, for “Asset Allocation,” is also dynamic. In Figure 23.14(a), the user is
prompted to enter this information, but in Figure 23.14(b), the textboxes are grayed and
locked since the information is not relevant for this option.
(a) (b)
Figure 23.14 An example of dynamic form controls.
Another way to employ user forms for a situation in which the user may need to modify
input multiple times is to create a “floating” form. The advantage of this type of user form
is that the user can select or modify cells in the spreadsheet without having to close the
form first. In Figure 23.15, there is an example of a floating form from a case study for
“Animating the Simplex Method.” This form allows the user to select the entering variable
for each iteration from the tableau on the spreadsheet; then, he or she can view the
results for that scenario on the floating form. The form is hidden when the user moves to
another sheet.
There are many options available for creating a user interface for an Excel DSS.
Choosing between functional buttons, form controls on the spreadsheet, or a user form
depends on the application and what options the user should have in providing and
changing input values.
23.5 Procedures
The next step in developing a DSS application is to create an outline of what procedures
we need in order to conduct the flow and execute the calculations. As discussed in
Chapter 15, we recommend organizing your code into several smaller procedures that
may be called from other main procedures or associated with buttons on the
We always begin our applications with a Main sub procedure that is associated with the
“Start” button on the “Welcome” sheet. From the Main procedure, we usually clear all
previous data and initializing variables by calling a ClearPrevious procedure. We then
either take the user to an input sheet or display an input form. For example:
Sub Main()
Call ClearPrevious
frmInput.Show
Worksheets(“Input”).Visible = True
Worksheets(“Welcome”).Visible = False
End Sub
Sub ClearPrevious()
‘clear ranges on other sheets
Worksheets(“Calc”).Range(“InputValues”).ClearContents
Worksheets(“Output”).Range(“Results”).ClearContents
‘initialize variables
Set InputRange = Worksheets(“Input”).Range(“InputStart”)
End Sub
There should then be a procedure that receives the user’s input. If we are applying user
forms as the interface for receiving input, then this code would be in the form’s event
procedures. For example:
Sub cmdOK_Click()
‘set variables equal to control values
NumRuns = txtNumRuns.Value
InputSize = txtInputSize.Value
ReDim InputArray(InputSize)
Unload Me
End Sub
Once the input is received, the calculations should be ready to perform. These
calculations may involve running a simulation with a loop structure or evoking the solver
with the Solver commands. The calculation procedure(s) may be called when the “OK”
button is clicked on a user form or it may be assigned to a “Solve” or “Continue” button
on an input spreadsheet. For example:
Sub DoSimulation()
For i = 1 to NumRuns
‘create random values
‘perform calculations
Next i
End Sub
Sub DoOptimization()
SolverReset
SolverOK SetCell:= …, MaxMinVal:= …, ByChange:=…
SolverAdd CellRef:= …, Relation:=…, FormulaText:= …
SolverOptions AssumeNonNeg:=True
SolverSolve UserFinish:=True
End Sub
The final procedure to be outlined is related to displaying the solution on the output
sheet. If there is a chart, we may need to update the source data. If a simulation took
place, we may want to create some histograms. In any case, we want to put the solution
values in a report table on the output sheet. The procedure to create the output may be
called from the calculation procedures or from another functional or navigational button
on the calculation spreadsheet. For example:
Sub CreateReport()
‘place solutions in report table
‘create histogram
Application.Run "ATPVBAEN.XLA!Histogram", Input, Output, Bin, Labels,
Pareto, Cumulative, Chart
Worksheets(“Output”).Visible = True
End Sub
These procedures should outline the overall flow of the application from user input to
calculations to output. Aside from these, we should also include any required
navigational procedures for “End” buttons or “Next,” “Back,” or “View” buttons. For
example:
Sub EndProgram()
Worksheets(“Welcome”).Visible = True
ActiveSheet.Visible = False
End Sub
We must also ensure that all variables are declared and that any variables used in
multiple procedures are declared as Public variables at the top of the module.
Receive Input Store the values from form controls or input cells to
corresponding variables. Record these values to
the appropriate cells in the calculation sheet.
Perform Perform calculations using function procedures,
Calculations simulations loops, or Solver commands.
Can the user easily modify the input to resolve the problem without having to re-enter all
the input from scratch? We should ensure that this is possible by confirming that the
user’s initial input values are preserved when re-displaying a user form or input sheet.
We must make sure that we do not call a ClearPrevious procedure unless the user has
indeed restarted the entire application. Also, we don’t want thedefault values to overwrite
the user’s last input values when re-showing a user form. This revision allows users to
quickly modify one or several parts of the input and resolve the calculations to compare
results.
Can the user change other parts of the calculations or the model when resolving? In
other words, we do not want the user to be limited to only modifying input values when
resolving. The user should be able to change some constraints or objectives, as well.
We therefore try to keep our application dynamic so a user can experiment with different
problem dimensions. This may not be possible or applicable for every DSS, but if it is, it
should be made available to the user. If certain dynamic options are not available to the
user, we state our assumptions clearly on the “Welcome” sheet to say so.
With resolve options, we may want to provide the user with a way to compare various
results or scenarios. We may also want to store multiple solutions for this comparison or
sensitivity analysis. So, we consider what the user is really interested in learning from
our DSS, recalling that a DSS is designed to aid a decision maker in making a decision.
We double-check that the results of the application are indeed beneficial to this decision
making process.
We will now provide a few examples from our case studies; a detailed description of the
DSS components can be found in each case study chapter in this part of the book. Let’s
first consider the “Reliability Analysis” case study in which a user is analyzing a parallel
series system of three machine types. After providing the necessary input, a simulation
runs to determine the mean failure time of the system and how often a particular
machine type caused system failure. Figure 23.16 displays the output sheet for this
study.
Figure 23.16 The output sheet for the “Reliability Analysis” case study.
This case presents two resolve options to the user. The first option is to return to the
simulation sheet and modify the initial input values in a given table (see Figure 23.17).
The user can then re-run the simulation and view the updated results.
Figure 23.17 The first resolve option: modify input in table and rerun simulation.
The second option is for the user to improve the system by adding one machine of a
particular machine type. To aid the user, or decision maker, in deciding which machine
type to add, we first run an optimization in the background and suggest the optimal
choice. We do not enforce this decision, but instead try to aid the decision maker. This
information is presented on a user form (see Figure 23.18).
Figure 23.18 The second resolve option: making a suggestion to aid the decision maker.
After the user selects a machine type, one machine of this type is added and the
simulation is rerun. The updated results then appear.
Another example is from the “Inventory Management” case study (see Figure 23.12). In
this case, the user can actually change the model base along with the input each time
the application is resolved. The user can decide which inventory model to use: Standard
EOQ, Backorders, or Reorder Point. There is also an input table, which can be modified,
on the same sheet.
In another example, the “Portfolio Management” case study (see Figure 23.13), after
users have created their portfolios, they can optimize their investment strategy by
minimizing risk (see Figure 23.19).
After filling the input in the user form for the optimization (Figure 23.19(a)), the resulting
optimized investment strategy displays on an output sheet (Figure 23.19(b)). However, if
the optimization is infeasible, or if the users want to experiment with different values,
they can either return to the input form to experiment with different values (by pressing
the “Modify Input” button), or return to the portfolio sheet to modify their stock selection
(by pressing the “Modify Portfolio” button). An extension to this case study may be to
allow users to modify their objective in optimizing their investment strategy; currently, we
assume that we minimize risk, but users may also want to maximize returns.
(a)
(b)
Figure 23.19 The two “Modify” buttons give the user different resolve options.
Resolve options are important DSS components. To ensure that the DSS is aiding the
decision maker, we allow the user to modify inputs or calculation options.
23.7 Summary
¾ There are five basic steps to develop a DSS application: Application Overview,
Spreadsheets, User Interface, Procedures, and DSS Components.
¾ The outline of the spreadsheets in the DSS should include: Welcome Sheet,
¾ Input sheet, Calculations sheet, and Output sheet.
¾ The spreadsheet user interface includes: Navigational Buttons, Functional
Buttons, Controls on the Spreadsheet, and User Forms.
¾ The outline of the procedures in the application should include: a Main
procedure, a Clear Previous procedure, a procedure to receive input, some
procedures and functions to perform calculations, and a procedure to generate
output. There may also be some Navigational procedures. Ensure that you also
outline what Variables you may need and if they are public or private.
¾ DSS Components include Resolve Options. The user should be able to modify
inputs, calculation options, constraints, objectives. Aid the decision maker in
making the best decision.
23.8 Exercises
23.8.1 Review Questions
Chapter Overview
24.3 Summary
24.4 Exercises
Chapter 24: GUI Design and Programming Principles
There are many GUI design books that list several different principles and guidelines for
effective GUI design. We present here a summarized version of what we feel are the
most important theoretical points when working in spreadsheet-based DSS applications.
These are: know the users and theirtasks and goals; maintain clarity; and stay
consistent.
It is important to know who the users of our application are. We ask ourselves: Are they
managers? If so, how deep is their understanding of the problem? Do they know the
model or algorithms being used to perform the calculations? What terminology do they
use to discuss the problem? If the users do not possess a highly technical understanding
of the application’s topic, then we should try to avoid overwhelming them with the details
of the model or calculations. An effective way to do so is to hide the calculation sheet.
Additionally, we provide instructions and label the input without relying on technical
terminology. For example, instead of labeling input C or D, we should assign meaningful
descriptions such as “Annual Cost” or “Annual Demand.” In the case that our users do
have a more technical understanding, we should present and explain the calculations
and assumptions. We may also want to provide more details with the terminology that
they are familiar with. While programming, we must always remember: know the user.
And we must also remember that the user is using our application to complete a task or
tasks and to achieve a goal. It is important to ensure that the user interface is an aid to
the user in completing these tasks so that the he or she feels that the DSS has served
its purpose of efficiency and convenience.
The user’s task domain includes “the data that users manipulate, the manner in which
that data is divided … and the nature of the manipulation that users perform on the data”
(Johnson). Remember that the users’ tasks are already necessary without the help of a
DSS; therefore, we must ensure that our DSS application aids them in completing these
tasks in the same domain that they are familiar with. These tasks should be organized
on some priority or hierarchy base in order to create a flow for the application. This flow
influences the outline of the entire application, as we discussed in the first section. Our
interface should guide the users so that they can work with the data in their task domain
in the order in which it needs to be completed.
2
Chapter 24: GUI Design and Programming Principles
For example, let’s suppose that the users’ task domain involves researching some
historical data, then computing a mean and standard deviation of this data, and finally
entering these values into a forecasting model. Based on the result of the model, the
users must determine the forecast demand for the next month and place an order of that
size. When constructing the interface for a forecasting DSS, we ensure that these tasks
are presented to the users in the same order. First, we ask them to enter the historical
data. If they usually get this data as a text file from a coworker, then we do not ask them
to enter it manually; instead, we prompt them to import the text file. Next, we
automatically calculate the mean and standard deviation and display the figures to the
users. Afterwards, we ask for some extra input for the forecasting model, but we try not
to get too technical. We then clearly display what their order amount should be, based
on this forecast. Because our users would find the DSS hard to learn, we do not try to
reorganize their tasks. We keep our task presentation simple so our users experience an
element of familiarity.
• What does the user understand about the problem and the model
calculations?
Summary
• What terminology is the user familiar with?
23.1.1.2 Clarity
A user interface is the communication between the users and the application; therefore,
if we want the users to use our application correctly, we must communicate clearly to
them how to use it. First and foremost, we need to make sure that there is a clear
description of what is involved on every spreadsheet and every form. For example, in
Figure 24.1, we display the calculation sheet for a “Sales Force Allocation” case study.
We ask the user to enter bound values for the optimization constraints. We then give
them two calculation options. We explain the users’ tasks in a text box at the top of the
sheet. We have bolded the button names and column names in the text to help the users
quickly identify the location of the tasks on the sheet.
3
Chapter 24: GUI Design and Programming Principles
Figure 24.1 Giving clear instructions and descriptions on each sheet and form.
The functionality of any button or control should be clear to the users. On spreadsheets,
we try to create some separation between navigational buttons and functional buttons.
For example, if the buttons “End,” “Back,” and “Solve” appear on an input sheet, it is
better to keep the navigational buttons “End” and “Back” together and to place the
“Solve” button somewhere else on the sheet.
(a) (b)
Figure 24.2 (a) The buttons are mixed, so functionality may be unclear. (b) Buttons are
clearly separated into navigation and calculation groups.
Likewise, on user forms, we ensure that functional buttons are separate from the “OK”
and “Cancel” buttons.
Aside from buttons and command button controls, all other controls should also be
clearly labeled so that their functionality is understood. We never leave a text box
unlabeled or assume that the users know what to enter. Likewise, we ensure that list
boxes and combo boxes are labeled so the users know what the list contains. Frames
containing grouped items should also be labeled to signify the grouping. The clearer the
controls are, the quicker the users can learn their functionality and the easier it is for
them to use the application. For example, compare Figure 24.3(a) and Figure 24.3(b);
without clear control labels, users have to hesitate and guess what information we are
asking for.
4
Chapter 24: GUI Design and Programming Principles
(a) (b)
Figure 24.3 (a) Control functionality is not clear. (b) Labels clearly designate control
functionality.
Another way to clarify control functionality is by creating control tip messages; these
provide more detailed instructions without cluttering the form. For example, Figure 24.4
illustrates a control tip for a combo box. When the users place the cursor over the combo
box, the text reads, “This list contains all products in the system.”
Figure 24.4 Control tips are also useful for clarifying control functionality.
Another benefit to clarifying user interface functionality is that it can reduce the errors
encountered by users. The most frequent user errors involve inputting values in an
incorrect format or type or choosing a selection or command button at an inappropriate
time. Even though error checking can be done, as discussed in Chapter 22, developing
a better-designed user interface can reduce this extra coding. Aside from clearly labeling
controls, we can also assign a default value as an example of what kind of input the
users should enter. We may also guide the users for proper formatting issues.
For example, referring to Figure 24.3(b), if the users are supposed to enter a cost, they
may enter “$20,000” or “20,000” or “20000.” If we do not want the user to enter “$,” “,” or
punctuation marks, then we need to convey this to them on the interface design. We
either write more specific instructions or guide them with default values; otherwise, we
must conduct some error checking in our code to ensure that a data type error does not
occur when we try to perform an operation on the users’ input values (see Figure 24.5).
5
Chapter 24: GUI Design and Programming Principles
Some other common formatting examples include those for numerical input such as
social security numbers and telephone numbers. In Figure 24.6(a) we show that the user
may input these values in various formats. This may cause errors when storing,
searching for, or performing operations with the data. Figure 24.6(b) has clarified the
formatting issues so that the users only enter numerical values without extra
punctuation.
(a) (b)
Figure 24.6 (a) Unclear formatting. (b) Clear formatting with default values.
If there is still a user error while using an input interface, we need to make sure that a
clear error message is provided to the users. The users should understand what they did
wrong and what they need to do to correct the problem. For example, the error message
“Incorrect input!” is not helpful to users. However, a message such as “You may not
enter negative numbers. Please enter a positive number” redirects the users to correct
the error. Errors should be hard to make and easy to correct.
Overall, clarity is very important in effective GUI design. It is essential to check sheet
and form instructions, control labels, and data input guidelines to ensure that the user
can clearly understand what to do.
23.1.1.3 Consistency
The third theoretical method by which to achieve effective GUI design is consistency.
Users are inclined to interact with an interface according to how their expectations. In
other words, they may expect an input prompt, button locations, and viewable options
based on their familiarity with working with the problem or with other interfaces. It is
important that within our application, or across similar applications, we keep certain
features of the user interface consistent.
The first place to design for consistency is on the spreadsheets. On each sheet in the
application, we try to keep the title, sheet description, and instructions all in the same
location. This way, if users are looking for an explanation of what is included on a
particular sheet, they can always refer to the same location on the sheet. We tend to
keep sheet titles and descriptions at the top left of each sheet layout. Also, we ensure
that the navigational buttons, especially the “End” button, is in the same location on each
sheet. Users should not have to search through the sheet to try to exit the application.
Compare the forms presented in Figure 24.7. We should also consider consistency in
the sheet layout for input cells and charts. For example, if we have multiple output
sheets, each with a chart, the charts should all be in the same position on each sheet.
(a) (b)
Figure 24.7 (a) The buttons are different shapes and different colors. The navigational
buttons are in different locations on the sheet. (b) The navigational buttons are
together and consistent.
When designing user forms, consistency can be enforced in several ways. First of all, as
with sheets, we need to ensure that a description label is always at the top of the form.
7
Chapter 24: GUI Design and Programming Principles
Also, we keep navigational command buttons such as “OK” and “Cancel” in the same
position on all forms. If “OK” is on the bottom right of a form and “Cancel” is on the
bottom left, we do not switch them on subsequent forms. Users should not feel tricked
into pressing the wrong button.
Regarding form controls, the alignment and size features can also improve the form
layout. So, we try to keep text box sizes the same throughout the form and align them
equally. We keep all buttons the same size as well. Similarly, we try to be consistent with
punctuation, such as colons. Compare the forms presented in Figure 24.8.
(a) (b)
Figure 24.8 (a) Buttons are different in size and in different locations. Colons are used
inconsistently. Textbox sizes differ. Numbers in the list are formatted differently. (b)
Grouped textboxes are of the same size. Colons are consistent. The numbers in the
list are consistent. Instructions are clearer.
If a form instruction label and command buttons are consistent in our application’s forms,
we can save interface development time by saving the first form we create and importing
it multiple times. (Refer to Chapter 18 for detailed instructions on how to do this.)
Another feature of form consistency that can aid the users in moving quickly through the
interface is the tab order of the form. We ensure that the tab order takes the users from
the top of the form down to the “OK” button and finally to the “Cancel” button. Again, the
idea is to help the users feel that the user interface is easy to learn and easy to use.
Keeping the forms consistent throughout an application aids the users in accomplishing
their tasks more quickly and efficiently.
We would now like to provide several examples of effective and ineffective GUI designs.
We have grouped these examples by different control types and a few more general
categories that apply to the entire user form.
23.1.2.1 Buttons
As previously stated, buttons should always be of the same size and shape. We also
should try to keep buttons the same color unless highlighting a particular function button
to guide the users. Grouping functional buttons together and navigational buttons
together is also a highly effective way to maintain interface consistency. We have seen
examples of this technique in Figures 24.21 and 24.26.
To reduce the memorization requirements of the users, we replace text boxes with list
boxes or combo boxes when possible. This technique also reduces the possibility for
errors. For example, if users are asked to enter a student name for their class grades,
using a combo box would prevent them from entering the name of a student who is not
in their class or from misspelling a student’s name. Compare the forms in Figure 24.9.
(a) (b)
Figure 24.9 (a) Using a text box forces the user to remember all the names and to spell
them correctly. (b) Using a combo box reduces user memorization and the chance for
errors.
When applying tab strips or multi pages, we try to minimize the number of tabs. Too
many tabs can cause tab positions to shift when clicked; the user may not see all of the
tabs and leave an input blank. So, we reorganize our input needs into multiple forms or
9
Chapter 24: GUI Design and Programming Principles
combine tab information to reduce the total number of tabs. Compare the forms in Figure
24.10.
(a) (b)
Figure 24.10 (a) Too many tabs. (b) Combining the tab strip and multi page into one
form with several text boxes.
Only option buttons should be used for mutually exclusive input. Even though option
buttons can also be used for non-mutually exclusive input, if they are not grouped in a
frame, we recommend check boxes for this purpose instead. We must be consistent in
our use of check boxes and option buttons for these respective purposes. Compare the
forms in Figure 24.11.
10
Chapter 24: GUI Design and Programming Principles
(a) (b)
Figure 24.11 (a) Option buttons are used inconsistently. (b) Option buttons are used
for mutually exclusive options and check boxes are used for other options.
For option buttons, we should always have more than one option grouped in a frame. If
there is only one option, then we treat it as an “on/off” option and use a check box.
Another way to modify only one option is to create another option with an opposite value.
For example, instead of just giving the users an “on/off” option for “Assume non-negative
values,” we could create another option, such as “Do not assume non-negative values”
or “Allow positive values.”
23.1.2.5 Frames
Frames can group similar items. We should therefore always have at least two controls
in a frame. However, if we have more than one frame on a form or sheet, to separate
different groups of controls, we may end up with only one control in one of the frames.
We should still avoid including all frames with only one control or only one frame with
only one control. Compare the forms in Figure 24.12. To apply frames with controls on
the spreadsheet, we can use shape boxes.
11
Chapter 24: GUI Design and Programming Principles
(a) (b)
Figure 24.12 (a) Using frames as labels. Two of the frames only have one control. (b)
The two frames have more than one control each.
Labels should be used for read-only information. We do not use a text box to present
information to users when they should not be able to modify it. For example, suppose a
form receives input for three machine types in a production system. We may use a loop
to display the same form to the users three times to receive the input for each machine
type. We can reveal the machine type number to the users, but we do not want them to
modify it; therefore, we use a label, not a text box (see Figure 24.13)
(a) (b)
Figure 24.13 (a) Users should not change the “Machine #” and “System” values, so they
should not be in text boxes. (b) Using labels instead. The labels are a different color
in order to demonstrate that their values have been set.
12
Chapter 24: GUI Design and Programming Principles
For dynamic controls, we may be making some controls visible or not visible or we may
keep them visible but make them inactive. To make a control inactive, we must gray it
out to an extent that users are not confused about whether or not they can change the
value in the inactive control (see Figure 24.14). We should also lock inactive controls so
they cannot be modified. If a control becomes inactive, we should also set the Tab Stop
property to False.
(a) (b)
Figure 24.14 (a) The “Username” and “Password” are inactive since “New User” is
selected. (b) The “Email Address” is inactive since “Returning User” is selected.
When using multiple forms, we must make sure to hide or unload sequential forms so
there is no layering on the screen. The only time a layering may occur is if there is a sub
form that must be filled before a main form can be completed. However, we should try to
avoid such situations.
Associating actions with event procedures can be confusing to users. For example,
when using command buttons, it is better to apply the Click event rather than the
MouseDown event. Users may press the mouse down, not see a result, and think that
the form is not working.
For text boxes, it is more efficient to use the AfterUpdate event rather than the Change
event, which may cause errors if certain formatting or values have been enforced. For
example, suppose we error check to ensure that a number has been entered in a text
box. If the user is deleting a previous value to enter a new one, then when the text box
changes from one value to empty, the error will occur. Instead, we use the AfterUpdate
event so we only check the value of the text box once the new value is completely
entered.
13
Chapter 24: GUI Design and Programming Principles
For check boxes, we recommend applying the Change event instead of the Click event.
However, be aware that with mutually exclusive option buttons, the Change event may
be over-triggered, so the Click event may be more desirable.
Check Boxes versus Use option buttons for mutually exclusive options;
Option Buttons use check boxes otherwise. Use check boxes for
“on/off” options or create an opposite option for
option buttons.
Frames Try to have more than one control per frame and
more than one frame per form.
Your code should reflect a consistent style; that is, it should appear that the same
person has developed all of the code. Spacing, indenting, line length, and other
formatting should be consistent. We recommend keeping procedures and functions
14
Chapter 24: GUI Design and Programming Principles
spaced far enough apart so it is easy to scan through the code. We also recommend
indenting to signify the beginning and end of a loop or logical structure. For example,
consider the two examples below:
If A is True Then
‘actions 1
ElseIf B is True Then
‘actions 2
End If
Clearer indenting:
If A is True Then
‘actions 1
ElseIf B is True Then
‘actions 2
End If
We also recommend being consistent with the code’s line length. It is better not to have
to scroll back and forth through a module to read various lines of code. So, we try to
break lines at logical places, while keeping line length as consistent as possible. Coding
style can also be observed in the naming standards and commenting style, which we will
discuss in later sections.
Another area where style should be consistent is in error checking. We must be sure that
we do not just check input from one form but not another or that we do not check input
from a Message Box but not a form. Whatever methods we use for error checking, we
try to apply them to all of our error checking routines, if possible. In general, when we
are outlining our procedures, we ensure that there is some consistency in our coding
approach.
24.2.2 Naming
Some other common naming standards refer to control names. We provide a list of the
starting name values for various controls in Chapter 18. For example, the name of a text
15
Chapter 24: GUI Design and Programming Principles
box should begin with “txt”; the name of a form should begin with “frm.” For Boolean
variables, we recommend starting the variable name with “Is” or “Do,” for example:
“IsDone” or “DoAnimation.” For constants, we recommend writing the names in all
uppercase, such as: “PI” or “INFINITY.”
We try not to make procedure names too vague. For example, instead of “Calc,” we use
a more descriptive name, such as “CalcReturn.” For function procedures, we may also
use names that signify the returned value, for example: “FindMinCost” or “GetUserInfo.”
In general, we use names that are not easily confused. This strategy helps to avoid
coding errors and ease debugging.
24.2.3 Comments
Commenting is a good habit to have as a coder. Comments help clarify what we have
done and why we have done it. They also benefit another programmer who may look
through our code later. So, we keep our comments up-to-date as we make changes or
updates to the code.
Using comments organizes our code and makes it easier to read. However, we must be
careful not to overdo it; we do not want to include unnecessary comments that only add
length to the code.
24.2.4 Efficiency
Another way to improve code efficiency is to ensure that extra memory is not being
used. For example, there is no need to declare a counting variable “i” as a Double; it
only requires the memory of an Integer data type. We also ensure that arrays are
dimensioned for the needed storage space with the ReDim Preserve statement.
We should also write our code to be dynamic for future extensions or updates. For
example, for bound variables, array sizes, or range values, we may want to use
variables instead of values. Consider the following examples:
Static structure:
For i = 1 to 10
‘do actions
16
Chapter 24: GUI Design and Programming Principles
Next i
Dynamic structure:
For i = 1 to NumProducts
‘do actions
Next i
--------------------------------------
Static structure:
Dynamic structure:
--------------------------------------
Static structure:
Dynamic structure:
In the dynamic structures in the above examples, it is easy to modify the code by
assigning a new value to the extra variable; otherwise, we would have to make multiple
modifications throughout the code.
24.3 Summary
¾ Designing an effective GUI involves the following theoretical points: knowing the
users and their tasks and goals, being clear, and being consistent.
¾ The most important programming principles are: writing with a consistent style,
using naming standards, including clear comments, and improving code
efficiency.
24.4 Exercises
24.4.1 Review Questions
1. What are some of the common errors made with option buttons?
2. Should you use only one frame in a user form?
3. What are some ways to enforce clarity in a user interface?
4. How do you define the user’s goals and tasks?
5. Give some examples of consistent and inconsistent use of navigational and
functional buttons in a DSS.
6. What are the main programming principles?
7. Give an example of a code before and after its efficiency has been improved.
18
Case Study 1
Eight Queens
Chapter Overview
CS1.2 Spreadsheets
CS1.3 User Interface
CS1.4 Procedures
CS1.5 Resolve Options
CS1.6 Summary
CS1.7 Extensions
Case Study 1: Eight Queens 2
The eight queens problem has 92 distinct solutions. If we exclude symmetry, there are
23 distinct solutions. We could generalize this problem into an N queens problem, which
would involve an N*N board and require us to place N queens so that none of them is in
the line of attack of any other queen. However, for this application, we assume the chess
board size is fixed at eight by eight.
To generate each solution, we use a simple algorithm. We must determine how to place
eight queens on the board so that none of them can attack another. In other words, no
queen can be in the same row, in the same column, or on the same diagonal. We know
we can only place one queen on each row of the chessboard; therefore, we examine
each row at a time to determine on which cell in that row, or in which column, a queen is
placed. For each placed queen, we keep track of its row and column index. These
indices will be used to check that no two queens have the same column index, have the
same row index, or lie on the same diagonal.
As we examine each row, we initially place a queen in the first “legal” cell. That is, if we
begin by examining the first row of the board to determine where to place the first queen,
we find that the first cell, or the first column, is “legal” since no other queens have been
placed. We therefore place the first queen in this first cell of the board’s first row. We can
then continue to the second row of the board and find that the first column is “illegal”
since the first queen has already been placed there. The second column is also illegal
since it conflicts with the first queen’s diagonal. However, the third column is legal;
therefore, we place the second queen in the third column of the second row since it is
the first legal cell we find. (See Figure CS1.2.)
We continue using this method for all the rows of the board. However, we may find a row
in which no cell, or no column, qualifies as a legal placement for the queen currently
being placed. In other words, depending on the locations of the previously placed
queens, we may not find an available column or cell that does not conflict with the
diagonal of another queen. In that case we must “backtrack.” We do not place the
current queen, but move back to the previously examined row and then move that
already-placed queen to a different legal cell. For example, if we have placed queens in
rows 1 through 4 but cannot find a legal placement for the queen in row 5, then we
would return to row 4 and try to move that queen.
Since we are initially always placing the queens in the first legal cells, we may have
ignored other legal options. For example, the second queen we placed in Figure CS1.2
could have also been placed in columns 4 through 8 of the second row. Therefore, we
try a different legal position for the queen placed in the row above the queen for which
we could not find a legal placement.
After moving the queen above it, we return to the queen that we were positioning and
see if a legal position has now become available. For example, suppose we revisited
row 4 and moved the queen to a new legal position; we would then return to row 5 and
try to place the queen we left. If there is still no available legal position, then we continue
backtracking until shifting a queen in some previously examined row eventually opens a
legal position for the current queen. We may continue using this backtracking technique
until we return to row 1 and have tried all possible legal positions for the queen in this
row. Once all positions have been tried, we can declare that all the possible solutions
have been generated. An example of a complete solution appears in Figure CS1.3. The
Place and IsLegal procedures are used to implement this algorithm; refer to section
CS1.5.
Since there are many possible solutions, 92 for the symmetric eight-by-eight board
scenario to be exact, we allow the user several options to view these solutions. The first
allows the user to generate one solution at a time. We assume that the solutions are
unique; in other words, none are repeated. The second option allows the user to
generate several solutions at a time. For example, he or she may generate five solutions
at once. The third option allows the user to generate all the possible solutions.
CS1.1.2 Input
The only input for this application is the number of solutions to generate if the user
chooses the second option for viewing the solutions.
CS1.1.3 Output
The output for this application is the solution set of the eight queens problem.
CS1.2 Spreadsheets
This application requires two spreadsheets: the welcome sheet and the solution sheet.
The welcome sheet contains the title and the description of the application as well as an
image. (See Figure CS1.4.) The “Start” button on the welcome sheet brings the user to
the solution sheet.
The solution sheet displays all of the possible solutions to the user. (See Figure CS1.5).
These solutions are displayed as chessboards with images of queen pieces in the
placements generated by the solution. The user can select between the three options for
viewing these solutions using a floating form visible on the solutions sheet. We will
discuss this form in more detail in the next section.
Since these placements are unique, a new chessboard is shown for each solution. All of
the solutions that the user generates are stored sequentially on the sheet so they cab be
compared. (See Figure CS1.6.)
closed for the user to scroll up and down the various solutions on the spreadsheet. The
form includes labels to give the user clear descriptions of their options, which are
indicated by the following five buttons: “Generate Next Solution,” “Generate Next K
Solutions,” “Generate All Solutions,” “Restart,” and “End Program.” The first three
buttons correspond directly to the users’ options of viewing the solutions. The “Restart”
button clears all of the solutions displayed, and the “End Program” button brings the user
back to the welcome sheet.
Figure CS1.7 The “Options” user form, which is a floating form on the solutions sheet.
If the user selects to generate several solutions at a time by pressing the “Generate Next
K Solutions” button on the form, an input box appears. This input box prompts the user
for the number of solutions that he or she wants to generate. In other words, the input
box prompts the user for the value of K. (See Figure CS1.8.)
Figure CS1.8 The input box used for generating multiple solutions.
CS1.4 Procedures
We will now outline the procedures for this application, beginning with the initial sub
procedure and the variable definitions. (See Figure CS1.9.) The “Main” procedure
initializes the variables and calls the “ClearPrevious” procedure; it then takes the user to
the solutions sheet and displays the “Options” form.
The “ClearPrevious” procedure initializes some variables and clears the solutions sheet.
(See Figure CS1.10.) The chessboards from the previously displayed solutions are
cleared by resetting the cell colors and borders. The queen images are cleared with the
“ClearQueens” procedure. (See Figure CS1.11.) The “ClearQueens” procedure loops
over all the drawing objects on the sheet to count the number of queens. It then deletes
the queen image that many times.
The user is now on the solutions sheet and can use the “Options” form for the user
interface. Figure CS1.12 outlines the procedures associated with each of the buttons on
this form. The “cmdRestart_Click” procedure simply calls the “ClearPrevious” procedure
to clear the solutions sheet. The “cmdEndProg_Click” procedure calls the
“ClearPrevious” procedure and returns the user to the welcome sheet. Finally, the
“cmdAllSoln_Click,” “cmdKSoln_Click,” and “cmdNextSoln_Click” procedures all call
their corresponding procedure in the main module: “AllSolns,” “KSolns,” and “NextSoln,”
respectively. (See Figure CS1.13.)
Figure CS1.13 The procedures for the three options for viewing solutions.
These three main procedures, “AllSolns,” “KSolns,” and “NextSoln,” all call the “Place”
procedure, which executes the algorithm for placing the queens. (Refer to Section
CS1.1.1.) This procedure requires that an iteration value be passed to it. From the
“NextSoln” procedure, we pass an iteration value of 1; from the “AllSolns” procedure, we
pass an iteration value of 100 (or any number >= 92); and from the “KSolns” procedure,
we pass an iteration value of K. To find K, we prompt the user with the input box
described in Section CS1.3. To ensure that the user enters a numeric value for the
number of solutions to generate, we use some error checking. If the user presses the
“Cancel” button on the input box, we bring him or her back to the welcome sheet.
The “Place” procedure generates the number of solutions specified by the iterations
value it receives when called. (See Figure CS1.14.) This iteration value is used as the
counter in the main “For, Next” loop. The large “Do, While” loop performs the algorithm
to generate one solution. This “While” condition checks that the row number, x, remains
less than the size of the board, in this case 8. For each row, the queen position is shifted
to all possible columns (from left to right, or 1 to 8), checking each time if the position is
legal. This check is performed with the “isLegal function.” (See Figure CS1.15.)
The “isLegal” function receives the row and column value of the queen currently being
placed. The column value passed is checked to determine if it is a value position or if it
should be shifted once more. If the row value, x, is 1, then the position is legal. Even if
we are backtracking, we assume that any location of the first queen on row 1 is valid.
Otherwise, we compare the columns and diagonals of all queens placed in the previous
rows to determine if there is a conflict with the queen currently being placed. We check
the columns by comparing the passed column value for the queen we are currently
placing to the column array value (column(j)) of each of the other queens (j). We check
the diagonals by comparing the absolute difference between the current row number (x)
and each queen number (j) with the absolute difference between the passed column
number (y) and the column array value of each queen. A Boolean value is set to true to
indicate that the passed column value of the current queen is a legal position if no
conflicts are found.
After this part of the “Place” procedure is done, we check to ensure that the queen was
placed in a legal position. If the column location of the queen is greater than the size of
the board, then we know no legal position was found. In that case, we backtrack by re-
initializing the illegal queen’s column and resetting the row count by one. The main “Do,
While” loop now repeats. If all the rows have been revisited in the backtracking and no
legal position was found for row 1, then we can declare that all the possible solutions
have been generated. If legal positions were found for each queen, then our row value
exceeds the board size and the main “Do, While” loop is exited. We then decrease the
row counter by one again to ensure that the next time the “Place” procedure is called, we
begin by backtracking. This step ensures that all the solutions generated are unique.
Next, we call the “Display” procedure. (See Figure CS1.16.) This procedure displays a
new chessboard for the generated solution and copies an image of a queen piece onto
each position for the current solution. A solution number also appears above the
displayed solution.
Figure CS1.17 displays a new sheet in which the user can specify which queens to fix.
To let the user fix the queens, we use a “Worksheet_SelectionChange” event procedure.
(See Figure CS1.18.) This procedure checks the location of the selected cell to ensure
that it is inside the board area. If so, then it checks to make sure no other queen has
been placed there previously. If this is also true, then a queen image is copied and
pasted onto the selected cell.
When the user is finished fixing the queens, he or she presses the “See Solutions”
button, which is assigned to a new procedure, the “UserProgram” procedure. (See
Figure CS1.19.) This procedure records which queens are fixed by noting the column
and row numbers. A new array also stores a Boolean variable of whether or not a row
has a fixed queen. When the algorithm is executed, this array will be checked so that the
rows with fixed queens are skipped. The “UserProgram” procedure then performs a
quick error check to ensure that the user did not place queens in illegal positions. Finally,
the user is taken to the solutions sheet, and the “Options” form appears.
Now, when the user returns to the solutions sheet, any solutions generated highlight the
fixed queens. (See Figure CS1.20.) The user still has the same three options for viewing
the solutions. Notice, however, that the “Generate All Solutions” option now generates
less than 92 solutions. Also note that in some cases no solutions are possible, given the
positions of the fixed queens.
Figure CS1.20 The fixed queens are highlighted in all the generated solutions.
We have also added one more user form that gives the user the option between
generating all the solutions with or without fixing some queens first. (See Figure
CS1.21.) This form is now shown when the “Start” button is clicked on the welcome
sheet. The procedures for this new form simply determine whether or not to take the
user to the new sheet for fixing the queens or to the solutions sheet. (See Figure
CS1.22.) If the user is taken to the solutions sheet, then the “Options” form also appears.
Figure CS1.21 The new user form that allows users to fix queens.
We have also made a few modifications to the procedures. As mentioned above, the
new form is now called from the “Start” button on the welcome sheet. In other words, we
have modified the “Main” procedure to display this new form instead of taking the user
directly to the user form and showing the “Options” form. (See Figure CS1.23.) The new
Boolean array for storing fixed queens is also initialized in this procedure.
We also modify the “Place” procedure. (See Figure CS1.25.) Now is when we use the
new Boolean array to determine whether or not the algorithm should skip a row in which
a queen has been fixed. We also check this array to ensure that a row with a fixed
queen is not re-examined during the backtracking loop. In the end of the procedure, we
check if the first iteration was able to generate a solution. If it was not able to, then we
inform the user that the positions of their fixed queens were too constrained to find a
possible solution.
The last procedure we modify is “Display.” (See Figure CS1.26.) Again, we use the new
Boolean array to check if a queen position was fixed by the user. If it was, then we not
only paste a queen image, but we also highlight the cell to indicate to the user that it is a
fixed queen.
CS1.6 Summary
¾ The eight queens problem consists of placing eight queens on a regular eight by
eight chessboard so that none of them is in the line of attack of any other queen.
¾ This application requires two spreadsheets: the welcome sheet and the solution
sheet. (Note that a new sheet is added with the resolve option.)
¾ For this application’s user interface, we use buttons, a user form, and an input
box.
¾ There are several procedures for this application that execute the algorithm for
generating possible solutions.
¾ The user can resolve the application by using the “Restart” button on the
“Options” form or by switching between any of the options for viewing the
solutions. There is also a more complex resolve option that allows a user to fix
certain queens before generating solutions.
CS1.7 Extensions
1. Generalize this problem to an N queens problem, which can use an N*N board
and place N queens so that none of them is in the line of attack of any other
queen. Prompt the user for the value of N before beginning the application.
2. Design an alternative user interface for the solutions sheet. Can you use a
control that allows the user to scroll through the multiple solutions on the sheet
without using the spreadsheet’s scroll bars?
3. Knights Trail Problem: You are given a chessboard and a knight, as well as the
knight’s starting position. The problem is to visit each square of the chessboard
(exactly once) with a minimum number of moves. In other words, the trail left by
the knight should be the shortest one possible. Develop a system that solves this
problem.
4. Queen Domination Problem: Determine the least number of queens that can be
placed on a chessboard so that each square either contains a queen or can be
attacked by one.
5. Crowded Board Problem: You are given a chessboard along with 8 queens, 8
rooks, 21 knights, and 14 bishops. The problem is to place these 51 pieces on
the chessboard so that no queen can attack another queen, no rook can attack
another rook, no bishop can attack another bishop, and no knight can attack
another knight. Ignore the intercession of pieces of another type from the one
under consideration. In other words, two queens can still attack each other even
if there is a bishop, a rook, or a knight between them. Develop a system to solve
the above problem.
Chapter Overview
CS2.2 Spreadsheets
CS2.3 User Interface
CS2.4 Procedures
CS2.5 Resolve Options
CS2.6 Summary
CS2.7 Extensions
Case Study 2: Projectile Motion 2
We use standard physics equations to calculate the projectile motion. Given the initial
condition of the object, we display its motion and plot its velocity and acceleration as a
function of time until it hits the ground. The values that define the object’s initial condition
are the initial height, initial velocity, and initial angle. The user provides these initial
values.
For the projectile calculations, we will also use values for time, a time increment for
animation, x-position (or distance) and y-position (or height), and x-velocity, and y-
velocity. Additionally, we will employ a constant for the gravitational force, which we set
at 9.81. We assume that, initially, the x-position and y-position are zero.
With the following equation, we calculate the total time that the projectile motion
requires:
We calculate the initial x-velocity and y-velocity with the following two equations:
We calculate the highest point of the projectile motion and the time at which this
maximum y-position is reached as follows:
Max Y-Position = Initial Height + Initial Velocity * (Sin(Initial Angle)) * Time at Max
Y-Position – (Gravitational Force / 2) * (Time at Max Y-Position)2
As we animate the projectile motion, we determine its x-position, y-position, velocity, and
angle for each time increment. We do so using the following equations:
We assume that the projectile motion stops at the ground, at a y-position equal to zero.
For more details on projectile motion and the equations described above, please see
Fundamentals of Physics by Halliday, Resnick, and Walker.
CS2.1.2 Input
The input for this application is the initial height, initial velocity, and initial angle for
whichever projectile scenario the user chooses.
CS2.1.3 Output
The output for this application is the path of the projectile motion.
CS2.2 Spreadsheets
We use two spreadsheets in this application: the welcome sheet and the projectile sheet.
The welcome sheet contains the title and the description of the application, as well as an
image. (See Figure CS2.1.) The “Start” button on the welcome sheet brings the user to
the projectile sheet.
The projectile sheet includes a chart to animate the projectile motion and a table to
display the values of the x-position, y-position, velocity, and angle for each time
increment. (See Figure CS2.2.) The picture next to the chart corresponds to the
projectile scenario, and the “Solve” button begins the projectile animation. The “Resolve”
button allows the user to change the projectile scenario and/or input values, and, finally,
the “End” button brings the user back to the welcome sheet.
Figure CS2.3 displays the projectile motion for the cannon scenario while it is being
animated. Notice that the chart is updated for each new row of values in the table.
Figures CS2.4, CS2.5, and CS2.6 exhibit the final projectile motions for the cannon,
plane, and cliff scenarios, respectively. (These particular projectile motions were
calculated using default input values.)
Figure CS2.4 The projectile motion of an object being shot from cannon.
Figure CS2.5 The projectile motion of an object being dropped from a plane.
Figure CS2.6 The projectile motion of an object being thrown off a cliff.
Once the user has selected a projectile scenario, the input form then appears. (See
Figure CS2.8.) This form has three text boxes to receive the values for the initial height,
initial velocity, and initial angle. We use labels to clarify the units assumed for each
value, and we also provide the user with default values. These default values differ
depending on the specific projectile scenario. Figures CS2.8, CS2.9, and CS2.10 reveal
the default values for the cannon, plane, and cliff projectile scenarios, respectively.
Figure CS2.8 The input form with the default values for the cannon projectile.
Figure CS2.9 The input form with the default values for the plane projectile.
Figure CS2.10 The input form with the default values for the cliff projectile.
The projectile sheet additionally has three buttons: “Solve,” a functional button that
begins the projectile animation; “Resolve,” also a functional button that allows the user to
change the projectile scenario and/or input values and re-perform the projectile
calculations and animation; and “End,” a navigational button that brings the user back to
the welcome sheet.
CS2.4 Procedures
We will now outline the procedures for this application, beginning with the initial sub
procedures and the variable definitions. (See Figure CS2.11.) The “Main” procedure
initializes the variables and calls the “ClearPrevious procedure,” which clears the
previous data and initializes the variable values. The “Main” procedure then takes the
user to the projectile sheet and displays the projectile scenario and the input forms. At
the end of the “Main” procedure, the “InitializeData” procedure is called.
Figure CS2.11 Variable declarations, the “Main” procedure, and the “ClearPrevious”
procedure.
The procedures for the “Projectile Scenario” form determine which scenario the user has
selected. (See Figure CS2.12.) The input form’s default values are then set depending
on this selection. The corresponding image for the scenario selected also appears next
to the chart on the projectile sheet.
The procedures for the input form record the values for the initial height, the initial
velocity, and the initial angle. (See Figure CS2.13.) The application also performs some
error checking.
The “InitializeData” procedure uses the values entered on the input form to compute the
total time of the projectile motion, a time increment for animation, the x- and y-positions,
and the x- and y-velocities. (See Figure CS2.14.) These calculations are performed with
the equations explained above in Section CS2.1.1. We set the initial x- and y- positions
to zero. Next, we display all the initial values in the table on the projectile sheet. The
user can now choose to press the “Solve,” “Resolve,” or “End” button.
When the user presses the “Solve” button, the application calls the “Projectile”
procedure. (See Figure CS2.15.) This procedure performs the projectile motion
animation by calculating the x- and y-positions, the velocity, and the angle for each time
increment. We use a “Do, Until” loop to perform these calculations until the y-position
value is zero or negative. We assume that the projectile motion stops at the ground: at a
y-position equal to zero.
The navigational procedures for the “Resolve” and “End” buttons appear in Figure
CS2.16.
Summary
CS2.6 Summary
¾ The projectile motion application allows the user to analyze three projectile
situations: when an object is shot from a cannon; when one is dropped from a
plane; and when one is thrown from a cliff.
¾ This application requires two spreadsheets: the welcome sheet and the projectile
sheet.
¾ This application uses buttons and two user forms as the user interface.
¾ Several procedures for this application initialize and perform the projectile motion
calculations and animation.
¾ The user can resolve the application by pressing the “Resolve” button on the
projectile sheet.
CS2.7 Extensions
1. Calculate the total distance traveled by the projectile motion. Display the result to
the user with the total time and maximum y-position values at the end of the
animation.
2. Add “object weight” as an input value. How do the projectile motion calculations
change?
3. Add another projectile scenario. What modifications do you need to make to the
spreadsheets, user interface, and procedures?
4. Enhance the resolve options by allowing the user to perform a comparison
between all the scenarios using similar input values. In other words, if an object
of the same weight, velocity, and angle is dropped in each scenario, determine in
which scenario the object:
a. reaches the maximum height (assuming all initial heights are equal).
b. travels the farthest.
c. reaches the ground in the shortest amount of time.
5. What are some other resolve options you could add to this application?
Chapter Overview
CS3.2 Spreadsheets
CS3.3 User Interface
CS3.4 Procedures
CS3.5 Resolve Options
CS3.6 Summary
CS3.7 Extensions
Case Study 3: Birthday Simulation 2
We provide the user with four options to perform trials of generating birthdays until two
or more match. The first option allows the user to enter a birthday manually. We define
birthdays to be the combination of a month number (1 to 12) and a day number (1 to 31).
All birthdays are displayed to the user on a calendar. The second option allows the user
to generate a random birthday. We do so by generating a month number randomly (an
integer between 1 and 12) first. Then, depending on this month number, a day number is
generated. We use a “Select Case” structure to determine the maximum number of days
per month in order to define the upper bound of the random day number.
The third option allows the user to generate multiple random birthdays until a match, or a
hit, is found. This option repeatedly performs the previous process, checking after each
random birthday generation whether or not the birthday month and day are repeated.
We perform this check using animation; however, it can also be performed with arrays or
other methods. When a match is found, the number of birthdays generated is displayed
to the user. For example, if 20 birthdays were generated until a match was found, the
number 20 would be displayed to the user.
The fourth option allows the user to perform a simulation. This simulation repeats the
third option multiple times, recording each time how may birthdays were generated until
a matching day was found. The number of runs is specified by the user before the
simulation begins. When the simulation is complete, the user can review the created
table, which displays the number of birthdays generated until a match was found for
each run. For a more detailed analysis, the user can click on a button to go to an
analysis sheet in which a histogram is created from the simulation data. This histogram
counts the frequency required to generate a certain number of birthdays until a match
was found. In other words, it may count the number of times it took to generate 20
birthdays until a match was found. The histogram also includes a cumulative percentage
calculation that displays the probability that a match will be found for a certain number of
birthdays generated. That is, it may show that there is a 45% probability that a match will
be found when 20 birthdays are generated. The fourth option is the clearest for
validating the statistical claim that, on average, there is a 50% probability that a match
will be found when 23 birthdays are generated.
CS3.1.2 Input
Since three of the four birthday generation options involve obtaining random birthdays
with VBA, only the first option requires user input:
CS3.1.3 Output
CS3.2 Spreadsheets
This application requires three spreadsheets: the welcome sheet, an input sheet, and an
output sheet. The welcome sheet includes the title, the description of the application,
and an image. (See Figure CS3.1.) The “Start” button on the welcome sheet brings the
user to the input sheet.
The input sheet is the main spreadsheet of the application. (See Figure CS3.2.) It has an
interface in which the user can choose from the four options to generate birthdays. It
also includes a calendar that animates the birthdays generated, and there is an area on
the right in which the output is displayed. A box on the right displays the number of
birthdays generated until a match is found. For the simulation, a table of the number of
birthdays generated for each run is displayed. (See Figure CS3.3.) Notice that another
button appears after the simulation is performed to allow the user to proceed to the
output sheet.
The output sheet contains the simulation analysis, primarily the histogram. (See Figure
CS3.4.) This histogram reveals the frequencies with which a certain number of birthdays
generated were necessary until a match was found. It also includes a cumulative
percentage calculation that illustrates the probability that a match will be found for a
certain number of birthdays generated. This histogram data appears in the table on the
left of the sheet.
Figure CS3.4 The output sheet contains analysis of the simulation option.
The first option uses two text boxes on the spreadsheet and the “Enter Birthday” button
to receive the month number and the day number for entering a manual birthday. The
second option and third option use the “Generate a Random Birthday” and “Generate
Random Birthdays” buttons to generate random birthdays. The fourth option uses a text
box and the “Start Simulation” button to receive the number of runs for the simulation
and to begin the simulation. The “View Analysis” button appears when the simulation is
complete and allows the user to view the output sheet.
This sheet also includes the “Exit” button, which allows the user to quit the application
and return to the “Welcome” sheet, and the “Start Over” button, which clears the
calendar and resets the value of the number of birthdays generated until a match was
found. The output sheet contains another “Exit” button as well as a “Return to Program”
button, which brings the user back to the input sheet.
CS3.4 Procedures
Let’s now outline the procedures for this application, starting with the initial sub
procedure and the variable definitions. (See Figure CS3.5.) The “Main” procedure begins
by calling the “ClearPrevious” procedure; it then takes the user to the input sheet and
sets default values for the controls located on the input sheet.
The “ClearPrevious” procedure clears the previous values from all the sheets. (See
Figure CS3.6.) For this application, we animate all the birthdays generated on a range of
cells that form a calendar. We do so by changing the color of the cells in the calendar
range that represent the dates on which a birthday has been generated and pasting an
image for each birthday. Therefore, the “ClearPrevious” procedure must not only clear
the previous values but also reset the cell colors and delete the images in the calendar
range.
The next two procedures correspond to the user’s first two options for generating a
birthday. The first procedure is the “ManualBday” procedure, which allows the user to
enter a birthday manually. It begins by recording the month number and the day number
entered by the user on the spreadsheet controls. It then performs some simple error
checking to ensure that both values were entered as numbers. Finally, it calls the
“CheckBday” procedure.
Several procedures call the “CheckBday” procedure to check whether or not a matching
birthday has been generated. (See Figure CS3.8.) It selects the cell in the calendar
range that corresponds to the month and day numbers passed to the procedure; in other
words, it selects the cell in the calendar range that corresponds to the birthday that was
just generated. We check to see if a match, or a hit, was found by looking at the color
index of the cell. We use this method since we are already animating the birthdays by
changing the color of the cells in the calendar. If the cell color has already been
changed, then we know that this birthday has already been generated and a match has
been found. If the selected cell still has its original color index, then this is the first time
this birthday has been generated and there is no match. In this case, we change the
color of the cell to reflect that a birthday has been generated to update the calendar for
the next check. Notice that we also copy and paste an image into the cell representing a
generated birthday. This procedure ends by updating the birthday counter, which is
displayed to the right of the calendar.
The “RandBday” procedure is very similar to the “ManualBday” procedure except that it
generates a random birthday instead of recording a birthday entered by the user. (See
Figure CS3.7.) A random month number is generated by selecting a number between 1
and 12. Then, depending on the generated month number, the procedure generates a
random day number. We use a “Select Case” structure to determine the maximum
number of days per month to define the upper bound of the random day number. The
procedure ends by calling the “CheckBday” procedure, passing the newly generated
birthday month and day values.
The “MultRandBday” procedure corresponds to the user’s third option for generating
birthdays. It calls the “RandBday” procedure repeatedly until a matching birthday is
generated. (See Figure CS3.9.) Using the “CheckBday” procedure’s “Hit” Boolean
variable, we form a loop.
The “MultRuns” procedure performs the simulation option, or the forth input option. (See
Figure CS3.10.) This procedure repeatedly calls the “MultRandBday” procedure for the
number of runs specified by the user. For each run, it records the number of birthdays
generated until a match, or hit, is found. It then stores these values along with the run
number in a table to the right of the calendar. When the simulation is complete, it
employs this data to generate a histogram of the frequencies and cumulative
percentages for the number of birthdays generated until a match was found. We then
update the output sheet by displaying the number of birthdays needed to have a 50%
chance of finding a match.
The navigational procedures correspond to the “Exit,” “View Analysis,” and “Return to
Program” buttons. (See Figure CS3.11.) The “Start” button is assigned to the “Main”
procedure and the “Start Over” button is assigned to the “ClearPrevious” procedure.
CS3.6 Summary
¾ The birthday simulation application demonstrates the validity of the statistical
claim that states that there is a 50 percent chance that, out of only 23 students in
a classroom, two or more students have the same birthday.
¾ This application requires three spreadsheets: the welcome sheet, the input sheet,
and the output sheet.
¾ There are several buttons used in the application as well as several controls
placed on the input sheet that correspond to the user’s input options.
¾ This application consists of several procedures that allow the user to generate
birthdays and check for a match.
¾ The user can repeatedly use any of the input options as resolve options and can
return to the program from the output sheet to repeat the simulation with a
different number of runs.
CS3.7 Extensions
1. Create an alternate “CheckBday” procedure that uses arrays to check for a
match instead of the calendar cells’ color index.
2. Create a different user interface for the input sheet. What improvements does
your new interface make?
3. Name another statistical claim that can be verified using a simulation application
similar to this one. Can further statistical analysis be performed for this new
application?
Chapter Overview
CS4.2Spreadsheets
CS4.3 User Interface
CS4.4Procedures
CS4.5 Resolve Options
CS4.6Summary
CS4.7 Extensions
Case Study 4: Poker Simulation 2
We assume the players are playing with one deck of 52 cards; there are four suits with
13 cards in each suit. If the user chooses to be dealt a random hand, then the entire
deck is shuffled and each player is dealt five cards. If however, the user selects his or
her own hand, these cards are moved to the front of the deck first and then the
remainder of the deck is shuffled to deal the other players.
To shuffle the deck, we manipulate an array of card values; this array is size 52 by 2, in
which the two dimensions store the suit and the card value separately. We set a counter
variable i equal to 1 (if the user wants a random hand) or 6 (if the user has already
specified his or her cards) and choose a random number j between i and 52. We switch
the cards in position j with position i and increase i by one.
For i = 1 (or 6) to 52
j = Int((52-i + 1)*Rnd() + i)
Switch Card(i) and Card(j)
Next i
Once the deck is shuffled, we deal each player five cards from the front of the deck, i.e.
from the beginning of the array. For example, the user may have cards 1 to 5, player 2
may have cards 6 to 10, and so on. We store each player’s hand in an array of size five
by 2.
After shuffling and dealing the cards, we sort each player’s hand. We sort the cards in
ascending order, leaving the high card as the last card in the player’s card array. To
perform the sort, we use a simple bubble sort algorithm. (See Chapter 17 for details on
this sorting algorithm.)
To determine who has won the game, we compare the status of each player’s hand.
There are nine possible status values corresponding to the nine possible poker hands.
For each run, we keep track of whether or not the user has the highest ranking hand.
After the simulation is finished, we find the probability of the user winning with the dealt
hand and the given number of players by dividing the number of runs in which the user
won by the total number of runs performed.
CS4.1.2 Input
• Number of players
• Number of runs
• How the user’s hand is dealt (random or specified)
• If the user’s hand is specified, the five cards (suit and value) selected
CS4.1.3 Output
• The probability of winning with the dealt hand and the given number of
players
CS4.2 Spreadsheets
This application requires three spreadsheets: the welcome sheet, the simulation sheet,
and a hidden image sheet. The welcome sheet contains the title, the description of the
application, and the “Start” button. (See Figure CS4.1). The “Start” button shows the
user some forms and then takes him or her to the simulation sheet.
The simulation sheet is the main sheet of the application. (See Figure CS4.2.) It displays
the user’s hand, the number of players in the game, and the user’s chance of winning for
this given hand and number of players. It also shows the other players’ hands for the last
run of the simulation. There are three buttons on this sheet; two are used for the resolve
options, and the “End” button takes the user back to the welcome sheet.
The third sheet, which is hidden, stores the card images by suit in increasing order of
their value. (See Figure CS4.3.) This organization makes the task of copying and pasting
players’ hands easier; we will discuss this more in the “Procedures” section.
The application contains two user forms: the input form and the hand selection form. The
former asks the user to specify the number of players that he or she wants to play with,
the number of runs to perform in the simulation, and how to deal his or her hand. (See
Figure CS4.4.) The user must play with at least one other player and at most four others.
A spin button and text box collect this value. The user’s hand can be dealt randomly
from a shuffled deck, or he or she can specify a specific hand. We use options buttons
for this input.
If the user opts to specify his or her own hand, then the hand selection form is displayed,
(See Figure CS4.5.) This form uses combo boxes for the suit and the value of each of
the five cards in the user’s hand.
CS4.4 Procedures
We will now outline the procedures for this application, beginning with the initial sub
procedures and the variable definitions. (See Figure CS4.6.) The “Main” procedure,
which is called from the “Start” button, calls the “ClearPrev” procedure and shows the
user the input form. It then formats the simulation sheet by hiding the display rows for
the players not playing. Then, it calls the “RunSim” procedure and takes the user to the
simulation sheet.
The input form procedures record the number of players and the number of runs
assigned by the user. (See Figure CS4.7.) It also sets a Boolean variable value to record
the user’s option about whether or not to receive a random hand or to specify a certain
hand. If the user opts to pick his or her own hand, then the hand selection form is also
displayed.
The selection hand form uses an array of controls for the combo boxes for the card suits
and another array for the combo boxes for the card values. (See Figure CS4.8.) It
initializes these variables in the initialization procedure. (See Figure CS4.9.) After
recording the suit and the value for each card selected by the user, we perform some
error checking to ensure that the same card was not selected more than once. We do so
with an array to store which cards were selected. (Is there an alternate way to perform
this error check?)
Figure CS4.8 The first part of the hand selection form procedures.
Figure CS4.9 The second part of the hand selection form procedures.
The “ClearPrev” procedure clears the status and card value cells from the simulation
sheet for all the players. (See Figure CS4.10.) It also deletes all the card images from
the simulation sheet.
The “RunSim” procedure, the main procedure of the application, performs the simulation.
(See Figure CS4.10.) It begins by calling an “Initialize” procedure, which initializes the
array for all the card values and suits. (See Figure CS4.11.) Next, it calls the “DealUser”
procedure, which deals the user’s hand. (Note that this procedure is only called if the
application is not being resolved.) It then performs the simulation by calling the “Shuffle,”
“DealOthers,” and “CalcChance” procedures for the given number of runs. Finally, the
winning probability is calculated and displayed to the user.
The “DealUser” procedure first checks whether or not the user has specified a certain
hand or whether he or she has opted to receive a random hand. In the latter case, the
cards are shuffled with the “Shuffle” procedure, and the first five cards are given to the
user by calling the “Deal” function procedure. If the user has specified his or her own
hand, then it has already been dealt, and we need to move those cards to the front of
the deck. We then sort the user’s hand with the “SortArray” procedure and display the
card images on the simulation sheet with the “GetImage” function procedure.
The “Shuffle,” “DealOthers,” and “Deal” procedures are illustrated in Figure CS4.13. The
first performs the shuffle algorithm described in Section CS4.1.1. “DealOthers” calls the
“Deal” function procedure for each player. It deals cards to each player, as described in
Section CS4.1.1 and then calls the “SortArray” and “GetImage” procedures.
The “SortArray” procedure sorts the cards with the bubble sort algorithm. The
“GetImage” procedure copies the images for each card in the player’s hand from the
hidden image sheet and pastes them on the simulation sheet. (See Figure CS4.13 for
both procedures.)
The “CalcChance” procedure finds the status of each player’s hand and compares it with
the user’s status. If the user has the highest status compared to all the players, then the
user wins and the number of wins for the user is incremented. Each player’s status is
determined with the “FindStatus” function procedure.
The “FindStatus” function procedure finds the status of each player’s hand by checking
each status condition sequentially. (See Figures CS4.16, CS4.17, and CS4.18.) First,
the number of each suit and each value are counted for the player’s hand. The status is
initialized as “High Card,” since this is the lowest ranking hand.
It then checks for “One Pair,” “Two Pairs,” “Three of a Kind,” and “Four of a Kind”
referring to the number of cards in each suit and their values. We also check for “Full
House” if “One Pair” or “Three of a Kind” was found.
If any of these statuses were found, then we do not need to check the other status
conditions. However, if the player’s status is still “High Card,” then we can check for
“Flush,” “Straight,” and “Straight Flush.” We then display the player’s status and hand on
the simulation sheet.
The only navigational procedure, which applies to the “End” button on the simulation
sheet, takes the user back to the welcome sheet.
When the simulation repeats, small changes may occur in the user’s chance of winning
with the given hand. (See Figure CS4.21.)
The second resolve option, selected with the “Deal New Hand” button, again shows the
input form to the user and allows him or her to change the number of players, the
number of runs, or the hand. For example, the user can specify his or her hand so that it
is the same but change the number of players; an increase in the number of players may
lessen the user’s chance of winning. (See Figure CS4.22.) Or, the user can keep the
same number of players and runs but ask to be dealt a new random hand. (See Figure
CS4.23.)
Figure CS4.22 Resolve option 2: “Deal New Hand,” changing the number of
players but keeping the same hand.
Figure CS4.23 Resolve option 2: “Deal New Hand,” keeping the same number of
players, but changing the user’s hand.
CS4.6 Summary
¾ The poker simulation application calculates the chance of winning a game of
poker given the hand that the user is dealt and the number of players in the
game. The user can either have a random hand dealt or specify his or her hand..
¾ This application requires three spreadsheets: the welcome sheet, the simulation
sheet, and a hidden image sheet.
¾ For this application’s user interface, we use navigational and functional buttons
as well as two user forms.
¾ Several procedures for this application initialize and perform the simulation for
the given user hand and number of players.
¾ The user can resolve the application by repeating the simulation for the same
user hand, dealing a new user hand, or changing the number of players.
CS4.7 Extensions
1. Allow the user to specify some cards but not necessarily their entire hand. In
other words, suppose that the user wants to know his or her chances of winning
if dealt an ace or two kings, but he or she doesn’t care about the other cards.
2. Allow the user to exchange two or three cards after initially dealt a random hand.
Also assume that the other players can switch two or three cards. How does this
affect their probabilities for winning?
3. Create a similar application for playing blackjack.
Chapter Overview
CS5.2 Spreadsheets
CS5.3 User Interface
CS5.4 Procedures
CS5.5 Resolve Options
CS5.6 Summary
CS5.7 Extensions
Case Study 5: NBA Lineup 2
There is also an optimization option in this application that allows the user to determine
the best lineup of players to play a selected team in any season.
We assume that the game data for the Indiana Pacers team is provided. This data,
which is imported by the developer, is stored in a hidden sheet. (See Figure CS5.1.) The
user does have the option to view this sheet.
For each game played, the data contains the following statistics: opposing team, game
number, season (regular or playoffs), and lineup. For each game’s lineup, these
additional statistics are recorded: minutes the lineup played, rating the lineup received,
and the five players in the lineup. The application options analyze these statistics.
The first analysis option is to view the performance of various lineup scenarios in which
certain players are in and certain players are out of the lineup. To do so, the user selects
the lineup he or she wants to analyze by determining which players should be in the
lineup and/or which players should be out of the lineup. In this way, the user does not
have to know a specific lineup that was actually used, but rather how the team performs
when certain players are in or out of the lineup. The user also selects a team and a
season for which he or she wants to see the results of this lineup. For example, a coach
may want to know how the team played against New York in the regular season when
the player Artest was in the lineup but player RMiller was not. For this analysis, we loop
over the statistics in the game data sheet to keep track of the total minutes and total
rating for every row of data in which the team and season match the user’s choice and in
which the players that should be in the lineup are listed and the players that should be
out of the lineup are not listed. The total minutes and average rating of the selected
lineup are then calculated and displayed to the user. We use the following code to scan
the data for this analysis.
For i = 1 To NumOut
OutPlayer = OutSet(i)
If PlayerPresent(OutPlayer, row) = True Then
Condition = False
End If
Next i
The second analysis option is to view individual player statistics for the total minutes
played and the average rating against any selected team(s). For example, a user may
want to know the total minutes and average rating that Bender has when playing against
Atlanta and Boston. To perform this analysis, the game data is scanned and the total
minutes and total rating are recorded for each team on the user’s list in which the
selected player is on the lineup. Then, the total minutes and average rating per team are
displayed. We use the following code to scan the data for this analysis.
The third analysis option is to view the game statistics for all combinations of a player
with his teammates. For example, a user may be interested to learn how well BMiller
plays with JFoster and how well he plays with O’Neal; therefore, he or she would view
the total minutes and average rating that were recorded when BMiller played with each
of his teammates. To perform this analysis, we scan the game data and record the total
minutes and total rating from every row in which the selected player and a particular
teammate played together. The total minutes and average rating for each combination of
the selected player and his teammates are then displayed. The application also presents
the total minutes and average rating for which the selected player played for all of the
game data. We use the following code to scan the data for this analysis.
For p = 1 To NumPlayers
NextPlayer = Player(p)
If NextPlayer <> SelectedPlayer And _
PlayerPresent(NextPlayer, row) = True Then
PlayerMinutes(p) = PlayerMinutes(p) + _
DataStart.Offset(row, 3).Value
PlayerRatingMinutes(p) = PlayerRatingMinutes(p) + _
DataStart.Offset(row, 3).Value * DataStart.Offset(row, 4).Value
End If
Next p
End If
row = row + 1
Loop
The final analysis option is lineup optimization. To determine the optimal lineup, which is
the one with the best overall average rating, the user selects a team and a season. The
application then reports the lineup with the maximum average rating that played against
the selected team in the selected season.
To perform this analysis, we need to view the game data for each unique lineup of the
selected team and season. In order to view the data for unique lineups, we first sort the
game data by the players in the lineup. We begin by ensuring that each row’s list of
players is sorted alphabetically. Next, we perform a row sort with the “Orientation”
argument of the “Sort” method:
We then sort the entire list of game data by each column of the lineup players. For
example, if there are two rows with lineups “Bender, BMiller, Croshere, Mercer,
Strickland” and “Bender, BMiller, Croshere, Mercer, Tinsley,” then the lineup ending with
“Strickland” is sorted above the one ending with “Tinsley.” Since there are five columns
of lineup players, and since Excel’s “Sort” method has a limit of three keys to sort by, we
perform two separate sorts:
We can now scan the sorted data and copy the rows whose team name and season
match those selected by the user for the analysis. We copy these rows to a hidden sheet
used for the optimal lineup analysis. (See Figure CS5.2.) Notice in this figure how the
lineups are sorted.
We now need to ensure that the analysis of each lineup is performed on unique lineups.
In other words, if the lineup “Bender, BMiller, Croshere, Mercer, Strickland” occurred in
more than one game, then we want to record the cumulative minutes played and the
average rating received by this lineup. To do so, we use a function procedure that
checks each lineup one row at a time and ensures that consecutive rows do not have
perfectly matching players in each column:
Function SameSet(row)
For col = 2 To 6
If OptDataStart.Offset(row, col).Value <>
OptDataStart.Offset(row - 1, col).Value Then
SameSet = False
Exit For
Else
SameSet = True
End If
Next col
End Function
We have now recorded the total minutes and total ratings for each unique lineup. We
convert the total ratings to average ratings and list the unique lineups on the final optimal
lineup sheet. We then sort all of these rows (with unique lineups, total minutes, and
average rating) by the average rating in descending order. The optimal lineup, which is
the first one listed, has the maximum average rating.
CS5.1.2Input
• Lineup scenario analysis: the players in and out of the lineup, the team,
and the season
• Player statistics: the player and the team
• All combinations analysis: the player
• Optimal lineup analysis: the team and the season
CS5.1.3Output
• Lineup scenario analysis: the total minutes and the average rating for the
selected lineup
• Player statistics: the total minutes and the average rating for the selected
player and the team
• All combinations analysis: the total minutes and the average rating for
each combination of the selected player and his teammates
• Optimal lineup analysis: the lineup with the maximum average rating for
the selected team and the season
CS5.2 Spreadsheets
This application requires five main sheets: the welcome sheet, the lineup scenario sheet,
the player statistics sheet, the all combinations analysis sheet, and the optimal lineup
sheet. Note that we also incorporate the two hidden sheets described in the above
section. The welcome sheet contains the title, the description of the application, and the
“Start” button. (See Figure CS5.3.) The “Start” button displays the user options form.
The lineup scenarios sheet presents the total minutes and the average rating that the
selected lineup achieved when the Pacers played the selected team in the selected
season. (See Figure CS5.4.) The selected lineup is determined by which players are in
and which players are out of the lineup. The “End” button returns the user to the
welcome sheet; the “Main Menu” button shows the user the options form again; the
“New Lineup” button allows the user to resolve this analysis option for a new set of input;
and the “View Details” button allows the user to view the hidden game data sheet.
The player statistics sheet shows the total minutes played and average rating for the
selected player when playing against each selected team(s). (See Figure CS5.5.) The
“End,” “Main Menu,” and “View Details” buttons are also on this sheet. The “New Player”
button allows the user to resolve the player statistics option for a new player and/or the
set of teams.
The all combinations sheet displays the total minutes and total average rating of the
selected player over all games as well as the total minutes and average rating for all
games in which this player played with each of his teammates. (See Figure CS5.6.) The
“End,” “Main Menu,” and “View Details” buttons are also on this sheet. The “New Player”
button allows the user to resolve the all the combinations analysis for a new player.
The optimal lineup sheet presents the optimal lineup for the selected team and season.
(See Figure CS5.7.) The optimal lineup is the one with the best average rating. The
application also reports the total minutes and average rating for each lineup that played
against the selected team in the selected season. The “End,” “Main Menu,” and “View
Details” buttons are also on this sheet. The “Resolve” button allows the user to resolve
the optimal lineup analysis for a new team and/or season.
If the user chooses the lineup scenario option, the lineup scenario sheet appears and
displays the lineup scenario form. (See Figure CS5.9.) On this form, the user selects
which players should be in the lineup and which players should not be in the lineup for
the analysis. The “In List” and “Out List” buttons allow the user to add the selected player
from the list box on the left to the corresponding “In” or ”Out” list box on the right. The
“Remove” buttons remove a selected player from the boxes.
Once the user has specified the lineup, the lineup team and season form appears. (See
Figure CS5.10.) We use a combo box to list all of the teams plus the “All” option to select
all of the teams. We use another combo box to list the seasons plus the “Both” option to
select both seasons.
If the user selects the player statistics option, then the player statistics sheet appears
and displays the player statistics form. (See Figure CS5.11.) The user first selects the
player from the combo box. Then, the user selects the teams for which he or she wants
to see the player’s statistics. A list box provides all of the team names plus “All,” which
the user can select to view the statistics for all the teams. The user can press the “Add”
button to add a selected team to the team list box on the right. The “Remove” button
removes a selected team from the list box.
If the user selects the all combinations option, then the all combinations sheet appears
and shows the all combinations form. (See Figure CS5.12.) On this form, the user
selects a player from the combo box.
If the user selects the optimal lineup option, then the optimal lineup sheet appears and
presents the optimal lineup form. (See Figure CS5.13.) On this form, the user selects the
team and season for which he or she wants to find the optimal lineup. The user selects
the team from the combo box and the season from another combo box.
CS5.4 Procedures
We will now outline the procedures for this application, beginning with the variable
declarations and initial sub procedures. (See Figure CS5.14.) The “Main” procedure,
which is called from the “Start” button, displays the options form to the user. The
“Initialize” procedure sets several range variables that are used on the application’s
worksheets.
Figure CS5.14 The variable declarations and the “Main” and “Initialize” procedures.
The procedures for the options form are presented in Figure CS5.15. Depending on the
selected option button, the corresponding sheet is made visible and the corresponding
form appears.
The procedures for the lineup scenarios form are shown in Figure CS5.16. In Figure
CS5.16 (a), the form is initialized by placing the list of players in the list box. The
procedure for the “OK” button records the names of the players in the “In” list box and
the “Out” list box to two respective arrays. The procedure in Figure CS5.16 (b) records
the name of a selected player to the “In” list box. Several error checking statements are
also included in this procedure. Similarly, in Figure CS5.16 (c), the procedure records
the name of a selected player to the “Out” list box and also performs some error
checking. The procedures in Figure CS5.16 (d) remove a selected player from the “In” or
“Out” list box, respectively.
(a)
(b)
(c)
(d)
Figure CS5.16 The lineup players form procedures.
Figure CS5.17 presents the procedures for the lineup team and season form. In these
procedures, the name of the selected team and season are recorded, and the “Lineup”
procedure is then called.
The “Lineup” procedure performs the analysis for the lineup scenario. (See Figure
CS5.18.) The procedure loops over the statistics in the game data sheet to keep track of
the total minutes and the total rating. It does so for every row of data in which the team
and season match the user’s choice and in which the players that should be in the lineup
are listed and the players that should be out of the lineup are not listed. The total
minutes and average rating for the selected lineup are then calculated and displayed to
the user.
Figure CS5.20 presents the procedures for the player statistics form. In Figure CS5.20
(a), the procedures initialize the form and record the selected player; the “PlayerStats”
procedure is then called. In Figure CS5.20 (b), the procedures record a selected team
and add it to the team list box or remove a selected team from the team list box.
(a)
(b)
Figure CS5.20 The player statistics form procedures.
The “PlayerStats” procedure performs the analysis for the player statistics option. (See
Figure CS5.21.) The game data is scanned, and the total minutes and total rating are
recorded for each team on the user’s list in which the selected player is on the lineup.
The total minutes and the average rating per team are displayed.
Figure CS5.22 presents the procedures for the all combinations form. In these
procedures, the form is initialized and the selected player is recorded. The
“AllComparisons” procedure is then called.
The “AllComparisons” procedure performs the analysis for the all combinations option.
(See Figure CS5.23.) We scan the game data and record the total minutes and total
rating from every row in which the selected player and a particular teammate have
played together. The total minutes and average rating for each combination of the
selected player and his teammates are displayed. The total minutes and average rating
for which the selected player has played for all of the game data are also displayed.
The “Optimize” procedure performs the optimal lineup option. (See Figure CS5.24.) First,
the optimal lineup form appears to record the team and season on which to perform the
analysis. We then sort the game data by the players in the lineup. We begin by ensuring
that each row’s list of players is sorted alphabetically. We then sort the entire list of
game data by each column of the lineup players. We can now scan the sorted data and
copy the rows whose team name and season match those selected by the user for the
analysis. We copy these rows to the hidden optimal data sheet. Figure CS5.24 (b)
illustrates these steps.
Next, we check each lineup one row at a time and ensure that consecutive rows do not
have perfectly matching players in each column. We do so with the “SameSet” function
procedure. (See Figure CS5.25.) We have now recorded the total minutes and total
ratings for each unique lineup. We convert the total ratings to average ratings and list the
unique lineups to the final optimal lineup sheet. We then sort all of these rows (with
unique lineups, total minutes, and average rating) by the average rating in descending
order. The optimal lineup is the first one listed; it has the maximum average rating.
These steps are illustrated in Figure CS5.24 (b).
(a)
(b)
Figure CS5.24 The “Optimize” procedure.
Figure CS5.26 presents the procedures for the optimal lineup form. In these procedures,
the form is initialized and the selected team and season are recorded.
CS5.6 Summary
¾ This application allows the user to view statistics and make informed coaching
decisions for a basketball team. The user can analyze lineup scenarios, player
statistics, combinations of a player with his teammates, or find the optimal lineup
to play a specific team in a specific season.
¾ This application requires five main sheets: the welcome sheet, the lineup
scenario sheet, the player statistics sheet, the all combinations analysis sheet,
and the optimal lineup sheet. Note that we also use two hidden sheets: the game
data and the optimal lineup data.
¾ For this application’s user interface, we use navigational and functional buttons
and six user forms.
¾ Several of this application’s procedures perform the analysis for the selected
option.
¾ The user can resolve this application by pressing the resolve buttons on the
analysis sheets to return to the corresponding input forms.
CS5.7 Extensions
1. Add summary statistics to the player statistics option.
2. Add summary statistics to the all combinations option.
3. Add summary statistics to the optimal lineup option.
4. Allow the user to import new game data into the application. Allow the user to
either overwrite the current data or to append it.
5. Create more analysis options for the optimal lineup option. What else would a
coach be interested in examining when determining the optimal lineup? For
example, what if a coach wants to know what the optimal lineup would be for a
given team and season if he or she knows a particular player cannot be in the
lineup?
Chapter Overview
CS6.2 Spreadsheets
CS6.3 User Interface
CS6.4 Procedures
CS6.5 Resolve Options
CS6.6 Summary
CS6.7 Extensions
Case Study 6: University Information System 2
We will not discuss database design in this chapter or in this book. If the reader is
unfamiliar with databases or Access, then we recommend referring to another text. For
this application development description, we will focus on building the queries and
creating a dynamic interface in which the user may run live queries for various options.
(a) (b)
Figure CS6.1 The Access database “UniversityInformationSystem” has six tables and
one query.
We will now provide an overview of the tables and queries in this database. It is
important to understand where different information is located before creating this
application’s queries.
The first table is “TblCourse”; it contains information about the courses offered at the
university. (See Figure CS6.2.) The titles of the fields stored in this table are: “CourseID,”
“DepartmentID,” “CourseName,” “Description,” “CreditHours,” and “CourseType.”
The third table, “TblFaculty,” contains information about the faculty at the university.
(See Figure CS6.4.) The titles of the fields stored in this table are: “FacultyName,”
“DepartmentID,” “Status,” “Nationality,” “Gender,” and “Phone.”
The fourth table, “TblSection,” contains information about the sections of each course
offered at the university. (See Figure CS6.5.) The titles of the fields stored in this table
are: “SectionNum,” “CourseID,” “FacultyName,” “Year,” “Semester,” “YearSem,” “Day,”
“Time,” “RoomNumber,” “Capacity,” and “Evaluation.”
The fifth table is “TblStudent”; it contains information about the students enrolled at the
university. (See Figure CS6.6.) The titles of the fields stored in this table are: “Name,”
“DepartmentID,” “StudentType,” “Email,” “Address,” “DateEntered,” “Nationality,” and
“Gender.”
The sixth table, “TblTranscript,” contains information about the student grades for all the
courses offered. (See Figure CS6.7.) The titles of the fields stored in this table are:
“SectionNum,” “StudentName,” and “Grade.”
The one query in the database is “qryCourseID”; it appends the “CourseID” field to
“TblTranscript.” (See Figure CS6.8.) The titles of the fields stored in this query are:
“CourseID,” “SectionNum,” “StudentName,” and “Grade.”
We will use the query function described in Chapter 21, Section 21.1.2.2 to create each
query; we call this function “QueryData.” (See Figure CS6.9.) We pass the following
parameters to this function: “Src” is the string of SQL code that defines the query;
“OutputRange” is the range to which we copy the recordset; and i and j are “Offset”
parameters for this output range. Please refer to Chapter 21 for a more detailed
explanation of query components.
CS6.1.2 Input
The input for this application depends on which option the user is solving. The input may
be among the following:
• Student name
• Course name
• Department name
• Faculty name
CS6.1.3 Output
The output for this application depends on which option the user is solving. The output
may be among the following:
There are two more output options that are left for the reader to develop. (See
the “Extensions” section.) These are:
CS6.2 Spreadsheets
This application requires seven main sheets: the welcome sheet, the student transcript
sheet, the department audit sheet, the course information sheet, the student schedule
sheet, the faculty grades sheet, and the faculty statistics sheet.
The welcome sheet contains the title, the description of the application, and the “Start”
button, which displays a main menu form. (See Figure CS6.10).
The student transcript sheet displays a selected student’s transcript and GPA. (See
Figure CS6.11.) The transcript includes all of the courses that the selected student has
taken. The “Select New Student” button allows the user to view the transcript for a new
student. The “Back to Student Menu” button takes the user back to the student menu
form. The “Exit” button returns the user to the welcome sheet. (See the “Applications”
section in Chapter 21 for details on the student transcript option.)
The department audit sheet displays the audit of courses for a selected student and his
or her department. (See Figure CS6.12.) The user can view all of the courses that are
required or the electives in the student’s department. The “Select New Student,” “Back to
Student Menu,” and “Exit” buttons are also available on this sheet.
The course information sheet presents a list of sections for a selected course in a
selected department. (See Figure CS6.13.) The user can view information for all of the
sections offered of the selected course in the selected department. The course
evaluation of each section is also displayed. The “Select New Course” and “Select New
Department” buttons allow the user to view information for a new course in the same or
another department, respectively. The “Back to Student Menu” and “Exit” buttons are
also available.
The student schedule sheet allows a user to create a schedule for the current semester.
(See Figure CS6.14.) To do so, the user can view all the sections offered of a selected
course in a selected department. From the given list of sections, he or she can select a
section number and press the “Add” or “Remove” buttons to add the course to or remove
it from his or her schedule. [See Figure CS6.14 (a).] The user can click the “View Weekly
Schedule” button to see all of the courses already added to the weekly schedule. [See
Figure CS6.14 (b).] Additionally, the “Select New Department,” “Select New Course,”
“Back to Student Menu,” and “Exit” buttons are available on this sheet.
(a)
(b)
Figure CS6.14 The student schedule sheet.
Faculty members can refer to the faculty grades sheet to view or modify student grades
for a course they are currently teaching. (See Figure 6.17.) They can then either modify
the grades by pressing the “Modify Grades of Current Students” button or add new
students and grades by pressing the “Add New Students and Grades” button. These
actions occur for the selected section number in the given table. After the faculty
member makes any changes, he or she can press the “Save” button to update the
database. The “Select New Faculty” button allows the user to perform these actions for a
new faculty member and the corresponding list of courses that he or she taught. The
“Back to Faculty Menu” button returns the user to the faculty menu form, and the “Exit”
button returns the user to the welcome sheet.
The administration uses the faculty statistics sheet to view statistics for all the faculty in a
selected department. (See Figure CS6.16.) The main statistics include the
student/faculty ratio, the graphs for the number of courses taught per faculty member,
the average evaluation per faculty, the nationality distribution of the faculty in the
department, and the gender distribution of faculty in the department. [See Figure CS6.16
(a).] The user can also view individual statistics for each faculty member in the selected
department by pressing the “View Details per Faculty” button. [See Figure CS6.16 (b).]
The user can return to the main statistics sheet by pressing the “View Summary per
Department” button. The “Select New Department” and “Exit” buttons are also available
on this sheet. Finally, the “Back to Administration Menu” button returns the user to the
administration menu form.
(a)
(b)
Figure CS6.16 The faculty statistics sheet.
The student form, which requires a frame and four option buttons, presents the user with
the student options. (See Figure CS6.18.) He or she can select between viewing a
transcript, viewing a department audit, seeing what courses are offered, and creating a
schedule.
The faculty form, which requires a frame and two option buttons, presents the user with
the faculty options. (See Figure CS6.19.) The user can view course information or add
and modify student grades.
The administration form, which requires a frame and two option butons, presents the
administration options to the user. (See Figure CS6.20.) Here, the user can view faculty
statistics or student statistics.
The student choice form appears when the user needs to select a student. (See Figure
CS6.21.) The combo box lists all the students in the database. A query is always
performed to update this list with the most current database information before the user
views it.
When the user needs to select a department, the department choice form appears. (See
Figure CS6.22.) The combo box lists all the departments in the database. A query is
always performed to update this list with the most current database information before
the user views it.
The course choice form is presented when the user needs to select a course. (See
Figure CS6.23.) The combo box lists all the courses in the database. A query is always
performed to update this list with the most current database information before the user
views it.
The faculty choice form appears when the user needs to select a faculty member. (See
Figure CS6.24.) The combo box lists all the faculty members in the database. A query is
always performed to update this list with the most current database information before
the user views it.
Faculty members use the student grades form to select students to add to a course
and/or to enter their grades. (See Figure CS6.25.) This form is similar to the student
choice form in that a query is always performed to update the list with the most current
database information before the user views it. However, the “Add” and “Done” buttons
allow the user to select more than one name from the form in order to populate their
course list on the faculty grades sheet.
Main menu form The user can choose between student, faculty, or
administration options.
Student menu form The user can choose among the following student
Summary options: view transcript, view department audit,
view course information, and create a schedule.
Faculty menu form The user can choose among the following faculty
options: view course information, view/modify
student grades.
Administration The user can choose among the following
menu form administration options: view student statistics, view
faculty statistics.
Student choice A student can be selected from the current list of
form students in the database.
Department choice A department can be selected from the current list
form of departments in the database.
Faculty choice A faculty member can be selected from the current
form list of faculty in the database.
Course choice A course can be selected from the current list of
form courses in the database.
Student grades Multiple students can be selected from the current
form list of students in the database.
Navigational “Exit,” “Back to Student Menu,” “Back to Faculty
buttons Menu,” “Back to Administration Menu,” “View
Details per Faculty,” “View Summary per
Department.”
Functional buttons “Select New Student,” “Select New Department,”
“Select New Course,” “Select New Faculty,” “Add,”
Remove,” “Modify Grades of Current Students,”
“Add New Students and Grades.”
CS6.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and initial sub procedures. (See Figure CS6.26.) The “Main” procedure,
which is called from the “Start” button, displays the main menu form to the user.
The main menu form procedures are presented in Figure CS6.27. Depending on which
option the user selects, the corresponding menu form for that option appears.
The procedures for the student menu form are presented in Figure CS6.28. These
procedures take the user to the sheet that corresponds to the student option selected. A
corresponding sub procedure is also called.
The first student option calls the “Transcript” procedure, which we will discuss in detail in
Chapter 21’s “Applications” section.
The second student option calls the “Audit” procedure, which prepares the department
audit for a selected student. (See Figure CS6.29.) The student is selected using the
student choice form procedures. (See Figure CS6.30.) These procedures initialize the
form by calling the “CreateStudentList” procedure. (See Figure CS6.31.) The
“CreateStudentList,” “CreateFacList,” “CreateDeptList,” and “CreateCourseList”
procedures all query the database to retrieve the current list of student names, faculty
names, department names, and course IDs and names, respectively.
Once the student has been selected, the “Department” procedure is then called from the
“Audit” procedure. The “Department” procedure collects from the database the
department name that corresponds with the selected student. The “Audit” procedure
then creates a query to find all of the course IDs, course names, and course types with
the selected department ID. For each of these courses, another query finds the section
number that the selected student was enrolled in; these queries are performed with a
loop.
Note: All queries performed in these procedures use the “QueryData” function procedure
described in Section CS6.1.1.
(a)
(b)
Figure CS6.31 The procedures for populating the combo boxes on the choice forms.
The third student option calls the “Courses” procedure, which begins by displaying the
department choice and course choice forms. (See Figure CS6.33.) The procedures for
these forms are illustrated in Figures CS6.34 and CS6.35. The “CreateDeptList” and
“CreateCourseList” procedures appear above in Figure CS6.31 (b). The “Courses”
procedure then performs a query to find the course name and course type for the
selected course. Another query is then performed to find the section numbers, faculty
names, years, semesters, days, times, and room numbers for the specified course.
The fourth student option calls the “Schedule” procedure, which begins by displaying the
department choice and course choice forms. (See Figure CS6.36.) A query is then made
to find the course name and course type for the selected course. Another query then
finds the section numbers, days, times, room numbers, and faculty names for the
selected course. The user then uses this information to create his or her schedule.
The user can add a course to or remove a course from his or her schedule by selecting
one of the section numbers form the query and pressing the “Add” or “Remove” button,
respectively. The “Add” button is assigned to the “AddCourse” procedure, which copies
the name of the selected course and pastes it into the weekly schedule cells, depending
on the course day and time. (See Figure CS6.37.) The “Remove” button is assigned to
the “RemoveCourse” procedure, which clears the selected course from the weekly
schedule by searching for the day and time of the course. (See Figure CS6.38.)
Figure CS6.39 presents the procedures for the faculty menu form, which provides the
user with the faculty options. Depending on the option the user selects, the
corresponding sheets are made visible and the corresponding procedure is called.
The first faculty option is left for the reader to develop. The second faculty option calls
the “FacGrades” procedure, which begins by showing the user the faculty choice form.
(See Figure CS6.40.) The “CreateFacList” procedure [shown in Figure CS6.31 (a)] is
called to populate the combo box on the faculty choice form. The procedures for the
faculty choice form record the selected faculty. (See Figure CS6.41.) The “FacGrades”
procedure then creates a query to find all of the course IDs, section numbers, and years
and semesters that the selected faculty has taught. For each of these courses, another
query finds the course name with a loop.
Now, the user can select either the “Modify Grades of Current Students” button to modify
the course grades or the “Add New Students and Grades” button to add new student
grades to a course. The first button is assigned to the “ModifyGrades” procedure, which
performs a query to find all of the students and grades for the selected section of the
selected course. (See Figure CS6.42.) The user can then modify these grades directly
on the spreadsheet. The “Add New Students and Grades” button is assigned to the
“AddGrades” procedure, which displays the student grades form. (See Figure CS6.42.)
The procedures for this form record the selected students and paste them on the
spreadsheet with the “Add” button until the “Done” button is pressed and the form is
hidden. (See Figure CS6.43.)
The user can save changes made to the grades or the student list in this faculty option
by pressing the “Save” button on the spreadsheet. This button is assigned to the
“SaveGrades” procedure. (See Figure CS6.44.) If the user has been viewing or
modifying the current list of student grades, then this procedure performs an UPDATE
query to record the new grades to the current table “TblTranscript” in the database. If the
user has added new students and new grades, then this procedure performs an INSERT
query to insert each new student and grade into “TblTrancript” in the database.
The administration menu form provides the user with the administration options; Figure
CS6.45 illustrates the procedures for this form.
The first administration option calls the “StatsFac” procedure. (See Figure CS6.46.) The
second administration option is left for the reader to develop. The “StatsFac” procedure
begins by showing the department choice form. A query then finds the faculty names,
status, nationalities, and genders for the selected department. This information is put on
the statistics details sheet. Next, a COUNT query determines the number of courses
taught and the average evaluations using a loop. Another COUNT query is then
performed to find the number of students in the selected department. The final COUNT
query finds the number of faculty in the selected department.
Next, a loop creates a unique list of the nationalities of all the faculty in the selected
department. Once this list is created, another loop counts the number of faculty of each
nationality. The number of males and females are also calculated. Using all of these
detailed statistics, the summary statistics are then created. The application calculates
the student to faculty ratio and updates charts for the number of course taught per
faculty, the average evaluation per faculty, the nationality distribution of faculty in the
department, and the gender distribution of faculty in the department.
(a)
(b)
Figure CS6.46 The “StatsFac” procedure.
(a) (b)
Figure CS6.47 The navigational procedures.
CS6.6 Summary
¾ This application allows the user to view information for several student, faculty,
and administration options by querying a university database.
¾ This application requires seven main sheets: the welcome sheet, the student
transcript sheet, the department audit sheet, the course information sheet, the
student schedule sheet, the faculty grades sheet, and the faculty statistics sheet.
¾ For this application’s user interface, we use navigational and functional buttons
as well as nine user forms.
¾ Several of this application’s procedures create queries for the various information
options.
¾ The user can resolve this application by pressing the resolve buttons on the
various option sheets to update the information for a new student, new
department, new course, or new faculty member.
CS6.7 Extensions
1. Add the following two options to the application:
a. A faculty option to view course information. The faculty may want to see,
for a selected course, who has taught the course in the past, how many
students are usually in the course, and what the average course
evaluation is.
b. An administration option to view student statistics. The administration
may be interested in the distribution of nationalities and genders among
students. They may also want to know these distributions as they relate to
GPA. Also, they may want to search for the distribution of students based
on how long they have been at the university.
2. Add more security to this application by creating a username and password for
each student and faculty member in the database. Also, create an administrative
password for all department chairs. Keep this list of passwords either in an Excel
spreadsheet or in a new table in the database. When the user selects an option
from the main menu form, ask him or her for a password and ensure that he or
she has the right to access that option. Where else could this security be added?
Chapter Overview
CS7.2 Spreadsheets
CS7.3 User Interface
CS7.4 Procedures
CS7.5 Resolve Options
CS7.6 Summary
CS7.7 Extensions
Case Study 7: Retirement Planning 2
Let’s now describe the simulation calculations performed. We assume that the following
input is known by the user and provided in the beginning of the application: current age,
current savings, annual salary, annual percent increase in salary, and percent of user’s
salary to invest each year. For the investment strategy, we assume that the user
distributes his or her funds between treasury (T) bills, bonds, and stocks. This strategy is
considered to be an asset allocation of the percent of the user’s savings he or she
wishes to invest. The asset allocation is provided by the user as percentages (summing
to 100 percent). All of this input is recorded by the user and placed on the sheet where
the simulation calculations are performed.
We translate the user’s percent values for the asset allocation into initial investment
dollar amounts using the formula below. This calculation is performed for all investment
types. (See Figure CS7.1.)
Figure CS7.1 The initial investments are calculated from the asset allocation
percentages.
Another source of input used in the simulation calculations is the historical data of
market returns for T. bills, bonds, and stocks. We assume this data has already been
collected (between 1926 and 1994) and stored in the simulation calculation spreadsheet.
(See Figure CS7.2.) We use bootstrapping to create the market returns for each
investment type for each year of the simulation. To perform the bootstrapping, we use
the VLOOKUP function, as follows:
Here, the range “BootData” contains all of the historical data presented in Figure CS7.1,
and the value 2 signifies that the value for T. Bills is in the second column of this range.
The formula is similar for bonds and stocks.
Next, we use these bootstrapped returns to calculate the ending investment returns for
the current year. (See Figure CS7.3.) For each investment type, we apply the following
formula:
Figure CS7.3 Calculating the ending investment returns using the bootstrapped market
returns.
We then calculate the current year salary, the current year addition to savings, and the
updated investment dollar amount for each investment type. (See Figure CS7.4.) To
calculate these values in their corresponding columns, we apply the following formulas:
Figure CS7.4 Calculating the yearly salary, addition to savings, and investment
amounts.
These calculations are performed (in each of the columns shown in the figures above)
over several years (or rows). The number of years for which the calculations are made
depends on which option the user is interested in solving. For each option, the first year
matches the current age of the user. For option A, the last year matches the year when
the user’s desired savings at retirement is met. For options B and C, the last year
matches the age at which the user wishes to retire.
For each option, these calculations are made for the necessary years and the final age
(option A) or savings (option B and C) at this final year is recorded. This entire set of
calculations is considered to be one run of the simulation. The simulation runs are
repeated by updating the random number generation from the bootstrapping formula.
We do so by typing the command “Calculate” in the VBA code, which is equivalent to the
F9 manual calculation update function in the spreadsheet. (Refer to Chapter 7 and the
RAND and RANDBETWEEEN functions.) The mean and standard deviation of the age
(option A) or retirement savings (options B and C) are then calculated from the results
recorded in each simulation run.
For option C, we use a neighborhood search technique to find the investment strategy
that maximizes the mean savings achieved in the simulation. We use five “big runs” of
the simulation in which we manipulate the investment strategy until the mean savings do
not improve. This manipulation is performed by adjusting the asset allocations by a small
value alpha (here 0.05). The neighborhood search tries variations of the asset
allocations with a “TrialInvst” array and alpha, as follows:
For i = 1 To 3
For j = i + 1 To 3
If LoopNum = 1 Then
TrialInvst(i) = TrialInvst(i) + alpha
TrialInvst(j) = TrialInvst(j) - alpha
ElseIf LoopNum = 2 Then
TrialInvst(i) = TrialInvst(i) - alpha
TrialInvst(j) = TrialInvst(j) + alpha
End If
Next j
Next i
The new asset allocations are only recorded if the mean savings improves. The steps for
this “big run” simulation are as follows: The asset allocations are initialized to 0.33 each,
and the mean savings from the ten simulation runs are recorded for the trial investment
values. (See Figure CS7.5.)
Figure CS7.5 The initialization of the big run simulation for option C.
If none of the alpha-variation of the investment amounts improve, then the big run ends,
and the max mean savings is recorded. The simulations are repeated in each big run
until there is no more improvement for these investment amounts. Let’s consider an
example illustrated in Figure CS7.6.
In Figure CS7.6 (a), the first big run has ended with the initial .33 equal asset allocation.
As shown in Figure CS7.5, there was no further improvement made to this mean savings
value from the other trial asset allocations. The next big run is now being performed. We
can see that in the first set of trial asset allocations, there has been an improvement
from the initial .33 equal allocation with a mean savings of $481,988.56 to the asset
allocation of .33, .38, and .28 (for T. bills, bonds, and stocks, respectively) and a mean
savings of $512,025.07.
In Figure CS7.6 (b), we see that this mean savings value and the corresponding asset
allocation are moved to the top of the list and the other values cleared to see if further
improvement can be made by searching the neighbors of this solution. In Figure CS7.6
(c), we see that no further improvement is made to this mean savings value, and so the
second big run ends. In Figure CS7.6 (d), this value and the asset allocation are copied
to the big run results, and this process is repeated with the .33 equal asset allocation for
the next big run. Figure CS7.6 (e) illustrates that all of the five big runs have finished.
(a)
(b)
(c)
(d)
(e)
Figure CS7.6 An example of the completed big run simulation calculations.
CS7.1.2 Input
• Current age
• Current savings
• Annual salary
• Annual percent increase in salary
• Percent of user’s salary he or she wishes to invest each year
• Desired salary at retirement with confidence interval (for option A)
• Desired age at retirement (for options B and C)
• Investment strategy (or asset allocation) (for options A and B)
CS7.1.3 Output
CS7.2 Spreadsheets
This application requires four sheets: the welcome sheet, the simulation calculations
sheet, the simulation runs sheet, and the results sheet. The welcome sheet contains the
title, the description of the application, and the “Start” button. (See Figure CS7.7.) The
“Start” button displays to the user several forms and then takes him or her directly to the
results sheet.
The simulation runs sheet contains the outputs for each run of the simulation. For option
A, the age of retirement is recorded for each run. [See Figure CS7.9 (a).] For option B,
the savings at retirement is recorded for each run. For option C, the savings at
retirement are also recorded for each "big run" whose investment strategy cannot be
improved. [See Figure CS7.9 (b).]
(a)
(b)
Figure CS7.9 The simulation runs sheet.
The results sheet displays the mean and standard deviation of the solutions for the
selected option. It also shows a corresponding histogram of the values of the simulation
runs. For option A, for the given desired returns and the confidence interval, the mean
and standard deviation of the retirement age values are presented with the
corresponding histogram. [See Figure CS7.10 (a).] For option B, for the given retirement
age, the mean and standard deviation of the savings values are presented with the
corresponding histogram. [See Figure CS7.10 (b).] For option C, the mean and standard
deviation of the savings values are presented with the corresponding histogram (as in
option B) along with the mean and standard deviation values for the asset allocations.
[See Figure CS7.10 (c).]
(a)
(b)
(c)
Figure CS7.10 The results sheet.
The user then views the input form. (See Figure CS7.12.) This is a dynamic form in that
the last set of input (grouped by three different frames) is different depending on which
option the user selected on the options form. If the user selects option A, then he or she
is prompted for the desired savings at retirement and the confidence interval. [See
Figure CS7.12 (a).] If the user selects option B, then the frame containing this input is
replaced by a frame that prompts the user for the age at which he or she wishes to
retire. [See Figure CS7.12 (b).] If the user selects option C, then the last frame with the
asset allocation becomes locked and grayed. [See Figure CS7.12 (c).]
(a) (b)
(c)
Figure CS7.12 The input form.
On the results sheet, for options A and B, we use spin buttons to allow the user to
modify his or her investment strategy and resolve the application by pressing the
“Resolve” button. We will discuss this in more detail in Section CS7.5.
CS7.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and initial sub procedures. (See Figure CS7.13.) The “Main” procedure,
which is called from the “Start” button, calls the “ClearPrev” procedure to clear the
previous values from all the sheets. The “Main” procedure then displays the option form
(from which the input form is shown). It ends by calling the “RunSim” and “CreateReport”
procedures and taking the user to the results sheet.
Figure CS7.13 The variable declarations and the “Main” and “ClearPrev” procedures.
Using Boolean variables, the procedures for the option form record which option the user
has selected. (See Figure CS7.14.) These procedures also perform dynamic formatting
for the input form.
The procedures for the input form perform some error checking for the text boxes and
then record the input for the selected option. (See Figure CS7.15.) Most input values are
recorded directly to the simulation calculations sheet. Because they will be checked
during the simulation code, the desired savings (for option A) and the retirement age (for
options B and C) are stored in variables.
The “RunSim” procedure performs the simulation. For options A and B, we simply loop
for the 100 runs and update the formulas on the simulation calculations sheet by typing
the command “Calculate.” We record the results of each run by taking the values on the
row specified by the user’s input for the selected option. [See Figure CS7.16 (a).] For
option C, we perform the big runs and neighborhood search steps defined in section
CS7.1.1. [See Figures CS7.16 (b), (c), and (d).]
(a)
(b)
(c)
(d)
Figure CS7.16 The “RunSim” procedure.
The “CreateReport” procedure displays the mean and standard deviation values for the
selected option. (See Figure CS7.17.) Some rows are hidden and unhidden, and the
spin buttons are made visible and invisible depending on which option is selected. The
histogram of simulation run values is also created.
(a)
(b)
Figure CS7.17 The “CreateReport” procedure.
When the “Resolve” button is pressed, the “Resolve” procedure is called. (See Figure
CS7.19.) This procedure begins by recording any changes to the input cells. If option A
or B is being resolved, then before changes to the investment strategy are recorded, the
procedure ensures that the updated investment strategy still sums to 100%. Finally, the
application re-calls the “RunSim” and “CreateReport” procedures.
CS7.6 Summary
¾ This application is to help the user answer the following questions: (i) At what
age can I retire?; (ii) How much savings can I retire with?; and (iii) What is my
best investment strategy? Simulation calculations for the selected option answer
these questions.
¾ This application requires four spreadsheets: the welcome sheet, the simulation
calculations sheet, the simulation runs sheet, and the results sheet.
¾ For this application’s user interface, we use navigational and functional buttons,
two user forms, and controls on the spreadsheet.
¾ Several application procedures perform the simulation calculations for the
selected option.
¾ The user can resolve this application by pressing the “Resolve” button to re-run
the simulation with updated input values.
CS7.7 Extensions
1. Add another histogram to the results sheet for option C that shows the frequency
with which the asset allocations were recorded for the big runs.
2. Allow the user to change the alpha value used in the neighborhood search for the
option C calculations.
3. Give the user the option to resolve the application by changing his or her option
selection. What other resolve options can be added?
4. Is there another option (aside from options A, B, and C) that can be considered in
this application? How would you implement this to the simulation calculations
sheet and procedures?
Chapter Overview
CS8.2 Spreadsheets
CS8.3 User Interface
CS8.4 Procedures
CS8.5 Resolve Options
CS8.6 Summary
CS8.7 Extensions
Case Study 8: Stochastic Customer Forecasting 2
This application determines a constant factor, a factor for each month, a factor for each
weekday, and special factors such that the sum of the square errors between the
predicted number of customers and the historical number of customers is minimized. To
forecast the number of customers with these factors, we use the following equation:
With the decision variable cells, the month number, and the weekday value for each
calendar day, we use LOOKUP functions to insert the corresponding month and
weekday factors into this formula. We also use the SUMPRODUCT function to insert the
corresponding special factors with a binary representation of which special factors are
applied to a particular date. Below is an example of how this formula is written in Excel.
(We consider month 1 = January and weekday 2 = Tuesday.)
To insert this formula for every calendar day given in the user’s historical data, we loop
over the data and dynamically enter the formula using the month and weekday in the
corresponding row of data. We also enter the formula for the sum of squared errors
between this forecasted number of customers and the historical number of customers. In
the “Procedures” section, we will review these formulas, and in the “Spreadsheets”
section, we will explain the layout of the model sheet.
Please see Microsoft Excel: Data Analysis and Business Modeling by Winston for more
details.
CS8.1.2 Input
CS8.1.3 Output
CS8.2 Spreadsheets
This application requires four sheets: the welcome sheet, the historical data sheet, the
solutions sheet, and the solutions chart sheet. The welcome sheet contains the title, the
description of the application, and the “Run Demo” and “Start” buttons. (See Figure
CS8.1.) The “Run Demo” button copies the demo data to the historical data sheet and
takes the user to this sheet. The “Start” button prompts the user for some input and then
takes him or her to the historical data sheet.
The historical data sheet contains the historical number of customers for every workday
for an entire calendar year. (See Figure CS8.2.) The user can change the calendar year
using the spin button at the top of the sheet; the calendar day, month, and weekday
values are automatically updated for the selected year. The user can also import this
information as a text file. Once the historical data has been entered, the user presses
the “Continue” button to proceed to the solution sheet.
The solution sheet contains the model and the results of the model for the factors in the
customer forecasting. (See Figure CS8.3.) A constant factor, a factor for each month, a
factor for each weekday, and special factors are determined such that the sum of the
mean square errors between the predicted number of customers and the historical
number of customers is minimized. Outliers in the mean square error column are
highlighted to help the user determine if another special factor should be considered.
The user can press the “View Chart” button to view the solution chart sheet.
The solution chart sheet graphs the predicted number of customers with the historical
number of customers. (See Figure CS8.4.) The user presses the “Return to Results”
button to return to the solutions sheet.
The user then views the historical data sheet where there is a spin button control that
allows the user to change the calendar year to study. (See Figure CS8.6.) When this
spin button is changed, the linked cell is updated, and the calendar days, the month, and
the weekday values are automatically updated.
When the user presses the “Continue” button on this sheet, he or she views the special
factors user form. (See Figure CS8.7.) This form prompts the user to select any special
factors that may bias his or her historical number of customers. There are three
categories of special factors: holidays, periodic days (per month), and ranges of dates.
For each category, some default days are provided as well as a blank day in which the
user can create his or her own special factors. The user can select all or none of these
and then click the “Solve” button to solve the model and proceed to the solution sheet.
Frames group these categories of special factors, and check boxes are provided for
each special factor selection. Additionally, combo boxes are provided for the months,
days, and periodic type and day.
Historical data Determines how the user enters the historical data.
form
Year spin button Allows the user to change the year of the historical
Summary data and to automatically update the calendar
days.
Special factors The user can select any or all of the special factors
form that may bias his or her historical data: holidays,
periodic days, or ranges of dates.
Navigational “End,” “View Chart,” and “Return to Results”
buttons buttons.
Functional buttons “Run Demo,” “Start,” “Continue,” and “Resolve.”
CS8.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and the initial sub procedures. (See Figure CS8.8.) The “Main” procedure,
which is called from the “Start” button, calls the “ClearPrev” procedure to clear the
previous values from all the sheets. It then initializes the historical data calendar dates
on the historical data sheet and takes the user to this sheet. The historical data form
then appears. The “Demo” procedure, which is called from the “Run Demo” button,
copies the demo year and data to the historical data sheet and takes the user to this
sheet.
Figure CS8.8 The variable declarations and the “Main” and “Demo” procedures.
The “ClearPrev” procedure clears the previous values and initializes the special factors
arrays. (See Figure CS8.9.) There are three special factors arrays: one for the holidays;
one for the periodic days; and one for the ranges of dates. The default dates are set, and
the Boolean values for the user selection are reset.
The procedures for the historical data form prompt the user to select a text file if he or
she chooses to import the historical data. Or, they take the user directly to the historical
data sheet if he or she chooses to enter the data manually. (See Figure CS8.10.) The
event procedure for the spin button on the historical data sheet updates the calendar
dates, month values, and weekday values for the selected year. (See Figure CS8.11.)
The “Continue” procedure is called from the “Continue” button on the historical data
sheet. (See Figure CS8.12.) This procedure copies the historical data and pastes it on
the solution sheet for the model calculations. It then displays the special factors form and
takes the user to the solution sheet. It ends by calling the “RecordSF” and “RunSolver”
procedures.
The procedures for the special factors form record all of the special factors selected by
the user. (See Figures CS8.13 and CS8.14.) The default days for each category are
checked as well as the user-defined special factors in each category. The application
also updates the corresponding special factors arrays, and an initialization procedure
sets the row sources of the form combo boxes and the default values.
Figure CS8.13 The first part of the special factors form procedures.
Figure CS8.14 The second part of the special factors form procedures.
The “RecordSF” procedure scans over the dates in the calendar from the historical data
and marks which special factor is present. (See Figure CS8.15.) A mark is also made in
the hidden special factors table, which is used in the model. For all possible days in each
category of the special factors, the corresponding special factors array values are
checked.
The “RunSolver” procedure solves the model. (See Figure CS8.16.) It inserts the
formulas for the column to predict the number of customers and to calculate the squared
error between the predicted and the historical values. “RunSolver” then names some
ranges and inserts more formulas for the sum of the squared errors, the average month
factors, and the average week factors. The Solver model is now prepared and run. The
user can then observe the solutions to highlight outliers using the mean and standard
deviation of the sum of the squared errors. Lastly, the application updates the data for
the graph of the predicted and historical values.
“RecordSF” and “RunSolver” procedures are re-called. The user stays on the solution
sheet to view the updated results.
“Resolve” button on the Re-shows the special factors form and re-
solution sheet optimizes the factor values and the
customer forecast.
Summary
CS8.6 Summary
¾ This application is to forecast stochastic customer arrival while considering a set
of factors that influence the forecast.
¾ This application requires four spreadsheets: the welcome sheet, the historical
data sheet, the solutions sheet, and the solutions chart sheet.
¾ For this application’s user interface, we use navigational buttons, controls on the
spreadsheet, and two user forms.
¾ Several procedures for this application collect the historical data and the special
factors for the model and solve the model to forecast the customer arrivals.
¾ The user can resolve this application by pressing the “Resolve” button on the
solution sheet. This selection re-shows the special factors form and re-optimizes
the factor values and the customer forecast.
CS8.7 Extensions
1. Add a dynamic element to the special factors form by selecting the first value in
the combo boxes for any corresponding special factor check box.
2. Ensure that the user’s previous selections on the special factors form appear
when the form is re-displayed during the resolve option. In other words, if the
user had previously selected a date or entered a period special factor, then it
should already be checked and entered when the form appears again.
3. Attempt to solve this problem using multiple regression instead of a non-linear
programming model.
Chapter Overview
CS9.2Spreadsheets
CS9.3User Interface
CS9.4Procedures
CS9.5 Resolve Options
CS9.6Summary
CS9.7 Extensions
Case Study 9: Sales Force Allocation 2
We begin by prompting the user to enter historical data about the sales force allocation.
We assume that this historical data is known and includes the number of calls, unit
sales, revenue per sale, and cost per call for all customers. This historical data is used to
find s-curve parameters to estimate future profit. In marketing research, the s-curve is
considered a more accurate model than the power curve. It is modeled as follows:
(b − a) S c
R =a+
d + Sc
The Solver finds for each customer the optimal s-curve parameters that minimize the
sum of squared errors in the estimated profit and the calculated profit. Given the
historical number of calls made to a customer, a variation of this value is found for 0.10,
0.50, 1.50, and 10 times the historical value. Aside from the historical number of calls,
which has a corresponding historical unit sales value, the user is asked to estimate the
unit sales for the variations of the number of calls. From each of these estimates, an
estimated profit value is found. A calculated profit value is then determined using the s-
curve parameters and the s-curve model provided above. The Solver is then used to find
the best s-curve parameters that minimize the sum of squared errors between these two
profit values.
Parameters
a (decision variables)
b
c
d
Once these s-curve parameters have been determined for each customer, they are used
to calculate the profit in the main optimization model. This model seeks to find the
number of calls that should be allocated to each customer so that the total profit over all
the customers is maximized. We consider that there is a lower bound and an upper
bound constraint for the number of calls that can be made to each customer. We also
consider that there is an upper bound on the total number of calls that can be made over
all the customers. For the trade-off curve option, we allow the user to vary this overall
upper bound and view the change in the optimized total profit.
Maximize: =SUM(Profit)
See Practical Management Science by Winston and Albright for more details.
CS9.1.2 Input
• Historical input = number of calls, unit sales, revenue per sale, and cost
per call for each customer
• Estimate input = expected unit sales for variations on the historical
number of calls
• Optimization input = lower and upper bounds on the number of calls that
can be made to each customer, upper bound on the total number of calls
that can be made across all customers
CS9.1.3 Output
CS9.2 Spreadsheets
This application requires five spreadsheets: the welcome sheet, the historical data sheet,
the s-curve parameters sheet, the optimization sheet, and the trade-off curve sheet. The
welcome sheet contains the title, the description of the application, and the “Run Demo”
and “Start” buttons. (See Figure CS9.1.) The “Run Demo” button copies some demo
historical data and then takes the user to the historical data sheet. The “Start” button on
the welcome sheet prompts the user for some input and then displays the historical data
sheet.
The historical data sheet stores the input for the historical number of calls made to each
customer, the unit sales achieved, the profit per unit sale, and the cost per call made.
The total profit is calculated per customer with a simple formula of unit sales * profit per
sale – number of calls * cost per call. The total profit overall customers is also calculated
as the sum of the total profit per customer. Figure CS9.2 presents the historical data
sheet with the demo data.
The “Find S Curve Parameters” button takes the user to the s-curve parameters sheet,
which is where the user optimizes the s-curve parameters for each customer. (See
Figure CS9.3.) For each customer, one at a time, the historical data is referenced as
input on this sheet. The number of calls and unit sales in the historical data are found in
the middle row of the main calculation table of this sheet. Using the historical number of
calls, the application calculates variations of .10, .50, 1.5, and 10 times the historical
value. The user is then asked to estimate the unit sales for these variations in the
number of calls. With the user’s values, the application calculates an estimated profit for
each scenario. The Solver then finds the a, b, c, and d parameter values for the s-curve
such that the mean squared error between the calculated profit from these parameters
and the estimated profit from the user’s input are minimized. A chart comparing the
estimated and calculated profits is shown.
Next, the user clicks the “Calculate” button to optimize these s-curve parameters for
each customer after he or she has entered the sales estimates. When all the customer’s
parameters have been optimized, the user can press the “Go to Optimization” button to
proceed to the optimization sheet.
The main model of the application is solved on the optimization sheet. (See Figure
CS9.4.) The user provides a lower bound and an upper bound on the number of calls
that can be made to each customer; these are the decision variables. Using the
optimized s-curve parameters and these decision variables, the unit sales are
calculated. From these unit sales, the profit per sale, and the cost per call input, the total
profit per customer is also calculated. The objective function is to maximize the sum of
these total profit values for all the customers. When the user presses the “Optimize”
button, the model is solved.
The user can also specify an upper bound constraint on the total number of calls for all
the customers. By selecting the “View Trade Off Curve” button, the user can view the
results of the different values of this upper bound for the total number of calls, which are
listed on the trade-off curve sheet.
The trade-off curve sheet presents the change in the overall revenue as the user
changes the upper bound on the total number of calls to be made. (See Figure CS9.5.)
The user varies these values by specifying a range and a step size for the trial upper
bound values. Once this information has been entered, the user can press the “Create
Curve” button to view the trade-off curve for these values. He or she can return to the
optimization sheet by pressing the “Return to Optimization” button.
For this application’s user interface, we use navigational and functional buttons, input
boxes and message boxes, and a user form. Pressing the “Start” button on the welcome
sheet displays an input box for the number of customers to analyze. (See Figure CS9.6.)
The user form then ascertains from the user how the historical data will be provided.
(See Figure CS9.7.) The user has two options: import a text file, for which he or she will
be prompted to select a file to import; or enter the data manually, for which he or she will
proceed directly to the historical data sheet. This form therefore requires two option
buttons.
When all of the customers’ s-curve parameters have been optimized, a message box
appears to inform the user that this sheet is completed. (See Figure CS9.9.)
Figure CS9.9 The s-curve input box when all the customers have been optimized.
We also use message boxes here for two error checks. If the user tries to go to the
optimization sheet before optimizing all the customers’ s-curve parameters, a message
box appears with an error message. (See Figure CS9.10.) If the user continues pressing
the “Calculate” button to optimize the customer’s s-curve parameters after all the
customers’ parameters have been optimized, then another error message appears. (See
Figure CS9.11.)
Figure CS9.10 Error checking to finish the s-curve parameters before going to the
optimization sheet.
Figure CS9.11 Error checking to prevent further s-curve optimization after is has been
completed.
CS9.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and the initial sub procedures. (See Figure CS9.12.) The “Main” procedure,
which is called from the “Start” button, calls the “ClearPrev” procedure to clear the
previous values from all the sheets. It then prompts the user with an input box to find the
number of customers. Next, it updates all the sheets with this value to prepare the
tables. Finally, the procedure ends by displaying the historical data form.
The “RunDemo” procedure is called from the “Run Demo” button on the welcome sheet.
(See Figure CS9.13.) It sets a Boolean value and then calls the “Main” procedure. The
Boolean value prevents the input box and the user form from being displayed to the
user. It then copies the demo data to the historical data sheet and takes the user to this
sheet.
The procedures for the historical data form determine if the user will import a text file or
enter the historical data directly into the sheet. (See Figure CS9.14.) If the user selects
to import a text file, a browser window opens so the user can select his or her file. The
file is then imported to the historical data sheet. If the user selects to enter the data
manually, then the historical data sheet appears.
The “SCurve” procedure is called when the user presses the “Find S Curve Parameters”
button on the historical data sheet. (See Figure CS9.15.) It prepares the s-curve
parameters sheet by readying the Solver and copying the input values for the first
customer. If the demo is being run, then the demo sales estimates are also copied for
the first customer.
The “Calculate” button on the s-curve parameter sheet calls the “CalcS” procedure,
which performs error checking and displays message boxes for the user to communicate
when customer parameters have been optimized. (See Figure CS9.16.) It runs the
Solver, stores the s-curve parameters for each customer, and then copies the input for
the next customer.
The “GoToOpt” procedure is called from the “Go To Optimization” button on the s-curve
parameter sheet. (See Figure CS9.17.) It prepares the optimization sheet by copying the
customer names, revenues, and costs and naming several ranges for the Solver model.
It then inserts formulas for the sales using the s-curve parameters found in the previous
procedure. It also inserts a formula to calculate the total overall profit for all the
customers. Next, the Solver is prepared. If the user is running the demo, then the demo
value for the upper and lower bounds on the number of calls per customer are also
inserted in the sheet.
The “Optimize” procedure is called from the “Optimize” button on the optimization sheet.
(See Figure CS9.18.) It places an initial guess in the decision variable cells and then
runs the Solver.
The “TradeOff” procedure is called from the “Create Curve” button on the trade-off curve
sheet. (See Figure CS9.18.) This procedure performs a loop from the minimum trial
value to the maximum trial value using the step size specified by the user for the upper
bound value on the total number of calls. In this loop, the upper bound value is updated
and the “Optimize” procedure is re-called to re-solve the model using the Solver. When
the loop is completed, the trade-off curve chart is updated.
CS9.6 Summary
¾ This application seeks to determine the optimal allocation of sales force in order
to maximize profits.
¾ This application requires five spreadsheets: the welcome sheet, the historical
data sheet, the s-curve parameters sheet, the optimization sheet, and the trade-
off curve sheet.
¾ For this application’s user interface, we use navigational and functional buttons,
input boxes and message boxes, and a user form.
¾ Several application procedures optimize the s-curve parameters, optimize the
main model, and create a trade-off curve.
¾ The user can resolve this application by re-optimizing the model on the
optimization sheet or by re-creating the trade-off curve on the trade-off curve
sheet.
CS9.7 Extensions
1. Re-design the s-curve sheet such that the values for all the customers appear at
the same time on the sheet. In other words, locate the estimates, input,
calculations, errors, and parameters in one table for each customer on the sheet.
Allow the user to enter the sales estimates and optimize over all the customers
as often as desired; in this way, the user may refine his or her guesses for some
or all of the customers.
2. Add a cost curve and a revenue curve to the trade-off curve. In other words, for
each upper bound trial value, not only record the total profit, but record the total
costs and revenues.
Chapter Overview
CS10.2 Spreadsheets
CS10.3 User Interface
CS10.4 Procedures
CS10.5 Resolve Options
CS10.6 Summary
CS10.7 Extensions
Case Study 10: Technical Analysis 2
Option A: The time horizon and a risk rate are set, and we then use various values of X
and Y (within a given bounds) to calculate the overall profit. A maximum profit is found
among these values.
Option B: The time horizon shifts (within a given bound) and the best values of X and Y
are determined for the maximum profit possible for scenarios of buy and sell, buy only,
and sell only.
Let’s now discuss how to make the calculations for determining the overall profit for a
given X and Y value. We perform these calculations in a calculation sheet with several
columns. Note that the formulas for these calculations are only in the rows
corresponding to the time horizon. In a later section, we will further describe the
calculation sheet as well as the procedure that generates the calculations, UpdateCalc.
The first two columns contain historical data from the S&P records, which we use to
calculate the average market price depending on X and Y. The next two columns check
if the investor can buy or sell shares based on the current trial value for X and Y,
respectively. We simply calculate the average market price based on the value of X for
buying and X for selling. We then enter the formulas and copy them for the time horizon
period.
The next column checks whether or not the user currently owns shares. This is a logical
check to ensure that the user does not sell shares that he or she does not own. For the
first period in the time horizon, we perform this check using the initial number of shares
provided by the user in an input form. For the remaining periods, we check the number
of shares owned in the previous period.
Else
Range("Own").Offset(StartCount, 0).Value = "No"
End If
Range("Own").Offset(StartCount + 1, 0).FormulaR1C1 = "=IF(R[-1]C[2] = ""Yes"",
""Yes"", IF(R[-1]C[3]=""Yes"", ""No"", R[-1]C))"
Range("Own").Offset(StartCount + 1, 0).Copy
Range(Range("Own").Offset(StartCount + 2, 0), Range("Own").Offset(EndCount,
0)).PasteSpecial
The next two columns confirm whether or not the user is going to buy or sell shares for
the current period. We check whether or not we are in a scenario in which we are buying
or selling. We then check whether or not the buy or sell check was met and whether or
not we have shares to sell.
Range("Buy").Offset(StartCount, 0).FormulaR1C1 =
"=IF(BuyAllowed=""Yes"",IF(AND(RC[-2] = ""No"", RC[-6]>RC[-5]),
""Yes"", ""No""),""No"")"
Range("Sell").Offset(StartCount, 0).FormulaR1C1 =
"=IF(SellAllowed=""Yes"",IF(AND(RC[-3] = ""Yes"", RC[-7]<RC[-5]),
""Yes"", ""No""),""No"")"
Range(Range("Buy").Offset(StartCount, 0), Range("Sell").Offset(StartCount,
0)).Copy
Range(Range("Buy").Offset(StartCount + 1, 0), Range("Sell").Offset(EndCount,
0)).PasteSpecial
For either action, we must determine the number of shares by assuming that we will
either buy all the shares for which we have funds or sell all the shares that we currently
own. When buying shares, we divide our current capital by the sum of the market price
and the transaction cost for buying.
The next column calculates the cash flow. We begin with a starting capital specified by
the user in an input form. Any time we buy, we spend all of our money. This cost comes
from the market price of the stock and the transaction cost for buying shares. Any time
we sell, we sell all of our shares and therefore gain their market price in value. However,
we also incur a transaction cost for selling.
Range(Range("CashFlow").Offset(StartCount + 2, 0),
Range("CashFlow").Offset(EndCount, 0)).PasteSpecial
The overall profit is stored at the top of the sheet. It is equal to our cash flow at the end
of the time horizon.
The formulas provided here are in terms of the cells and variables used in the sheet and
the procedure descriptions below.
CS10.1.2 Input
• Initial capital
• Initial number of shares
• Risk free rate
• Time horizon
CS10.1.3 Output
• Table with various X and Y values and profit values for each combination
• Maximum profit and X and Y for maximum profit
• Number of buys and sells at maximum profit
CS10.2 Spreadsheets
We will use four spreadsheets in this application: the welcome sheet, a calculation
sheet, an output sheet for Option A, and an output sheet for Option B. The welcome
sheet contains the title and the description of the application as well as some images
(see Figure CS10.1). The “Start” button takes the user to an input form and then an
options form. If the user selects Option A, he or she will see the output sheet for Option
A; otherwise, the output sheet for Option B appears.
We determine the output for Option A and Option B by performing calculations on the
“Calculations” sheet (see Figure CS10.2), which the user can view by clicking a “View
Calc” button on either of the output sheets. The user can return to the output sheet by
clicking the “Back” button. An “End” button allows the user to exit the application.
(a)
(b)
Figure CS10.2 The Calculation sheet.
The first two columns contain historical data from the S&P records. The dates are
recorded by year and months, with months represented as decimals. The price per
share is included for each date. The next two columns check if the investor can buy or
sell shares based on the current trial value for X and Y, respectively. The next column
checks whether or not the user currently owns shares. As previously mentioned, this is a
logical check to ensure that the user does not sell shares he or she does not own.
Above the next set of columns are several input cells, which the user provided in the
initial input form and in the resolve options on the output sheets. The “Buy Allowed” and
“Sell Allowed” cells permit the user to vary the scenarios for Option B. The “Buy” and
“Sell” columns confirm whether or not the user wants to buy or sell shares for the current
period. For either action, the number of shares is also determined. Next, we calculate
the cash flow in the next column and store the overall profit at the top of the sheet. Note
that the formulas for these calculations are only in the rows corresponding to the time
horizon.
The output sheet for Option A has a spreadsheet user interface that allows the user to
specify the risk rate and the time horizon (see Figure CS10.3). Bounds for the X and Y
values have been set in the input form already. Based on these bounds, a table is
created to try different combinations of X and Y in the calculations. We record the overall
profit for each trial, the maximum profit amongst these is highlighted, and the number of
buys and sells at this maximum profit value.
If the user changes the risk rate or time horizon, he or she must then click the “Resolve”
button to re-run the calculations. The “View Calc” button allows the user to view the
calculation sheet and the “End” button to exit the application.
The output sheet for Option B displays the X and Y values that yield a maximum profit
for a shifting time horizon (see Figure CS10.4). These X, Y, and profit values are
calculated for three different scenarios: buying and selling, buying only, and selling only.
The time horizon shifts according to the values provided by the user in a user form for
Option B. If the user wishes to changes these values, he or she can press the “Resolve”
button. The “View Calc” button allows the user to view the calculation sheet and the
“End” button to exit the application.
The next form that the user sees is the options form (see Figure CS10.6), which allows
the user to choose between Option A and Option B. We employ a frame with two
mutually exclusive option buttons.
If the user selects Option A, then the Option A form appears (see Figure CS10.7). This
form receives the initial values for the time horizon, which (along with the risk rate
provided in the input form) will be used to perform the initial calculations for Option A.
To resolve Option A, the user does not return to any of these forms, but rather
manipulates the controls on the spreadsheet to change the risk rate and/or the time
horizon (see Figure CS10.8). We employ a spin button linked to a cell to allow the user
to change the risk rate. We include two text boxes and two combo boxes to allow the
user to select new years and/or months for the time horizon.
Figure CS10.8The user interface controls on the output sheet for Option A.
If the user selects Option B from the option form, the Option B form appears next (see
Figure CS10.9). This form receives input for the shifting time horizon. The length of each
trial horizon is set in the “Period of Time per Interval” text box. The step size for the shift
in the starting date is also an input. The starting and ending dates are considered to be
bounds for the trial time horizons. We include combo boxes for the months of the time
horizon bounds. This form appears to the user again when he or she resolves Option B.
CS10.4 Procedures
We will now outline the procedures for this application. We begin with the Main sub
procedure and variable definitions (see Figure CS10.10). The Main sub procedure
simply calls a ClearPrev procedure and displays the input form and options form. The
ClearPrev procedure clears the output sheets’ values and formatting for Option A and
Option B. It also initializes some variables.
The code to receive the input on the input form appears in Figure CS10.11. In this code,
the upper and lower bounds for X and Y are assigned to their corresponding variables.
The other input values are recorded on the calculations sheet. The application makes
note of the initial risk rate on the corresponding user interface of Option A’s output sheet.
The code to receive the user’s selection on the options form appears in Figure CS10.12.
If the user selects Option A, then the output sheet for Option A is formatted, a
corresponding Boolean variable is set, the user is taken to the output sheet for Option A,
and the Option A form comes into view. Once the user has entered the initial time
horizon in the Option A form, the application calls the SolveA procedure. If the user
selects Option B, a corresponding Boolean variable is set, the user is taken to the output
sheet for Option B, and the application calls the SolveB procedure. In response to the
SolveB procedure, the Option B form appears to receive the time horizon shift input.
The code for the Option A form records the initial time horizon on the user interface of
Option A’s output sheet (see Figure CS10.13). This form is also initialized to reveal the
first month to be selected in the “Month” combo boxes.
The SolveA procedure performs the main calculations for Option A (see Figure
CS10.14). It begins by determining whether or not we are solving Option A. If we are,
then we record any changes made to the user interface controls on the output sheet for
Option A. We use the SolveA procedure to solve Option B and should not record this
information in that case.
We then loop through all of the X,Y combinations to calculate their profit values and
determine the maximum profit scenario. To perform the calculations, we use an
UpdateCalc procedure (see Figure CS10.15), which loops through the rows in the
calculation sheet that correspond to the time horizon specified by the user. In these
rows, we enter all of the necessary formulas for each column in the calculation sheet.
For Option B, we use the SolveB procedure shown in Figure CS10.16. This procedure
begins by displaying the Option B form to the user. Since we will display this form every
time Option B is resolved, we call from the SolveB procedure instead of the options form.
Next, we loop through the various time horizon values, solving for the best X and Y
values for maximum profit each time. For each time horizon value, we examine three
scenarios: buying and selling, only buying, and only selling. To enforce these scenarios,
we modify the “Buy Allowed” and “Sell Allowed” cells on the calculation sheet. We call
the SolveA procedure to find the optimal X, Y, and profit values for each scenario. Again,
the SolveA procedure calls the UpdateCalc procedure to modify the calculation sheet
formulas as needed for the varying X, Y, and time horizon values.
The Option B form code simply records the time horizon bounds, step size, and interval
length (see Figure CS10.17). The form is also initialized to select the first month in the
“Months” combo boxes.
We use the navigational procedures for the “End,” “View Calc,” and “Back” buttons (see
Figure CS10.18).
Option A The user can change the risk rate and/or time
Resolve Option horizon.
Option B Allows the user to change the shift in time horizon
Summary Resolve Option values.
CS10.6 Summary
¾ The “Technical Analysis of Market Timing” problem seeks to determine values for
X and Y and a max profit for buying and selling shares for a certain time horizon.
¾ There are four spreadsheets in this application: the welcome sheet, a calculation
sheet, an output sheet for Option A, and an output sheet for Option B.
¾ There are four user forms, some controls on the output for the Option A
spreadsheet, and a few navigational and functional buttons.
¾ There are several procedures for this application that allow the user to enter
input, perform calculations, view results, and resolve.
¾ There is a resolve option for Option A in which the and Option B.
CS10.7 Extensions
1. What other analysis could you add to the output sheets? (Hint: What about charts
and/or graphs?)
Chapter Overview
CS11.2 Spreadsheets
CS11.3 User Interface
CS11.4 Procedures
CS11.5 Resolve Options
CS11.6 Summary
CS11.7 Extensions
Case Study 11: Beta of Stocks 2
Option 1: Determine the optimal Beta value per stock for a given period of time.
Option 2: Evaluate the MSE (mean square error) for various values over the past months
for the Beta estimate.
The users begin the application by selecting a portfolio of stocks from a set database.
We provide historical data about the stock returns for each stock in the database. We
use S&P data for the corresponding market return values in this historical data. The
users, then, are seeking to determine the beta values based on the two optional
estimation methods so they can better predict future stock returns for their portfolios.
rs = α + β*rm
Where rs is the return of the stock, we are predicting, rm is the return on the market, and
α and β are parameters. The β parameter is what we refer to as the beta of a stock,
which measures the responsiveness of a stock’s returns to the market return.
For Option 1, to estimate each stock’s α and β parameters, we use the Solver to
calculate those that minimize the MSE between the actual stock returns and the
predicted stock returns. The users provide an input that directs which months of
historical data to include while determining the best beta estimate. This value is
indicated as an interval using the month numbers in the historical data.
For Option 2, we also consider the number of months of historical data used in
determining the optimal beta value to be an important factor. therefore, we must
determine the best number of months to minimize the MSE of the stock return
predictions. We estimate the beta parameter with the SLOPE function and calculate the
average beta for the resulting best number of months. The users provide an input of the
number of trial months with which to experiment. This input is an interval of the number
of months to use with an assumed step size of one month.
These calculations will become clear as we describe the spreadsheets and procedures.
For more information, please see Practical Management Science by Winston and
Albright.
CS11.1.2 Input
CS11.1.3 Output
• the chart of the predicted stock returns versus the historical stock returns
for each stock.
CS11.2 Spreadsheets
We incorporate five spreadsheets in this application: the welcome sheet, two calculation
sheets, and two option sheets. The welcome sheet includes the title, the description of
the application, and some images (see Figure CS11.1). The “Start” button on the
welcome sheet brings the users to a form where they make their portfolio selection. The
users then see an option form where they can decide to use Option 1 or Option 2 for
determining the beta estimates.
If the users choose Option 1, then the output sheet for Option 1 appears (see Figure
CS11.2). Here, using the controls on the spreadsheet, they can specify which months of
historical data they want to use to determine the optimal beta values. They then click the
“Calculate” button to run the calculations and view the results. They can view the “Stock
Performance” chart for each stock in their portfolio using the combo box above the chart.
The “Return to Menu” button returns the users to the options form. The “End” button
allows them to exit the application.
The calculations for Option 1 are performed on the “Calc 1” sheet (see Figure CS11.3),
which always remains hidden from the users. The first columns on this sheet are the
S&P market returns (see Figure CS11.3(a)) over the past 146 months. The next columns
are for all of the stocks in the database. We have two columns for each stock: the actual
returns from the historical data; and the predicted returns. The latter are determined
using the formula:
We are solving for each stock’s alpha and beta values. To do so, we keep a set of
ranges named “Alphas” and “Betas” for each stock and reference them in these columns
(see Figure CS11.3(b)). For example, since the “Cin” stock is listed first in the database
of stocks, the formula for predicting its returns is:
We also include a column for calculating the square error between the historical returns
and the predicted returns. We update this column’s formulas depending on which stock
we are currently solving for. The MSE value is the average of these values for the rows
corresponding to the historical months specified by the users to use. We also include
some columns for copying the historical and predicted returns for the source data of the
“Stock Performance” chart. We copy the data for the stock selected from the combo box
and paste it above the chart on the Option 1 output sheet.
(a)
(b)
Figure CS11.3 The calculation sheet for Option 1.
If the users select Option 2, they will then see the Option 2 output sheet (see Figure
CS11.4). The users enter an upper and lower bound for the trial number of months to
use in estimating beta and then click the “Calculate” button. The calculations are
performed and the optimal number of historical months is recorded for each stock. The
minimum MSE and the mean beta value for this number of months are also recorded.
The sum of the MSE for the entire portfolio displays above a bar graph.
The users can view two chart types: the MSE values for all the stocks in the portfolio or a
histogram of the beta values for each stock. To select which chart to view, the users can
click on the option buttons above the chart. If the users are viewing the histogram of beta
values for a stock, they can select which stock to view from the combo box above the
chart. As in the previous output sheet, the “Return to Menu” button returns the users to
the options form, and the “End” button allows them to exit the application.
The calculations for Option 2 are performed on the “Calc 2” sheet (see Figure CS11.5).
The first two columns again are the historical S&P market returns for 146 months (see
Figure CS11.5 (a)). The actual stock returns for all of the stocks in the database are also
recorded. We then include a column that calculates the beta values of a particular stock
with the SLOPE formula:
=SLOPE(StockReturns / MarketReturns)
The number of months of stock returns and market returns selected for this formula will
vary as we try all of the values in the interval provided by the users. For each beta value,
we calculate the predicted stock return with the formula:
=MarketReturns * Betas
This is the same formula as in Option 1, with Alphas assumed to be 0. Using this
predicted value and the actual stock returns, we calculate the square errors. From these
errors, we determine the MSE.
(a)
For each trial value of the number of historical months to use in the calculations, we
record the MSE and the mean beta value (see Figure CS11.5 (b)). We keep these
values separate for the chart source data of the two charts available on the output sheet.
We calculate the histogram data for each selected stock by referring to the mean beta
column.
(b)
Figure CS11.5 The calculation sheet.
The second form that the users see is the options form (see Figure CS11.7). We include
two option buttons in a frame to enable the users to select between Option 1 and Option
2. Depending on their choice, they will be taken to the corresponding output sheet.
The controls on the Option 1 output sheet include two text boxes, an option button, and
a combo box (see Figure CS11.2). The text boxes enable the users to specify an interval
of months to use for the Option 1 calculations. The option button indicates the active
chart and the combo box enables the users to select which stock is graphed in the chart.
The controls on the Option 2 output sheet include two text boxes, two option buttons,
and a combo box (see Figure CS11.4). The text boxes enable the users to specify upper
and lower bounds for the trial number of months for the Option 2 calculations. The option
button indicates which chart is active, and the combo box enables the users to select
which stock is graphed in the second chart.
Navigational buttons include “End” and “Return to Menu.” Functional buttons include
“Calculate” and “Start.”
CS11.4 Procedures
We will now outline the procedures for this application, beginning with the initial sub
procedures and variable definitions (see Figure 28.13). The Main procedure initializes
certain variables and calls the ClearPrev procedure, which clears previous values from
all sheets. The Main procedure then displays the portfolio form to the users. After the
users create their portfolio, the PrepCalc procedure is called. This procedure prepares
the output and calculation sheets based on the stocks that the users had selected (see
Figure 28.14). The Main procedure then presents the options form.
The code for the portfolio form sets a Boolean value in a Portfolio array to True when the
stock is added and to false when it is removed (see Figure CS11.10). These procedures
also perform some error checking to ensure that stocks are not added multiple times and
that the users are operating the controls correctly. The options form code takes the
users to the appropriate output sheet depending on their selection (see Figure CS11.11).
If the users press the “Calculate” button on the Option 1 output sheet, the DoOption1
procedure is called (see Figure CS11.12). This procedure begins by recording the input
specified by the users on the output sheet’s controls. It then loops over the Portfolio
array to update the formulas and run the Solver for each stock selected by the users.
When the calculations are done, the values are recorded on the output sheet. The code
then calls the Worksheets(“Option1”).cmbStocks_Change event procedure to update the
chart data.
The procedures for the “Option1” output sheet are event procedures for the option button
and combo box (see Figure CS11.13). These procedures update the chart data on the
Option 1 calculation sheet.
If the users select the “Calculate” button on the Option 2 output sheet, the DoOption2
procedure is called (see Figure CS11.14). This procedure begins by recording the input
specified by the users on the output sheet’s controls. It then loops from the lower to the
upper bound of the number of months to use in the SLOPE formula and updates the
formula for each loop.
Once the loop is complete, it searches for the number of months that yields the minimum
MSE value for each stock (see Figure CS11.15). It then updates the output sheet and
the MSE chart accordingly.
The event procedures for the “Option2” sheet are for the output sheet’s controls (see
Figure CS11.16 and CS11.17). Using the data on the calculation sheet, they update both
the MSE chart and the histograms, based on the selected stock.
Figure CS11.16 The first part of the Option 2 output sheet code.
Figure CS11.17 The second part of the Option 2 output sheet code.
The navigational codes are for the “End” and “Return to Menu” buttons (see Figure
CS11.18).
Option 1 The users can change the input values on the output
sheet. To re-run calculations, they click on the
“Calculate” button again.
Summary Option 2 The users can change the input values on the output
sheet. To re-run calculations, they click on the
“Calculate” button again.
CS11.6 Summary
¾ The Beta of Stocks application seeks to predict the return on several stocks
based on the return of the market. The users have the following two options for
estimating the beta of stocks: find the optimal Beta value per stock for a given
period of time; or evaluate the MSE for various values over the past months to
use in Beta estimate.
¾ This application consists of five spreadsheets: the welcome sheet, two
calculation sheets, and two option sheets.
¾ The application includes two user forms, controls on both of the output sheets,
and several navigational and functional buttons.
¾ Several procedures for this application allow the users to …
¾ The resolve options for both Option 1 and Option 2 are provided by the controls
on the spreadsheet. These controls allow the users to modify their input values
and re-run the calculations by clicking the “Calculate” button again after
modifying the input values.
CS11.7 Extensions
1. Allow the users to view the calculation sheets by creating appropriate
navigational buttons and procedures.
2. Add an additional resolve option that allows the users to modify their portfolio at
any time.
3. Add an option to make stock return predictions based on new market return data
and the found beta values.
4. Add an option to compare the minimum MSE found from Option 1 and Option 2.
Chapter Overview
CS12.2 Spreadsheets
CS12.3 User Interface
CS12.4 Procedures
CS12.5 Resolve Options
CS12.6 Summary
CS12.7 Extensions
Case Study 12: Option Pricing 2
Let’s now discuss the calculations and assumptions for both of the user’s options. The
compare prices option calculates the option price for a given exercise price over various
volatility values. The Black Scholes formula, which calculates the option price, assumes
that the selected stock prices follow a lognormal random variable. It also assumes that
the risk free rate is continuously compounded. A different formula computes the option
price for the call options, C, and the put options, P. Both formulas are complex, so we
break them into two parts: d1 and d2. The formula uses the current stock price, S, the
exercise time, t, the exercise price, X, the risk free rate, r, and the annual volatility, v. It
also uses the cumulative distribution function of the normal distribution, FN(.).
ln( S / X ) + (r + v 2 / 2)t
d1 =
v t
d 2 = d1 − v t
C = S * FN (d1 ) − Xe − rt * FN (d 2 )
P = S *( FN (d1 ) − 1) − Xe − rt *( FN (d 2 ) − 1)
We consider three possible exercise prices for the call and put options. The exercise
prices for the call options are all greater than the current stock price, and the exercise
prices for the put options are all smaller than the current stock price. The option prices
are computed for each exercise price of the call and put options. These option prices are
recalculated for the new risk rates and for various volatility values in the compare prices
option. For the optimization option, they are used as input.
The optimization option seeks to determine the optimal number of options to purchase
for each option price. The objective function is to minimize the overall risk, which is the
standard deviation of the returns. The total cost should be less than or equal to the
available funds displayed in the input cells. The average returns should be greater than
or equal to the desired returns. And the desired returns are equal to the average returns
found using a set of initial trial values for the decision variables.
We employ the Solver to determine the solution of this optimization problem. The Solver
commands are:
We use bootstrapping on historical stock prices to estimate the final stock price of a
selected stock for 1000 months. For each estimated stock price, the call option revenue
and the put option revenue are calculated for each of the three exercise prices using
their corresponding option prices. The revenue for the call options is the value of the
current price minus the option price. The revenue for the put options is the value of the
option price minus the current price. Both revenues are considered to be zero if their
values are negative. The total revenues are the sum of the product of each option’s
revenue and the corresponding decision variable value. The returns are the difference
between the total cost of the options bought and the total revenue.
For more details on option pricing, please refer to Introduction to Probability Models by
Winston and Data Analysis and Decision Making by Albright, Winston, and Zappe..
CS12.1.2 Input
• Stock name
• Current stock price
• Current number of shares
• Volatility
• Risk free rate
• Available funds for buying options
• Current date
CS12.1.3 Output
CS12.2 Spreadsheets
This application requires four spreadsheets: the welcome sheet, the comparison option
sheet, the optimization option sheet, and the calculation sheet. The welcome sheet
contains the title, the description of the application, and the “Start” button. (See Figure
CS12.1.) The “Start” button shows the user several forms and then takes him or her to
either the comparison option sheet or the optimization options sheet.
The comparison option sheet allows the user to compare various option prices with
varying volatility values. (See Figure CS12.2.) The user’s input, which has already been
received from the user via a user form, is reported in a table. The initial risk rate appears
in a text box; however the user can change this value with the adjacent spin buttons. As
this value is changed, the option prices are recalculated using the new risk rate value
and various volatility values. These volatility values vary in a range whose center value
is the user’s initial volatility input value. The option prices are calculated for three
exercise prices. There are three exercise prices that are greater than the user’s current
stock price input for the call options and three exercise prices that are less than the
user’s current stock price input for the put options. The graphs reflect the option prices of
the varying volatility values for each of these exercise prices for both the call and put
options.
The optimization option sheet displays the result of an optimization performed that
determines the best number of call and put options to buy in order to minimize risk. (See
Figure CS12.3.) The user’s input appears in a table; below this table are the returns and
the risk of the user’s investment resulting from the optimization. Then, a table displays
the call options and the put options; each table shows the three exercise prices and their
corresponding option prices. The sheet also presents the optimal number of each of
these options to buy, which was determined by the optimization. The user can press the
“View Model” button to view the calculations sheet that performs this optimization.
The calculations sheet contains historical data and formulas used in the optimization.
(See Figure CS12.4.) The historical data includes the recorded returns for three different
stocks over 60 months. We employ this data to perform bootstrapping on the final stock
price for 1000 months. For each estimated stock price, the call option revenue and put
option revenue are calculated for each of the three exercise prices using their
corresponding option prices. The total revenues are then calculated using the decision
variable cells (the number to buy of each option). Next, the returns are calculated using
the total cost of the options bought; this total cost is also calculated using the decision
variable cells and the option prices. The total cost should be less than or equal to the
available funds displayed in the input cells. The objective function cell is the risk formula
cell; the risk is calculated as the standard deviation of the returns. The average returns
should be greater than or equal to the desired returns, and the desired returns are equal
to the average returns found using a set of initial trial values for the decision variables.
The second user form is the input form. (See Figure CS12.6.) We include this form to
gather all of the input values from the user. Frames group the textboxes and labels of
similar input. In the combo box, the user can select one of the three available stocks.
The compare prices sheet includes a spin button and textbox control to allow the user to
change the risk rate and update the option price values. The “End” navigational button
brings the user back to the welcome sheet, the “Return to Options Form” button
redisplays the options form, and the “View Model” and “View Results” buttons bring the
user to the calculations sheet and optimization option sheet, respectively.
CS12.4 Procedures
We will now outline the procedures for this application beginning with the initial sub
procedures and variable definitions. (See Figure CS12.7.) The Main procedure, which is
called from the “Start” button, presents the options form.
The procedures for the options form note which option the user has selected. (See
Figure CS12.8.) If the user has chosen the compare prices option, then he or she is
taken to the comparison sheet, the input form is displayed, and the ComparePrices
procedure is called. If the user has chosen the optimization option, the calculation sheet
and input form are displayed, and then the Optimize procedure is called.
The procedures for the input form record the user’s input. (See Figure CS12.9.) The
values from the text boxes are converted to double, integer, and date data types as they
are equated to their respective variables. The application employs the input for the
current stock price to create the exercise prices for the call and put options.
The BlkSchols function procedure, which appears in Figure CS12.10, is called from both
the ComparePrices and the Optimize procedures to compute the option prices with the
Black Schols formula. The input for this function is the exercise price, the option type
(call or put), and the volatility value. Since the function is a little complicated, we use two
variables to break it into parts. The first part uses the current price, the exercise price,
the risk rate, the volatility, and the exercise time (6 months = 0.5 years). The second part
uses the first part as well as the volatility and exercise time. We then use these parts to
compute the option price in the final formulation of the Black Schols equation. The final
computation varies for call and put options, so we use the option type input passed to
the function to determine which calculation to perform. This final calculation uses both
parts previously calculated as well as the current price, exercise price, risk rate, and
natural logarithm (stored as a constant variable).
The ComparePrices procedure performs the calculations for the comparison option.
(See Figure CS12.11.) It begins by displaying the input values on the compare prices
sheet, and it also outputs the three exercise prices for the call and put options. Next, it
initializes the risk rate controls (both the spin button and the text box) to present the risk
rate value provided by the user on the input form. It then uses the user’s volatility input
value to create a table of volatility values for the call and put options. Each table has a
range of volatility values of ten more than and ten less than the user’s initial value. The
procedure then loops over these volatility values and calculates the option price for each
exercise price in the call and put options for these volatility values. It performs this
calculation by calling the BlkSchols function procedure and passing the exercise price,
option type, and volatility value. Notice that the option price is only calculated for the
positive volatility values.
The application calls the Update procedure when the risk rate controls are changed. The
Change event procedure for the spin button and the LostFocus event procedure for the
text box update the control values and then call the Update procedure. (See Figure
CS12.12.) This procedure recalculates the option prices for all the volatility values in the
call and put option tables using the new risk rate value provided by the user. (See Figure
CS12.13.) The axis scales on the charts are also updated.
The Optimize procedure performs the calculations needed for the optimization option.
(See Figure CS12.14.) The procedure first calculates and stores the option prices for the
call and put options’ three exercise prices by calling the BlkSchols function procedure. It
then calls the RecordInput and DoCalc procedures, which perform the main optimization
calculations. The Optimize procedure then updates the summary report on the
optimization results sheet and takes the user to this sheet.
The RecordInput procedure records the user’s input on the calculation sheet. (See
Figure CS12.15.) The input values from the input form as well as the exercise prices and
option prices are then recorded.
The DoCalc procedure runs the Solver to find the optimization results. (See Figure
CS12.16.) Even though most of the formulas for the optimization have already been
inserted in the cells on the spreadsheet during preparation, the bootstrapping formulas
are inserted into the future stock value column using the stock selected by the user. The
trial decision variable values then determine the desired returns for the user. Finally, the
application prepares and runs the Solver, which determines the optimal solution.
The navigational procedures are for the “End,” “View Model,” and “View Results”
buttons. (See Figure CS12.17.)
Compare Prices Uses the risk rate controls to change the risk rate
Option and to calculate the updated option price values;
uses the “Return to Options Form” button to enter
Summary new values into the input form.
Optimization Uses the “Return to Options Form” button to enter
Option new values into the input form.
CS12.6 Summary
¾ This application provides the user with two options for working with European call
and put options: compare option prices for a set risk rate and various volatility
values; and find the optimal number of call and put options of various option
prices to buy in order to minimize risk for a desired return.
¾ This application requires four spreadsheets: the welcome sheet, the compare
prices sheet, the optimization option sheet, and the calculation sheet.
¾ The user interface requires navigational buttons, controls on the spreadsheet,
and two user forms.
¾ Several procedures for this application perform the compare prices calculations
and optimization calculations.
¾ The user can resolve the application by changing the risk rate controls for the
compare prices option or by selecting the “Return to Options Form” button to
enter new values in the input form for both options.
CS12.7 Extensions
1. Include American options as part of all the calculations. Let the user choose
between European and American options as part of the input form.
2. Allow the user to vary the exercise time of the options; in other words, do not
always assume a six-month option. Add this feature to the input form.
3. Allow the user to enter a desired return for the optimization option. Then provide
the option to minimize risk or maximize return before performing the optimization.
Chapter Overview
CS13.2 Spreadsheets
CS13.3 User Interface
CS13.4 Procedures
CS13.5 Resolve Options
CS13.6 Summary
CS13.7 Extensions
Case Study 13: Supply Chain Management 2
The supply chain management model is a linear programming model in which we are
trying to ascertain the number of products to produce and ship in order to minimize total
costs of the supply chain. We define the parts of the model below. Keep in mind that the
data given is multi-dimensional; in other words, some decisions are made per product i,
per factory j, per customer k, per period t, and per resource r.
Decision variables:
1. The number of each product to produce (in each factory for each period) = xijt
2. The number of each product to ship (from each factory to each customer for
each period) = yijkt
Constraints:
1. The amount shipped of each product (per factory in each period) must be
less than or equal to the amount produced of each product (at each factory in
each period). Remember that the amount shipped is also per customer;
therefore we must sum these values over all customers for each product,
factory, and period to compare it to the production amount.
2. The resources used (per factory per period) must be less than or equal to the
available resources (per factory per period). Here, resources used are equal
to the resources needed per product (given, Nri) times the amount produced
of each product, and the available resources are given (per factory per
period, Arjt).
3. The ending inventory (per product per factory per period, Vijt) must be greater
than 0 for each period. Here, we define the ending inventory of period t+1 as
equal to the ending inventory in period t plus the amount produced in period
t+1 minus the amount shipped in period t+1.
4. The amount shipped (of each product from each factory to each customer)
must meet the demand per customer. Here, the demand has been given per
product per customer per period, Dikt. Remember that the amount shipped is
also per factory; therefore we must sum these values over all factories for
each product, customer, and period to compare it to the demand values.
∑j yijkt ≥ Dikt
yijkt ≥ 0, xijt ≥ 0
Objective function: Minimize total costs = total shipping costs + total holding costs + total
production costs.
1. Total shipping costs = amount shipped * shipping costs per product from
each factory to each customer. Here, shipping costs have been given per
product per factory per customer, sijk. Remember that the amount shipped is
also per period; therefore we must sum these values over all periods for each
product, customer, and factory to multiply with the shipping costs.
∑t yijkt * sijk
2. Total holding costs = ending inventory * holding cost per product per factory
per period. Here, holding costs have been given per product per period, hit.
Remember that the ending inventory is also in terms of factories; therefore
we must sum these values over all factories to multiply with the holding costs.
∑j Vijt * hit
3. Total production costs = amount produced * production cost per product per
factory per period. Here, production costs are given per product per factory
per period, pijt.
xijt * pijt
4. Total costs:
To prepare this model, we use a spreadsheet that includes the Solver, which finds the
solution. We place empty cells for each decision variable and use these cells as well as
some input cells to create the formulas for the constraints and the objective function.
CS13.1.2 Input
CS13.1.3 Output
CS13.2 Spreadsheets
This application requires over twenty spreadsheets: the welcome sheet, a model
description sheet, seven input sheets, the model calculations sheet, a report sheet, a
detailed output sheet, and ten output sheets that include pivot tables and pivot charts.
The welcome sheet contains the title, the description of the application, and the “Start”
button. (See Figure CS13.1.) There is also a “Model Description” button in the
application description that takes the user to the model description sheet. (See Figure
CS13.2.) The model description sheet explains the supply chain management model in
detail.
The seven input sheets are organized to receive the multi-dimensional input in tables
corresponding to the problem size. The first of these is the demand sheet, which
receives input on the demand per product per customer per period. (See Figure
CS13.3.)
The available resources input sheet receives input on the amount of each resource
available for each factory in each period. (See Figure CS13.4.)
The resources needed input sheet receives input about the amount needed of each
resource for each product. (See Figure CS13.5.)
The holding costs input sheet receives the holding costs per product in each period.
(See Figure CS13.6.)
The production costs input sheet receives input on the production cost per product for
each factory in each period. (See Figure CS13.7.)
The shipping costs input sheet receives input for the shipping costs per product from
each factory to each customer. (See Figure CS13.8.)
The initial inventory input sheet receives input on the initial inventory of each product at
each factory. (See Figure CS13.9.)
The model calculations sheet references all of the input on these input sheets to set up
the constraints and the objective function for the model. (See Figure CS13.10.) This
sheet prepares the decision variable cells, the constraint formulas, the constraint right-
hand side values, and the objective function formulas so the Solver can optimize the
supply chain management problem.
Once the problem has been solved, the user is taken to the report sheet. (See Figure
CS13.11.) This sheet is used as a navigational interface for all of the output sheets. The
user can also view the model calculations sheet or a detailed output sheet. The detailed
output sheet stores the resulting decision variable and the cost values found by the
Solver. (See Figure CS13.12.)
The ten output sheets that the user can view include five output categories, each with a
pivot table output sheet and a pivot chart output sheet. We include pivot tables and pivot
charts to allow the user to filter and organize the results for greater clarity and analysis.
Pivot tables and pivot charts are especially helpful when working with multi-dimensional
data. (See Chapter 6.)
The number to produce output sheet contains a pivot table of the amount of each
product produced per factory per period. (See Figure CS13.13.) From here, the user can
also view the corresponding pivot chart sheet. (See Figure CS13.14.)
Figure CS13.13 The number to produce output sheet with pivot table.
The number to ship output sheet contains a pivot table of the amount of each product
that will ship from each factory to each customer in each period. (See Figure CS13.15.)
The corresponding pivot chart appears in Figure CS13.16.
Figure CS13.15 The number to ship output sheet with pivot table.
The shipping costs output sheet contains a pivot table with the total shipping costs for
each product from each factory to each customer. (See Figure CS13.17.) There is also
an associated pivot chart sheet. (See Figure CS13.18.)
Figure CS13.17 The shipping costs output sheet with pivot table.
The holding costs output sheet contains a pivot table with the total holding costs for each
product at each factory in each period. (See Figure CS13.19.) The corresponding pivot
chart sheet appears in Figure CS13.20.
Figure CS13.19 The holding costs output sheet with pivot table.
The production costs output sheet contains a pivot table with the total production costs
of each product in each factory for each period. (See Figure CS13.21.) There is also a
corresponding pivot chart sheet. (See Figure CS13.22.)
Figure CS13.21 The production costs output sheet with pivot table.
The first user form is the input form. (See Figure CS13.23.) It receives the parameters
that define the size of the problem: the number of products, the number of factories, the
number of customers, the number of resources, and the number of periods. For this
application, we assume these values are fixed as shown.
The second user form is a navigational form for the input sheets. (See Figure CS13.24.)
Each button on the form takes the user to the respective input sheet. The “Solve” button
on the bottom of the form is a functional button that calls the procedures to solve the
problem.
A “Return to Form” button on every input sheet takes the user back to the navigational
form for the input sheets. On the report sheet, the “View Model Sheet” button takes the
user to the model calculations sheet, and the “View Detailed Output” button takes the
user to the detailed output sheet. In Section CS13.5, we will discuss the “Resolve”
button. The other buttons take the user to the respective output sheets, and the “End”
button returns the user to the welcome sheet. Each output sheet contains two buttons:
“Return to Report” and “View Chart.” Each pivot chart sheet also contains two buttons:
“Return to Table” and “Return to Report.”
CS13.4 Procedures
We will now outline the procedures for this application beginning with the initial sub
procedures and variable definitions. (See Figure CS13.25.) The “Start” button calls the
Main procedure, which begins by calling the ClearPrev procedure. (We have currently
put a comment mark in front of this code so the user can use the default values if
desired.) It then initializes some variables and presents the input form. If the application
is dynamic, it would then need to call a procedure to create the tables using the size
values provided on the input form. Finally, it calls the navigation form for the input
sheets.
Figure CS13.25 The variable declarations and the Main and ClearPrev procedures.
The procedures for the input form simply display the default values for the problem size.
(See Figure CS13.26.) If the application allowed the user to change these values, the
new sizes would need to be recorded.
The procedures for the second user form include several navigational procedures to take
the user to the various input sheets. (See Figure CS13.27.)
Figure CS13.27 The navigational procedures for the input sheets’ navigational
form.
Several additional procedures for this form perform some error checking and call the
main procedure that solves the model, SolveSCM. (See Figure CS13.28.) The user is
then taken to the report sheet.
Figure CS13.28 The additional procedures for the input sheets’ navigational form.
The SolveSCM procedure is the main procedure of the application. (See Figure
CS13.29.) Before the model can be solved, the model sheet must be prepared.
However, since we assume this application is of a fixed parameter size, the necessary
formulas and references have already been prepared in Excel. Therefore, we can simply
activate this sheet and run the Solver commands. These commands set the objective
function as the overall costs and the decision variables as the amount per product to
produce and ship. Notice that the corresponding ranges have already been named in
Excel to simplify the Solver references. The shipping, resources, inventory, and demand
constraints are also set with the Solver commands. As the final Solver commands run,
the feasibility of the problem is checked and the problem is solved.
The report sheet requires several navigational procedures. (See Figure CS13.30.) These
include procedures for the “View Model Sheet” and “View Detailed Output” buttons as
well as the five buttons to view the output sheets.
Additionaly navigational procedures are required for the output sheets and other sheets.
(See Figure CS13.31.) The “Return to Report,” “View Chart,” and “Return to Table”
buttons all need the output sheet procedures. Notice that we use a worksheet variable
type here to avoid repeating these procedures for various sets of output sheets. The
other navigational procedures are for the “Model Description,” “Return to Application,”
“Return to Form,” “Resolve,” and “End” buttons. Notice that the Resolve procedure is
used for the “Return to Application,” “Return to Form,” and “Resolve” buttons.
Figure CS13.31 The navigational procedures for the output sheets and other
sheets.
CS13.6 Summary
¾ In this application, we seek the best production and shipping schedule that
minimizes the supply chain’s costs. These costs include the production costs, the
holding costs, and the shipping costs.
¾ This application requires over twenty spreadsheets: the welcome sheet, a model
description sheet, seven input sheets, the model calculations sheet, a report
sheet, a detailed output sheet, and ten output sheets that include pivot tables and
pivot charts.
¾ For this application’s interface, we use navigational and functional buttons as well
as two user forms.
¾ Several procedures in this application initialize and perform the model
calculations to find the optimal production and shipping plans that minimize
overall costs.
¾ The user can resolve the application by pressing the “Resolve” button on the
report sheet; he or she revisits the input sheets, modifies the values, and
resolves the model.
CS13.7 Extensions
1. If the user were able to change the size parameters of the problem, which sheets
would this affect?
2. If the user were able to change the size parameters of the problem, which
procedures would this affect?
3. If the user were able to change the size parameters of the problem, what are
some new procedures that would need to be created? Make these changes to
the application so it is dynamic. What other resolve options are now possible?
Chapter Overview
CS14.2 Spreadsheets
CS14.3 User Interface
CS14.4 Procedures
CS14.5 Resolve Options
CS14.6 Summary
CS14.7 Extensions
Case Study 14: Inventory Management 2
For all model descriptions, we will use the following notation: Q = order quantity, K =
order cost, h = holding cost, C = unit cost, S = shortage cost, D = mean demand. For
each model, we are trying to determine the optimal order quantity that minimizes the
total cost. The total cost, TC, is the sum of the order costs, holding costs, unit costs, and
shortage cost.
The EOQ model finds the optimal order quantity by solving the first order conditions of
the total cost equation. Here, we do not assume a shortage cost; therefore the total cost
equation is simply:
TC = CD + hC(Q/2) + K(D/Q)
The optimal value for Q can be determined from the following result:
2DK
Q=
hC
The EOQ model with Backorders does consider a shortage cost. The first order
conditions can then be solved to find Q, as follows:
2 KD h+S
Q= *
hC S
The Reorder Point model considers a shortage cost, and, more importantly, it assumes
that the demand is stochastic. Therefore, instead of assuming a constant demand equal
to the mean demand D, it uses the standard normal distribution with the mean and
standard deviation, V, of demand. We use this mean and standard deviation to compute
the mean and standard deviation of the demand during lead time, L. We represent the
mean demand during lead time with µ and the standard deviation of demand during lead
time with σ. We compute these values as follows:
µ = L*D
σ= V
∞
Loss(z) = ∫z
(t − z )φ (t )dt
where φ (t ) is the density function of the standard normal distribution. With this value, we
calculate Q in the next iteration. We perform three iterations of these calculations:
2DK
1. Q =
hC
hC * Q
2. P(Reorder) =
S *D
4. R = σ * z + µ
5. E(Shortage) = σ * Loss ( z )
K + S * E ( Shortage)
6. Q = 2D *
hC
For more details on these methods, please refer to Supply Chain Management:
Strategy, Planning, and Operations by Chopra and Miendl and Production and
Operations Analysis by Nahmias.
CS14.1.2 Input
• The general input: order cost, holding cost, lead time, and mean demand.
(The mean demand is either directly provided by the user or calculated
from historical data provided by the user.)
• The standard EOQ method: only the general input.
• The EOQ with Backorders method: the general input and the shortage
cost.
• The Reorder Point method: the general input, the shortage cost, and the
standard deviation of demand.
CS14.1.3 Output
• Costs: Annual total costs, order costs, holding costs, and shortage costs
• Strategy: Order quantity, number of orders, time between orders, and
reorder point
• Chart and table of inventory values for an example year using the solution
strategy
CS14.2 Spreadsheets
This application requires four spreadsheets: the welcome sheet, the historical data
sheet, the input sheet, and the results sheet. The welcome sheet contains the title, the
description of the application, and the “Start” button. (See Figure CS14.1.) The “Start”
button shows the user a form to determine how the demand information will be provided.
If the user selects to provide historical data, then the historical data sheet appears. The
“Run Demo” button takes the user to the historical data sheet and enters demo historical
data.
The historical data sheet allows the user to enter several demand values for past
periods. (See Figure CS14.2.) These values are used to calculate the mean and
standard deviation for the demand. The mean demand is used for the deterministic
methods (EOQ and EOQ with backorders), and the standard deviation of the demand is
used for the stochastic method (Reorder Point).
When the user is finished entering the historical demand or if he or she directly gave the
mean demand on the welcome sheet’s form, the input sheet next appears. The input
sheet is the main sheet of the application. (See Figure CS14.3.) On this sheet, the user
must enter the remaining general input: the order cost, the holding cost, and the lead
time. He or she must then select the method to find the optimal inventory ordering
strategy. If the user selects the EOQ method, then no more input is necessary; he or she
can see the solution by pressing the “Solve” button. If the user chooses the EOQ with
Backorders method, then the application requires a shortage cost before solving. If the
user selects the Reorder Point method, then the application requires a shortage cost and
the demand standard deviation before solving. Note that even though the calculated
mean and the standard deviation are shown in their respective input cells on this sheet,
the user can overwrite or change them at any time.
Once the problem has been solved, the user is taken to the results sheet. (See Figure
CS14.4.) This sheet displays the costs and inventory ordering strategy for the found
solution. The costs include the total annual cost, the annual ordering cost, the annual
holding cost, and the annual shortage cost. The ordering strategy includes the order
quantity, the number of orders, the time between orders, and the reorder point. A chart
and table show the inventory levels for an example year of data following the solution
strategy.
(a) (b)
Figure CS14.5 The data form.
On the input sheet, three option buttons allow the user to select the method to solve the
problem. If he or she selects the Standard EOQ option, then no further input is required.
(See Figure CS14.6.) If he or she selects the EOQ with the Backorders option, then the
input cell for the shortage cost becomes available. (See Figure CS14.7.) If he or she
selects the Reorder Point option, then the input cells for the shortage cost and demand
standard deviation become available. (See Figure CS14.8.)
Figure CS14.7 Selecting the EOQ with the Backorders option button.
The “End” button is a navigational button that brings the user to the welcome sheet. The
“Continue” and “Solve” buttons are both functional buttons. “Continue” calculates the
mean and standard deviation of the historical demand, and “Solve” calls the
corresponding procedure for the method selected by the user. We will discuss the
“Resolve” button, a navigational button, Section CS14.5.
CS14.4 Procedures
We will now outline the procedures for this application beginning with the initial sub
procedures and variable definitions. (See Figure CS14.9.) The Main procedure, which is
called from the “Start” button, calls the ClearPrev procedure and displays the data form.
The RunDemo procedure also calls the ClearPrev procedure and copies and pastes the
demo historical data to the historical data sheet; it then takes the user to the historical
data sheet. The ClearPrev procedure clears any previous historical data and initializes
several variables.
Figure CS14.9 The variable declarations and the Main, RunDemo, and ClearPrev
procedures.
The procedures for the data form record which option the user has selected. (See Figure
CS14.10.) If the user chooses to enter historical data, then the historical data sheet
appears. If the user chooses to provide the mean demand, then the mean demand is
recorded and the user views the input sheet. Since this form is dynamic in that it only
shows the mean demand text box if the corresponding option button is selected, there is
also some small code to change the colors of this text box and label when the second
option button is clicked.
If the user opts to enter historical data, then he or she presses the “Continue” button to
proceed to the input sheet. This button is assigned to the Continue procedure. (See
Figure CS14.11.) The Continue procedure calculates the mean and standard deviation
of the provided demand values. The DataCheck function procedure is then called to
check if the data is stationary. (See Figure CS14.15.) This check is necessary before
assuming a constant demand when using the EOQ and EOQ with Backorders methods.
The Continue procedure then initializes the input cells on the input sheet and takes the
user to this sheet.
The UserInput procedure is called from the “Solve” button on the input sheet. (See
Figure CS14.11.) It records the input values and then calls the corresponding procedure
for the method selected by the user.
The SolveEOQ procedure is called to use the Standard EOQ method. (See Figure
CS14.12.) This procedure begins by ensuring that the demand is stationary (by checking
the variable set in the DataCheck function). If it is, then the EOQ and total costs are
calculated. The application then displays the costs and strategy on the results sheet.
Next, the Results procedure is called.
The SolveBackOder procedure is called to use the EOQ with Backorders method. (See
Figure CS14.13.) The procedure begins by ensuring that the demand is stationary
(again, by using the variable set in the DataCheck function). If it is, the application then
calculates the EOQ and costs. The costs and strategy appear on the results sheet, and
the Results procedure is then called.
The application calls the SolveReOrder procedure to use the Reorder Point method.
(See Figure CS14.14.) This procedure begins by finding the best order quantity and
reorder point. To do so, we perform three iterations of these calculations in order to
ensure accuracy. The first iteration employs the standard EOQ method to find the order
quantity, which is then used to find the probability of reordering. Next, this probability is
standardized and used to find the reorder point. The application then uses the reorder
point with the loss function to find the expected number of shortages. This value is then
employed to find the order quantity in the next iteration.
To use the loss function, we call the LossFunc function procedure by passing the
standardized probability of reordering. This function finds the partial integration of the
standard normal curve for this value. (See Figure CS14.15.)
The application calls the Results procedure to update the results sheet after the selected
method is completed. (See Figure CS14.16.) It generates example data and updates the
chart to illustrate the inventory order strategy to the user. The results sheet is then
displayed.
The navigational procedures apply to the “End” and “Resolve” buttons. (See Figure
CS14.17.)
CS14.6 Summary
¾ The Inventory Management application allows the user to plan an inventory
ordering strategy using either the Standard EOQ, EOQ with Backorders, or
Reorder Point method.
¾ This application requires four spreadsheets: the welcome sheet, the historical
data sheet, the input sheet, and the results sheet.
¾ The user interface requires navigational and functional buttons, controls on the
spreadsheet, and a user form.
¾ Several procedures in this application record the input values and perform the
calculations using the selected method.
¾ The user can resolve the application by pressing the “Resolve” button on the
results sheet to return to the input sheet. There, he or she can select a different
method or change the input values and resolve the problem.
CS14.7 Extensions
1. For the Reorder Point method, add the calculation of the safety stock. Ensure
that this value is now also displayed on the results sheet.
2. Add a new method option for Quantity Discounts. What procedures are affected?
Is any new input needed? Are any new results displayed?
3. Add another resolve option that allows the user to return to the historical data
sheet to append more data and resolve the problem with the same general input
values. Ensure that the mean and standard deviation of the demand are updated.
Chapter Overview
CS15.2 Spreadsheets
CS15.3 User Interface
CS15.4 Procedures
CS15.5 Resolve Options
CS15.6 Summary
CS15.7 Extensions
Case Study 15: Forecasting Methods 2
In this section, we describe the parameters and models for performing the following
adaptive methods for forecasting calculations: Moving Averages, Exponential
Smoothing, Holt's Method for Trends, and Winter's Method for Seasonality.
The Moving Averages method forecasts the demand in a period t to be the average of
the previous N periods of historical data. We use this method when the historical data
does not seem to follow any trend or seasonality. The parameters for this method are
the initial period to begin forecasting, ti, the final period to forecast, tf, and the number of
historical periods to use, N, in finding the average demand for the next period. The user
must enter an initial period, ti, greater than the number of historical periods used in the
calculations, N.
N
Lt = (∑ Di ) / N
i =1
Ft +1 = Lt
The Exponential Smoothing method, which is also employed when the historical data
does not seem to follow any trend or seasonality, forecasts demand by using the
average demand over all historical periods, n. The parameters for this method are the
initial period to begin forecasting, ti, the final period to forecast, tf, and a smoothing
constant α between 0 and 1.
1 n
L0 = ∑ Di
n i =1
The demand base is updated using the smoothing constant α for each period t:
Lt +1 = α Dt +1 + (1 − α ) Lt
Ft +1 = Lt
Holt’s Method for Trends is used when the historical demand data seems to follow a
trend. The user input for this method is the initial period to begin forecasting, ti, the final
period to forecast, tf, a smoothing constant α, and a smoothing constant β. Both α and β
must be between 0 and 1.
We determine an initial estimate of the base and trend by performing a linear regression
on the relationship between the demand D and each period t. We perform this
regression in Excel with the INTERCEPT and SLOPE functions in order to find the initial
base and initial trend values, respectively.
Dt = L0 + T0t
The base and trend values are then updated using the smoothing constants α and β:
Lt +1 = α Dt +1 + (1 − α )( Lt + Tt )
Tt +1 = β ( Lt +1 − Lt ) + (1 − β )Tt
Ft +1 = Lt + Tt
Note that with this method, forecasts for future periods are made with the following:
Ft + n = Lt + nTt
We use Winter’s Method for Seasonality when the historical demand seems to follow
both trend and seasonality. The user input for this method is the initial period to begin
forecasting, ti, the final period to forecast, tf, the number of periods per season, p, a
smoothing constant α, a smoothing constant β, and a smoothing constant γ. All
constants α, β, and γ must be between 0 and 1.
In this method, we also want to determine the initial base and trend estimates through
linear regression. However, since there is seasonality, we must first deseasonalize the
demand before using a linear regression. To deseasonalize the demand, we find the
values Dt :
t −1+ ( p / 2)
Dt = ( Dt −( p / 2) + Dt + ( p / 2) + ∑
i = t +1− ( p / 2)
2 Di ) / 2 p , for p even
t + ( p / 2 )
Dt = ∑
i = t − p / 2
Di / p , for p odd
Dt = L0 + T0t
Once we determine these estimates, we recalculate the deseasonalized demand for all
historical data periods (t = 1 to n) with the same equation. With this deseasonalized
demand, we can also calculate estimates for the seasonal factors, St .
St = Dt / Dt
Using these seasonal factor estimates for all historical data periods, we can then find the
seasonal factors for each period of seasonality. Here, we use r as the number of
seasonal cycles in the historical data, or r = n / p.
r −1
Si = (∑ S jp +i ) / r
j =0
We then update the base, trend, and seasonal factors using the smoothing constants α,
β, and γ, as follows:
Lt +1 = α ( Dt +1 / St +1 ) + (1 − α )( Lt + Tt )
Tt +1 = β ( Lt +1 − Lt ) + (1 − β )Tt
St + p +1 = γ ( Dt +1 / Lt +1 ) + (1 − γ ) St +1
Ft +1 = ( Lt + Tt ) St +1
Note that with this method, forecasts for future periods are made with the following:
Ft + n = ( Lt + nTt ) St + n
Please see Supply Chain Management: Strategy, Planning, and Operations by Chopra
and Meindl for more details.
CS15.1.2 Input
The input for this application is the parameters for each forecasting method.
CS15.1.3 Output
The output for this application is the MSE for each forecasting method and the best
method that has the minimum MSE.
CS15.2 Spreadsheets
This application requires four spreadsheets: the welcome sheet, the historical data
sheet, the compare methods sheet, and the best method sheet. The welcome sheet
contains the title, the description of the application, an image, a “Run Demo” button, and
a “Start” button. (See Figure CS15.1.) The “Run Demo” button enters the historical data
for the user, and the “Start” button allows the user to enter his or her own historical data.
They both bring the user to the historical data sheet.
The historical data sheet stores the user’s historical data, which is indicated as the
demand period. (See Figure CS15.2.) When the user has finished entering or modifying
the historical data, he or she can press the “Continue” button to go to the compare
methods sheet.
The compare methods sheet is the main sheet of the application where the user can
select a forecasting method with the check boxes. (See Figure CS15.3.) A box of
corresponding parameter controls is displayed for the selected method. The user can
enter or modify these parameters and then press the “Forecast” button to see a forecast
of the data, which is charted with the historical data. The MSE for the selected method
with the given parameters is then calculated and displayed in the table on the bottom of
the sheet. The user can press the “Find Best Method” button to proceed to the best
method sheet.
The best method sheet displays the name, parameters, and MSE value for the best
forecasting method. (See Figure CS15.4.) This method is determined by finding the
minimum MSE value among all the forecasting methods with the given parameters. The
sheet also contains a chart with the historical data and the forecast data of this method.
The “Compare Methods” button returns the user to the compare methods sheet from the
best method sheet. The “Continue,” “Forecast,” and “Find Best Method” buttons are
functional buttons that call procedures, which we will discuss later.
We create the main user interface using controls on the compare methods sheet. (See
Figure CS15.5.) Four check boxes allow the user to select between the four available
forecasting methods. A rectangle, acting as a frame, groups the text boxes and the
labels for each set of parameters. Each group of controls is layered so that, depending
on which method is selected, the corresponding parameter controls are at the front.
Figure CS15.5 Forecasting and the parameters for the Moving Average Method.
For example, in Figure CS15.5, the Moving Average check box is selected, and,
therefore, the group of controls for the Moving Average parameters appears. In Figure
CS15.6, the Winter’s Method with Seasonality check box is selected, and the group of
controls changes to display the parameters for the Winter’s Method.
Figure CS15.6 Forecasting and the parameters for the Winter’s Method.
CS15.4 Procedures
We will now outline the procedures for this application, beginning with the initial sub
procedures and the variable definitions. (See Figure CS15.7.) The “Start” button calls the
“Main” procedure, which, in turn, calls the “ClearPrevious” procedure and takes the user
to the historical data sheet. The “RunDemo” procedure, called from the “Run Demo”
button, copies a set of historical data and takes the user to the historical data sheet.
Finally, the “ClearPrevious” procedure clears the previous data on all the sheets.
Figure CS15.7 The variable declarations and the “Main,” “RunDemo,” and “ClearPrev”
procedures.
The “Continue” button on the historical data sheet calls the “Continue” procedure, which
counts the number of periods of historical data entered by the user..(See Figure
CS15.8.) It then initializes the controls on the compare methods sheet, where it takes the
user.
The “Calc” procedure, called from the “Forecast” button, checks which method the user
selected and records the corresponding parameters. (See Figures CS15.9 and
CS15.10.) It then calls one of the following procedures to perform the calculations for
this method: “MovAvg,” “ExpSm,” “Holt,” and “Winter.”
The “MovAg” procedure performs the calculations for the Moving Average method by
using the initial period, the final period, and the number of historical periods for
determining the average. (See Figure CS15.11.) The “ExpSm” procedure performs the
calculations for the Exponential Smoothing method using the initial period, the final
period, and the smoothing constant. (See Figure CS15.12.) The “Holt” procedure
performs the calculations for Holt’s Method with Trends. (See Figure CS15.13.) This
method requires initialization of the base and trend values before it employs the user’s
parameters. These parameters include the initial period, the final period, and two
smoothing constants. The “Winter” procedure performs the calculations for Winter’s
Method with Seasonality. (See Figures CS15.14 and CS15.15.) This method requires
initialization of the base and trend values, which it calculates by first deseasonalizing the
data. Next, it employs the initial period, the final period, the number of periods in a
season, and three smoothing constants specified by the user. Each of these procedures
ends by calculating the MSE.
Once the selected method calculations are performed, the application calls the
“UpdateChart” procedure, which updates the chart on either the compare methods or
best method sheet by resetting the source data to include the newly forecasted data
values. (See Figure CS15.16.)
The “FindBest” procedure is called from the “Find Best Method” button on the compare
methods sheet. (See Figure CS15.17.) This procedure begins by calling the “Calc”
method to run each of the forecasting method procedures with the last parameters set
by the user. It then evaluates the MSE values for each method to find the method with
the minimum MSE. Once it does, it recalls the corresponding calculation procedure to
produce the forecast data to graph. It then displays the minimum MSE value for this
method, the method’s name, and the method’s parameters on the best method sheet.
The navigational procedures are for the “End” buttons on all the sheets and for the
“Compare Methods” button on the best method sheet. (See Figure CS15.18.)
CS15.6 Summary
¾ The forecasting methods application allows the user to calculate forecasts using
Moving Averages, Exponential Smoothing, Holt’s Method with Trends, and
Winter’s Method with Seasonality. It also determines the best method for the
given historical data by finding the method that yields the minimum MSE.
¾ This application requires four spreadsheets: the welcome sheet, the historical
data sheet, the compare methods sheet, and the best method sheet.
¾ For this application’s user interface, we use navigational and functional buttons
as well as controls on the spreadsheet.
¾ Several procedures for this application initialize and perform the forecasting
calculations for the selected methods.
¾ The user can resolve the application by changing the method parameters and re-
forecasting the data or re-determining the best method.
CS15.7 Extensions
1. Modify the application so more than one method can be calculated and graphed
at a time. For example, if the user wants to compare the forecast data for two
methods at one time, he or she should be able to select both methods on the
compare method sheet and see the graphed results for both methods.
2. Modify the application so a user can compare forecasts for one method with
different sets of parameters. How would you create this new interface?
3. Create a procedure to optimize the parameters for all of the methods. (Hint: Use
the Solver.)
Chapter Overview
CS16.2 Spreadsheets
CS16.3 User Interface
CS16.4 Procedures
CS16.5 Resolve Options
CS16.6 Summary
CS16.7 Extensions
Case Study 16: Capital Budgeting 2
This application has both deterministic and stochastic input. The stochastic input
includes the sales volume, the product development cost, and the time horizon. Since
these values are unknown, we prompt the user for the best case, worst case, and most
likely values they have. A triangular random variable analyzes the values that may occur
between the best case and worst case values. For this application, we want to generate
a random number from the triangular distribution for a given probability of this input. We
therefore model the inverse triangular distribution function; for a best case, a, worst
case, b, and most likely value, m, and a given probability, p, the formula is:
a≤m≤b
(b − a )(m − a ) p m−a
F −1 ( p) = a + , if 0 ≤ p ≤
2 b−a
(b − a )(b − m)(1 − p ) m−a
F −1 ( p) = b − , if ≤ p ≤1
2 b−a
The Triang function procedure takes as input the best case, worst case, and most likely
values and outputs a random number that is found from this triangular distribution for a
given probability.
We use this function to create values for this stochastic input during the simulation runs.
In each run, these values are updated and placed on a simulation calculation
spreadsheet that contains several formulas. The formulas are therefore updated with
these new values, and the run results are recorded.
'TIME HORIZON
NumYears = Triang(Time(i, 1), Time(i, 2), Time(i, 3))
Range(Range("Prod" & i & "NPV").Offset(-1, 0), Range("Prod" & i & "NPV") _
.Offset(-1, NumYears)).Name = "Prod" & i & "Time"
'SALES VOLUMES
For year = 1 To NumYears
Range("Prod" & i & "Start").Offset(6, year + 1).Value = Triang(Sales(i, 1), Sales(i, 2),
Sales(i, 3))
Next
Range(Range("Prod" & i & "Start").Offset(6, NumYears + 2), _
Range("Prod" & i & "Start").Offset(6, 11)).ClearContents
The simulation calculations seek to determine the total profit and net present value
(NPV) of each product given its input values. The deterministic input values include the
tax rate, the discount rate, the labor cost, the variable cost, and the selling price. Using
these values as well as the stochastic values calculated for each run, the application
calculates the depreciation, the before tax profit, the after tax profit, the cash flow, and
the NPV. The simple formulas for these calculations appear in the table below; the NPV
Excel function calculates the NPV.
Before tax profit Labor Cost + Selling Price * Sales Vol – Variable
Cost * Sales Vol – Depreciation
After tax profit ((1-Tax Rate) * Before Tax Profit
Range(“Prod” & i & “Time”)… Range(“Prod1Time”) …
Cash flow
Prod. Devel. Cost After Tax Profit + Depreciation
Range(“Prod” & i & “NPV”)
NPV (NPVfunction using Discount
Rate and Time Horizon)
CS16.1.2 Input
This application requires two types of input: deterministic and stochastic. The
deterministic input is:
• Tax rate
• Discount rate
• Labor cost
• Variable cost
• Selling price
• Desired NPV
CS16.1.3 Output
This application has two types of output: output per product and overall, or comparative,
output. The output per product is:
• Depreciation
• Before tax profit
• After tax profit
• Cash flow
• Actual NPV
CS16.2 Spreadsheets
This application requires five spreadsheets: the welcome sheet, the input sheet, the
simulation calculations sheet, the simulation details sheet, and the results sheet. The
welcome sheet contains the title, the description of the application, and the “Start”
button. (See Figure CS16.1.) The “Start” button on the welcome sheet prompts the user
for the input values and then displays the input sheet.
The input sheet stores the input values for each product entered by the user. (See
Figure CS16.2.) These input values are recorded from a user form, which we will
describe in the next section. This sheet stores the deterministic values for the labor cost,
the variable cost, the selling price, the tax rate, the discount rate, and the desired NPV.
The stochastic input is recorded by best, worst, and most likely values for the sales
volume, production development cost, and time horizon.
(a)
(b)
Figure CS16.2 The input sheet.
In the simulation calculations sheet, the application calculates the simulation formulas to
determine each product’s total profit and NPV. (See Figure CS16.3.) The deterministic
input values are referenced from this sheet as input for the formulas for depreciation,
before tax profit, after tax profit, cash flow, and NPV. The stochastic input values are
updated for each run of the simulation using the triangular distribution function with the
best, worst, and most likely parameters provided by the user. As these values are
updated for each run of the simulation, the formula results are also updated and
recorded on the simulation details sheet.
The simulation details sheet records the NPV and total profit values for each product for
each run of the simulation. (See figure CS16.4.) The application employs these values to
determine each product’s minimum, maximum, and average NPV and profit values,
which are stored on the results sheet.
The results sheet displays each product’s minimum, maximum, and average NPV and
profit values in a table. (See Figure CS16.5.) It also uses the simulation details sheet
values to calculate the probability that the NPV will be greater than or equal to the
desired NPV provided by the user. The comparative, or overall, output shows which
product has the largest average NPV and profit values.
Then, the user views an input form for each product he or she is comparing. (See Figure
CS16.8.) This form prompts the user for the deterministic and stochastic input; each set
of input values is grouped into a respective frame. Text boxes are used for all the data,
and the stochastic input text boxes are arranged in a tabular format since each requires
a best, worst, and most likely value.
This same form is shown to the user for each product; therefore, the product number
label is updated for each product shown. Default values are provided for all the input,
including the product name. As the user enters the input values for each product, they
are recorded to the input sheet, which appears in the background. For example, in
Figure CS16.9, the input values have already been entered and recorded for the first two
products on the input sheet. The input form now prompts the user for the input values of
the third product.
Figure CS16.9 The input form appears in front of the input sheet.
CS16.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and initial sub procedures. (See Figure CS16.10.) The Main procedure is
called from the “Start” button. It calls the ClearPrev procedure to clear previous values
and tables from all the sheets. It then prompts the user with the two input boxes for the
number of products and the number of runs for the simulation. Notice that the error
checking code is used for both of these input boxes. ClearPrev then uses the number of
products to re-dimension some arrays and takes the user to the input sheet. Once the
input sheet appears, the Main procedure performs a loop to display the input form for
each product. The product number label on the form is updated in each loop along with
the default value of the product name.
The input form procedures record all of the input values on the input sheet. (See Figure
CS16.11.) Notice that the variable i used in the loop in the Main procedure to show the
input form is used in the input form procedures to determine on which row on the input
sheet values will be recorded. This variable also indexes the arrays to store the
stochastic input parameters. This code also performs some error checking to ensure that
the value for the time horizon worst case input is less than or equal to 10.
The Main procedure ends by calling the FormatSheets and RunSim procedures.
The RunSim procedure performs the simulation calculations. (See Figure CS16.12.) For
each simulation run, each product’s production development cost, time horizon, and
sales volume values are updated with the triangular distribution function. The formulas
on the simulation calculation sheet are then updated, and the resulting NPV and profit
values are recorded on the simulation details sheet. When all of the runs are complete,
the RunSim procedure calls the CreateResults procedure.
The Triang function procedure generates a possible value from the triangular distribution
function using the best case, worst case, and most likely case values as parameters.
(See Figure CS16.12.) These parameter values are passed to the Triang function
procedure using the corresponding arrays for product development cost, time horizon,
and sales volume. (Refer to Section CS16.1.1 for details about the triangular distribution
function.)
Figure CS16.12 The RunSim procedure and the Triang function procedure.
The CreateResults procedure displays the results on the results sheet. (See Figure
CS16.13.) First, it first creates some dynamic range names on the simulation details
sheet. Then, with these range names, it finds the minimum, maximum, and average NPV
and profit values for each product. It next loops over all of the average NPV and profit
values to find the product with the best NPV and the product with the best profit.
The FormatSheets procedure creates tables for each product on various sheets. (See
Figure CS16.14.) It begins by creating tables for each product on the simulation
calculations sheet. It also creates some dynamic range names on this sheet; these
names are used in the formulas for the simulation calculations. FormatSheets then
references the input values from the input sheet on these new tables on the simulation
calculation sheet. It then creates columns for storing the simulation results on the
simulation details sheet and also updates some of its dynamic range names. Finally, it
formats the results sheet by creating the tables for each product to store the results.
Notice that for each product, tables are created by copying and pasting an initial table
that is never cleared from the sheet.
Figure CS16.16 The buttons on the results sheet; both “Resolve” and “Return to
Input” lead the user to the resolve options.
Figure CS16.18 The buttons on the input sheet; “Start Simulation” resolves the
application.
“Resolve” button on the Returns the user to the input sheet where
results sheet he or she can change the input values
before resolving the problem.
Summary “Start Simulation” button Re-calls the RunSim procedure to re-run
on the input sheet the simulation.
CS16.6 Summary
¾ In this application, the user can consider which of several possible future
products would be most beneficial to invest in.
¾ This application requires five spreadsheets: the welcome sheet, the input sheet,
the simulation calculations sheet, the simulation details sheet, and the results
sheet.
¾ For this application’s user interface, we use navigational buttons, input boxes,
and a user form.
¾ Several procedures in this application collect the input for the model and solve
the model by running a simulation.
¾ The user can resolve this application by pressing the “Resolve” button on the
results sheet or the “Start Simulation” button on the input sheet.
CS16.7 Extensions
1. Add to the results sheet the product that has the highest probability of having a
NPV greater than or equal to the desired NPV.
2. Add histograms to the results sheet that illustrate the frequency with which each
product’s profit and NPV values were achieved. (Remember to also add to the
ClearPrev procedure so that each histogram is deleted for the previous
products.)
3. Restructure this application to solve the capital budgeting problem with an integer
programming model. Which procedures change and which can be retained?
Which sheets change and which can be retained?
Chapter Overview
CS17.2 Spreadsheets
CS17.3 User Interface
CS17.4 Procedures
CS17.5 Resolve Options
CS17.6 Summary
CS17.7 Extensions
Case Study 17: Portfolio Management and Optimization 2
The underlying model for optimizing the portfolio investment plan minimizes risk as the
objective subject to certain constraints. These constraints include: the cash invested
must be less than or equal to the cash available for investing; the return on the portfolio
must be greater than or equal to the minimum return desired for the entire portfolio; and
the amount invested per stock must be less than or equal to a maximum amount to be
invested in each stock.
We will now outline this model mathematically. Let xi be the amount we invest per stock i
in the portfolio. The index i varies from 1 to n, where n is equal to the number of stocks
in the portfolio.
Let ri represent the expected return on stock i. The expected returns are determined
based on each stock’s historical data of annual returns from the past five years.
Let vi represent the variance of stock i. This variance is calculated based on historical
data of quarterly market prices for the past four quarters and the current quarter. To
calculate the variance, we first determine the quarterly variance for each stock by finding
the difference between each quarter’s market price and the mean market price. We then
calculate the average variance for each stock.
Let ρij be the covariance between two stocks i and j. The covariance shows the
relationship between two stocks. If the covariance is positive, then as one stock value
increases so does the other stock value at a ratio equal to the covariance value. If the
covariance is negative, then as one stock value increases, the other stock value
decreases at a ratio equal to the covariance value. We define the covariance factor for
each pair of stocks i and j as the following product:
We can now define the risk of the portfolio, z, as the sum of the product of the square of
the investment and the square of the variance over all stocks plus the total covariance
factor for all stocks in the portfolio.
z = ∑i (xi2 * vi2) + ∑i∑j ρij (vi vj) (xi xj) for i and j = 1 to n
The return on the portfolio is defined as the sum of the product of the investment amount
and the expected return over all stocks.
Let’s represent the optimization inputs from the users as follows: C = total cash available
for investing; D = desired return on the portfolio; and M = maximum amount to invest per
stock. We can now finalize the mathematical model representation.
Subject to:
∑i xi ≤ C for i = 1 to n
xi ≤ M for i = 1 to n
We will use the Premium Solver to solve this nonlinear programming problem. To find
out more about calculating risk by variance in market price, please refer to Investment
Science by Luenberger.
CS17.1.2 Input
Each selected stock’s annual return and market price change are recorded to calculate
the expected return and price variance.
Before solving the optimization, we prompt the users for the following input:
CS17.1.3 Output
The output reported to the users after the optimization is complete consists of the
following:
• Minimum return
• Cash used
• Portfolio return
• Amount to invest per stock
CS17.2 Spreadsheets
We use nine spreadsheets in this application: the welcome sheet, four data sheets, two
information sheets, a model sheet, and an output sheet. The welcome sheet contains
the application title, a description, and some images (see Figure CS17.1). The “Start”
button calls the Main sub procedure and displays the Main Menu to the users.
Let’s first discuss three of the four data sheets. The first data sheet, the “All Stocks”
sheet (see Figure CS17.2), contains the complete list of stocks available for this
application. The sheet lists each stock’s name and category, as well as its quarterly price
for five quarters (including the current price) and the annual return for five years.
The second data sheet is the “Covariance” sheet (see Figure CS17.3). This sheet stores
the covariance values (ρij) between all pairs of stocks. This data will be used in preparing
the optimization model to calculate the total covariance factor for the portfolio.
The third data sheet is the “Portfolio” sheet (see Figure CS17.4). It records the
information from the “All Stocks” sheet for the stocks in the users’ portfolios. We will also
use this sheet later to perform some calculations for the optimization (see Figure
CS17.5). These calculations are for the mean price over five quarters, the variance in
price for each quarter, the sum of the variances, and the sum of the annual returns.
The first sheet that the users visit is the “Create/Edit Portfolio” sheet (see Figure
CS17.6); it is the first of two information sheets. In this sheet, the users can create or
modify their portfolios by performing one of the two following actions: selecting several
stocks from the “All Stocks” list and clicking the “Add” arrow button to add these stocks
to their portfolios; or selecting several stocks from the “Portfolio” list and clicking the
“Remove” arrow button to remove these stocks from their portfolios.
If the users are not sure which stocks are best to add or remove from their portfolios,
they can click the “Compare All Stocks” button to proceed to the second information
sheet: the “Compare Stocks” sheet (see Figure CS17.7). Otherwise, if they feel their
portfolios are complete, they can click the “Save Portfolio and Return to Main Menu”
button to return to the Main Menu. The “Exit” button allows the users to exit the
application.
The “Compare Stocks” sheet allows the users to select several stocks from the “All
Stocks” list and plot either their annual returns or the change in market price. The users
select one of these options from the combo box on the spreadsheet and then press the
“Show Plot” button. The corresponding chart then appears. The users can then return to
the “Create/Edit Portfolio” sheet by clicking the “Return to Create/Edit Portfolio” button.
The users can also exit the application at this point by clicking the “Exit” button.
To create these charts, we use the forth data sheet: the “Chart Data” sheet (see Figure
CS17.8). If the users have selected to plot the change in market price, then we copy the
quarterly prices for the selected stocks from the “All Stocks” sheet and paste them on
the first table shown. If the users have selected to plot the annual returns, then we copy
the annual returns from the “All Stocks” sheet and paste them on the second table
shown. The two overlaying graphs on the “Compare Stocks” sheet refer to these tables
as their source data.
After the users have finalized their portfolios, they complete an input form and then come
to the output sheet, which we call the “Investment Report” sheet (see Figure CS17.9).
This output sheet graphs the investment amounts for each stock in the users’ portfolios
and displays the values in an adjacent table. The minimized risk, or variance, of the
portfolio is also provided. The return and desired return are displayed, as well as the
cash used and the total cash available.
The “Modify Input” and “Modify Portfolio” buttons correspond to two resolve options,
which we discuss later. The “View Model” button allows the users to view the model
sheet, and the “Exit” button allows them to exit the application.
The model sheet lists all of the stocks in the portfolio with adjacent cells for the
investment amount (see Figure CS17.10). These are the changing cells whose values
are determined when optimization is performed. These values, solved as percentages,
are converted into dollar amounts in the next column. We refer to the dollar amounts to
check the constraint that each stock receives less than or equal to the maximum
investment amount per stock. The code then calculates the expected return and
variance and displays these figures in the next two columns. We use these values in the
third column; their sum is the risk value. This risk value, in the “Minimum Risk” cell at the
top of the sheet, is the objective function cell. The code procedures also calculate the
formulas for the forth column: the covariance factors. These factors are calculated for all
pairs of stocks in the portfolio using the stock in each row as the pivot. The total
covariance factor is the sum of the values in this column. The “Min Return” and “Unit
Constraint” columns capture the other two constraints.
new portfolio, edit their current portfolio, or optimize their portfolio investment plan. We
use a frame and option buttons to present these three options.
If the users select to create a new portfolio, then they are taken to the “Create/Edit
Portfolio,” which is cleared of past data. If the users select to edit their current portfolio,
then they are taken to the same sheet, except no clearing is done. If the users select to
optimize their portfolio investment plan, then the optimization code is run and the users
are taken to the output sheet. We include some error checking, which assures that the
users create a portfolio before optimizing.
The users can select the function buttons to “Add” and “Remove” stocks to and from
their portfolio on the “Create/Edit Portfolio” sheet. If they use the navigational button to
“Compare All Stocks,” then they can use the control on the “Compare Stocks”
spreadsheet. This control is a combo box that lists the two plot options: “Change in
Market Price” and “Annual Return” (see Figure CS17.12). Once the users make a
selection, they can use the “Show Plot” functional button to display the plot for the
selected stocks.
Figure CS17.12 The combo box control on the “Compare Stocks” spreadsheet.
Once the users save their portfolio with the “Save Portfolio and Return to Main Menu”
functional button on the “Create/Edit Portfolio” sheet, they may then optimize their
portfolio. When they make this selection from the Main Menu, they can observe the
optimization input form: “Minimize Risk” (see Figure CS17.13). This form prompts the
users for three optimization inputs: the total cash available to make the investment, the
minimum desired return on the portfolio, and the maximum amount they can invest per
stock. We employ text boxes and labels to clarify what input is needed.
Main Menu Form Gives the users three options: create new portfolio,
edit current portfolio, or optimize portfolio
investment plan.
Summary Compare Stocks Allows the user to select between change in
Spreadsheet market price plot and annual returns plot for
Control comparing stocks.
Minimize Risk Receives input that is used for the optimization
Input Form model.
Navigational Include: “Exit,” “View Model,” “Compare All
Buttons Stocks,” “Return to Create/Edit Portfolio,” and
“Return to Investment Report.”
Functional Buttons Include: “Add,” “Remove,” “Save Portfolio and
Return to Main Menu,” “Show Plot,” “Modify Input,”
and “Modify Portfolio.”
CS17.4 Procedures
We will now outline the procedures for this application, beginning with the initial sub
procedures and variable definitions (see Figure CS17.14). The Main procedure informs
the users that the Solver is needed. This prompt only occurs the first time the users
opens the application. Some Boolean variables are set and the Main Menu appears. If
the users select to create a new portfolio, the ClearPrevious procedure clears all
previous portfolio data. We do not clear this data if the users want to edit the previously
created portfolio.
Figure CS17.14 The variable declarations, the Main procedure, and the
ClearPrevious procedure.
Once the Main Menu appears and the users make a selection, the Main Menu code
performs the appropriate actions (see Figure CS17.15). If the users choose to create a
new portfolio, the ClearPrevious procedure is called and they are taken to the
“Create/Edit Portfolio” sheet. If the users choose to edit their portfolio, then they are
taken directly to the “Create/Edit Portfolio” sheet without clearing any data. If the users
select to optimize their portfolio investment plan, we first perform an error check to
ensure that a portfolio has already been created. If there is a portfolio, then we display
the optimization input form and call the optimization procedure.
If the users have already gone to the “Create/Edit Portfolio” sheet, they will have some
additional sorting features for the “All Stocks” list and the “Portfolio” list of stocks. They
can sort the list by the stock names or the stock categories. The sorting procedure is
placed in the SelectionChange event procedure of the worksheet (see Figure CS17.16).
Figure CS17.16 The sorting procedure for the “Create/Edit Portfolio” sheet.
On the “Create/Edit Portfolio” sheet, the users can click on the “Add” and “Remove”
buttons to create their portfolio. The “Add” button is associated with the AddToPortfolio
procedure (see Figure CS17.17 and CS17.18), which includes much error checking to
ensure that the button functionality works correctly. We must ensure that the users have
selected a name from the list of “All Stocks” to add to the portfolio. We then add the
selected stocks to the portfolio and format the new list. We must also check that no
stocks were repeated.
Figure CS17.19). We then remove the stocks from the “Portfolio” list and shift the other
stocks to reformat the list.
If the users go to the “Compare Stocks” sheet, they will also be given sorting features for
the “All Stocks” list. They can sort the list by the stock names or by the stock categories.
Again, we place the sorting procedure in the SelectionChange event procedure of the
worksheet (see Figure CS17.20).
Figure CS17.20 The sorting procedure for the “Compare Stocks” sheet.
Once the users make a selection from the combo box on the worksheet, they can press
the “Show Plot” button to update the chart. This button is assigned to the ShowPlot
procedure (see Figure CS17.21). Depending on which selection was made in the combo
box, a corresponding procedure is called.
If the users select “Annual Return”, then the application calls the PlotAnnualReturn
procedure (see Figure CS17.22). This procedure copies the selected stocks’ annual
return records from the “All Stocks” worksheet in a table on the “Chart Data” sheet.
There are already two charts created on the “Compare Stocks” sheet that have the
corresponding tables on the “Chart Data” sheet set as their respective source data.
Therefore, instead of refreshing the source data of a chart in the PlotAnnualReturn
procedure, we simply change the appropriate chart object’s position so it is in front of the
other chart.
If the users select “Change in Market Price” in the combo box, then the PlotChangePrice
procedure is called (see Figure CS17.23). This procedure performs the same error
checking and copying actions as the PlotAnnualReturn procedure, but for the quarterly
market price data instead of the annual returns. It also changes the appropriate chart
object’s position so it is in front of the other chart.
Once the users have finished creating the portfolio, they can click the “Save Portfolio
and Return to Main Menu” button, which is assigned to the SavePortfolio procedure (see
Figure CS17.24). This procedure simply copies all of the stock data from the “All Stocks”
sheet and pastes it on the “Portfolio” sheet for the stocks in the portfolio. It then calls a
navigational procedure to display the Main Menu again and return to the welcome sheet.
If the users now select to optimize the portfolio investment plan, the optimization input
form will appear. The code for this form performs some error checking and then assigns
the text box values to the corresponding variables (see Figure CS17.25). Note that the
percentages for the last two inputs are converted into decimal values.
The Optimize procedure is then called (see Figure CS17.26 and CS17.27). This
procedure clears the model sheet and inserts the updated variance and expected return
values along with the covariance factors. It also records the desired return and total cash
inputs. Once the changing cells and constraint cells are prepared, the Solver commands
can be used.
The Solver commands set the objective function, decision variables, and constraints and
then run the Solver. The SolverSolve command is set to an SResult variable, with which
we can communicate with the users if the problem is infeasible. This interaction allows
the users a chance to modify their input values or portfolio selection and then to resolve
the problem. The procedure then updates the output sheet and creates a table with the
decision variable results.
The navigational procedures are used for eight different buttons (see Figure CS17.28).
CS17.6 Summary
¾ The “Portfolio Management and Optimization” application allows users to create
and/or edit a portfolio as well as optimize the portfolio investment plan.
¾ This application consists of nine spreadsheets: the welcome sheet, four data
sheets, two information sheets, a model sheet, and an output sheet.
¾ There are two user forms, one control on a spreadsheet, and several
navigational and functional buttons.
¾ This application incorporates several procedures, which allow the users to create
and modify their portfolio and then view the optimized investment plan.
¾ The output sheet offers two resolve options: modify the optimization input or the
portfolio; and re-run the optimization.
CS17.7 Extensions
1. Solve the optimization by maximizing returns instead of minimizing risk.
2. Allow the users to place unique upper and lower bounds on the amount to invest
for each stock.
3. Allow the users to fix certain stock investments and optimize for the others.
4. Add more data to the “All Stocks” sheet and create more charts for the “Compare
Stocks” sheet.
5. What is another resolve option that can be added to the application? Implement
this new option.
Chapter Overview
CS18.2 Spreadsheets
CS18.3 User Interface
CS18.4 Procedures
CS18.5 Resolve Options
CS18.6 Summary
CS18.7 Extensions
Case Study 18: Reliability Analysis
In this application, we consider a series parallel system of machines in which there are
three machine types, each working in parallel and connected serially (see Figure
CS18.1).
A1 B1 C1
A2 B2 C2
A3 B3 C3
A4 C4
C5
Figure CS18.1 An example of a parallel serial system made up of three machine types A, B, and
C, each with a various number of machines.
To determine failure, we consider each machine type to have a k out of n system. Given
there are n machines in a machine type, any time k of those n machines fail, the entire
machine type fails. We then consider the entire system of all machine types to also be a
k out of n system. In this application, we assume n = 3 machine types and k = 1. That is,
any time one machine type fails, the entire system fails.
We use the Weibull distribution to estimate the failure times of the machines in each
machine type. Weibull random variables are the most common random variables used to
model failure times of machines (see Probability Models by Winston for more details).
The cumulative distribution function of the Weibull distribution is:
F(t) = 1 – e ^ ((-t / β) ^ α)
X = β * (ln (1 / 1-p) ^ α
where p is the probability that X is in the Weibull distribution and X is the Weibull random
variable. We use this function to generate random Weibull values for the simulation.
However, we first need to determine the parameters α and β. We optimize the Weibull
parameters for each machine type based on its given means and standard deviations of
time to failure. We simulate the system failure time using these Weibull parameters.
2
Case Study 18: Reliability Analysis
For this application, we analyze the distribution of the system failure time. We also try to
identify which machine type causes the most system failure. We wish to correct the
bottleneck machine type and improve the overall performance of the system. The output
sheet for this analysis includes a histogram of the system failure time with the mean
system failure time as well as a histogram of the frequency with which a particular
machine type causes a system failure.
CS18.1.2 Input
Using the model described in the above section, we can define the following input:
We use one user form to receive the first four input values from the user. We also
include one Input Box to record the number of runs to perform. We keep the input values
on a table in the simulation sheet to enable the user to modify them at any time.
CS18.1.3 Output
The output appears on the output sheet. There are also some important resolve options,
which we will discuss in a later section.
CS18.2 Spreadsheets
We use six spreadsheets in this application: the welcome sheet, a calculation sheet for
optimizing the Weibull parameters, a hidden calculation sheet for preparing the
simulation data, a simulation sheet, another calculation sheet for recording the results of
the simulation runs, and the output sheet. The welcome sheet provides a description of
the application and has a “Start” button assigned to the Main procedure (see Figure
CS18.2).
The first calculation sheet optimizes the Weibull parameters for each machine type (see
Figure CS18.3). The optimization is performed using the Solver; therefore, the sheet is
organized with ranges for the decision variables, the constraints, and the objective
function. The input cells for this optimization are the mean and standard deviation for
3
Case Study 18: Reliability Analysis
time to failure for each machine type. Each machine type’s Weibull parameters are
optimized one at a time.
The decision variables are the Weibull parameters alpha and beta. Both of these have
the upper and lower bounds shown adjacent to their cells. These bounds are the only
constraints. We then calculate a mean and standard deviation time to failure using alpha
and beta to find the square error compared to the user’s input for these values. The
objective function is therefore to minimize the sum of these square errors.
4
Case Study 18: Reliability Analysis
Figure CS18.3 The calculation sheet for optimizing the Weibull parameters.
There are two buttons on this sheet: “End” and “Back.” The “End” button returns the user
to the welcome sheet and the “Back” button returns to the output sheet once the
application is completed.
We then create a timeline of failure times with a hidden calculation sheet for each run of
the simulation (see Figure CS18.4). The time to failure for each machine on all machine
types is recorded. The machine number and the machine type are employed in the
animation.
Figure CS18.4 The hidden calculation sheet for the simulation data.
5
Case Study 18: Reliability Analysis
The next sheet that the user sees is the simulation sheet, which contains an input table
and the simulation animation layout (see Figure CS18.5). The input table summarizes all
of the user’s input values in an initial input form. It also reports the Weibull parameters
optimized in the previous sheet. The user can modify these values at any time and they
will be recorded before the simulation runs.
The animation layout reveals all of the machines for each machine type. As the
simulation runs, a failed machine changes to red in the animation. If a particular machine
type reaches its respective k number of failed machines, then a system failure occurs.
The word “Failed” appears above the machine type that caused the system failure. In
Figure CS18.4, machine type A has caused the system failure since six of its machines
have failed; as the input table reveals, six of the ten machines are required to cause a
failure for machine type A.
This sheet contains two buttons plus a hidden button. The “End” button brings the user
to the welcome sheet and the “Start Simulation” button calls the procedure that begins
the simulation. The hidden buttons is the “View Analysis” button, which is made visible
once the simulation is completed. It returns the user to the output sheet.
Figure CS18.5 The simulation sheet with the animation layout and input table.
The next sheet is another calculation sheet; it contains the results of each run of the
simulation (see Figure CS18.6). The time until the system failed is recorded along with
the machine type that caused the system failure. We will use the table for bin values
later when creating the histogram for the machine types.
6
Case Study 18: Reliability Analysis
Figure CS18.6 The third calculation sheet for the simulation runs’ results.
There are two buttons on this sheet: “End” and “Back.” They have the same
functionalities as the buttons on the first calculation sheet.
The final sheet, the output sheet (see Figure CS18.7), displays the mean system failure
time, a histogram of the system time failures from the simulation, and a histogram of the
frequencies with which each machine type caused the system failure. This is the most
important information for the user to analyze in order to determine how the system is
behaving.
7
Case Study 18: Reliability Analysis
(a)
(b)
8
Case Study 18: Reliability Analysis
This sheet includes several buttons. The “End” button returns the user to the welcome
sheet. The “View Details” button takes the user to the third calculation sheet in which the
results of the simulation runs are recorded. The “View Weibull” button takes the user to
the first calculation sheet in which the Weibull parameters were optimized. The last two
buttons, “Return to Simulation” and “Resolve,” are used with the resolve options which
we will discuss in a later section.
This form appears to the user three times in order to receive the input for each machine
type. We include a dynamic label at the top of the sheet that displays the name of the
current machine type with which the input is associated. Notice that we use a label and
not a text box for this value since the user should never modify its value.
9
Case Study 18: Reliability Analysis
Figure CS18.8 The input form with the dynamic label value for machine type “A.”
In the previous section, we described the input table on the simulation sheet . It simply
summarizes the user’s input from the user form as well as the optimized Weibull
parameters. The user can change this input before the simulation is run. The
navigational buttons and functional buttons were also discussed in the previous section
with each corresponding worksheet.
The Input Box prompts the user for the number of simulation runs (see Figure CS18.9).
A title is given to the Input Box as well as a default value of 25 runs.
Figure CS18.9 The Input Box prompts the user for the number of simulation runs to
perform.
The Message Box informs the user that the Weibull parameter optimization has been
completed (see Figure CS18.10). The corresponding calculation sheet appears behind
the Message Box. We take the user directly to the simulation sheet, not allowing him or
her to pause on the calculation sheet. However, the user can revisit the Weibull
parameter optimization calculations once the simulation is complete.
10
Case Study 18: Reliability Analysis
Figure CS18.10 The Message Box informs the user that the Weibull parameter
optimization is complete.
CS18.4 Procedures
Let’s now outline the procedures for this application. We begin with the Main sub
procedure and variable definitions (see Figure CS18.11). The public variables are
applied in multiple procedures. We define all other variables within the procedure that
they are used.
The Main procedure begins by calling the ClearPrev procedure, which initializes
variables and clears previous worksheet values (see Figure CS18.12). The formatting for
the animation layout also clears.
Next, the Main procedure uses a loop to show the user the input form so one form is
displayed for each machine type. The dynamic label is adjusted before the form appears
so it displays the name of each machine type. The form values are recorded into arrays
using the same index in the loop. The Click event procedure of the “OK” command
button on the input form performs some error checking and then assigns the input values
to the corresponding array variables (see Figure CS18.13).
The Input Box for the number of runs then appears. Some error checking is done to
check if the user pressed the “Cancel” button on the Input Box.
11
Case Study 18: Reliability Analysis
12
Case Study 18: Reliability Analysis
13
Case Study 18: Reliability Analysis
Once we return to the Main procedure, the Message Box appears to inform the user that
the optimal Weibull parameters have been found for each machine type. We then call
the PrepSim procedure, which rerecords the values from the input table on the
simulation sheet and then prepares the animation layout (see Figure CS18.15). The
input values are rerecorded so that at a later time, if the user changes the input values,
the simulation and animation will be updated. This feature becomes more relevant when
the PrepSim procedure is called from the main simulation procedure.
14
Case Study 18: Reliability Analysis
The Main procedure then takes the user to the simulation sheet. On the simulation
sheet, the “Start Simulation” button calls the StartSim procedure; this is the main
simulation procedure. The StartSim procedure initializes the animation, creates the
simulation data, and performs the simulation and animation (see Figure CS18.16). The
StartSim procedure begins by calling the PrepSim procedure, as explained above.
It then begins a loop for the number of runs provided by the user. The loop creates data,
performs the animation, and records the results of the simulation run. To create the
simulation data, we call the CreateData procedure; it generates failure times for each
machine of each machine type (see Figure CS18.17). These values are generated with
the WeibullInv function procedure (see Figure CS18.18), which uses the inverse function
provided in the model description to produce a random Weibull variable value with a
random probability value from the Rnd() function. The failure times are stored in the
hidden calculation sheet with their corresponding machine numbers and machine type
15
Case Study 18: Reliability Analysis
names. We then sort the values based on the failure time in order to form the timeline to
which we will refer when performing the animation.
The StartSim procedure can now begin the animation process. It reads through the time
values in the timeline just created and checks which machine number of which machine
type has just failed. It then changes the formatting of the respective cell on the animation
layout to reflect to the user that a machine has failed. It also checks whether or not
enough machines of a particular machine type have failed to cause system failure. If so,
then the word “Failed” appears over the appropriate machine type column in the
animation layout. The time of the system failure is recorded to the third calculation sheet,
along with the machine type that caused the failure.
The Application.Wait function pauses the animation after each system failure. We then
call the AnalysisPrep procedure.
(a)
16
Case Study 18: Reliability Analysis
(b)
Figure CS18.16 The StartSim procedure.
17
Case Study 18: Reliability Analysis
The AnalysisPrep procedure refers to the results recorded for each run on the third
calculation sheet to create the histograms for the system failure and machine type
failures (see Figure CS18.18). It also reports the mean system failure time.
The StartSim procedure then displays the “View Analysis” button on the simulation
sheet.
18
Case Study 18: Reliability Analysis
The only other procedures are the navigational procedures (see Figure CS18.19). These
are for the “End,” “Back,” “View Analysis,” “View Details,” “View Weibull,” and “Return to
Simulation” buttons.
19
Case Study 18: Reliability Analysis
20
Case Study 18: Reliability Analysis
For example, in Figure CS18.20, we have returned to the simulation sheet after the initial
simulation was run. We have changed the values for the “Number of Machines to Cause
Failure” in the input table from 6 for each machine type to 3, 6, and 9. We then press the
“Start Simulation” button again to restart the simulation. We can observe that the new
input has been recorded, as the first machine type has now caused the system failure
when 3 of its machines failed.
We can then view the output sheet again by pressing the “View Analysis” button; we now
can observe that indeed the first machine type has caused the majority of the system
failures (see Figure CS18.21).
21
Case Study 18: Reliability Analysis
The second resolve option enables the user to determine the system bottleneck machine
type and add one machine to this machine type to improve system performance, or, to
increase the mean system failure time. We perform this option with a Resolve procedure
that is called when the user presses the “Resolve” button on the output sheet (see
Figure CS18.22).
The Resolve procedure reveals a resolve form in which the user can choose which
machine type to add a machine to. However, before doing that, we want to suggest to
the user which machine type would be the best choice. To do so, the Resolve procedure
begins by performing some trials to determine the optimum choice. For each machine
type, one machine is added and the simulation is rerun. We then record the
improvement in the mean system failure time. After this process is done for all three
machine types, we compute a ratio of these improvements to each machine type’s cost.
The optimal machine type is the one with the highest ratio; in other words, it is most
beneficial to add a machine that will cause the most improvement in system failure time
at the least cost.
22
Case Study 18: Reliability Analysis
The resolve form then appears (see Figure CS18.23). When the form is initialized, we
display the improvements in system failure time, machine costs, and ratio values for
each machine on a table in the form (see Figure CS18.24). We also update a dynamic
label to display the best choice, which we suggest to the user, and select the
corresponding option button.
23
Case Study 18: Reliability Analysis
Figure CS18.24 The initialization event procedure for the resolve form.
24
Case Study 18: Reliability Analysis
A frame with these option buttons retrieves the user’s machine type selection. The “OK”
button records this selection and increases the number of machines of this machine type
by one (see Figure CS18.25). It then updates the input table and animation layout and
recalls the StartSim procedure.
Figure CS18.25 The Click event procedure for the “OK” button on the resolve form.
The user can then observe the new simulation and animation. For example, in Figure
CS18.26, we have added one machine to the first machine type. Notice that this
modification is reflected in the input table and the animation layout. The user can then
review the analysis to determine if the mean system time has indeed improved. This
process may be repeated as many times as the user desires.
25
Case Study 18: Reliability Analysis
CS18.6 Summary
¾ The reliability analysis problem estimates the system failure time of a system of
machines. A parallel serial system is considered; in other words, if one machine
type fails, the entire system fails. A k out of n system is considered for each
machine type. The Weibull distribution models the machine failure times for the
simulation.
¾ There are five spreadsheets for this application: the welcome sheet, the
calculation sheet for Weibull parameter optimization, the hidden calculation sheet
26
Case Study 18: Reliability Analysis
for simulation data, the simulation sheet, the calculation sheet for simulation
results, and the output sheet.
¾ The initial input user form requests input that is summarized in an input table on
the simulation sheet; the user can modify this input at any time.
¾ This application has several procedures that walk the user through the Weibull
parameter optimization, simulation, and analysis.
¾ The first resolve option allows the user to rerun the simulation after modifying the
input table on the simulation sheet. The second option enables the user to
improve the mean system failure time by adding one machine to a selected
machine type. The most favorable choice is suggested to the user.
CS18.7 Extensions
1. Add an option to the output sheet in which the user can determine the probability
that the system will survive past a specified system failure time.
2. Add an option that repeats the second resolve option until a desired system
failure time is reached.
3. Make the application more dynamic by allowing the user to analyze more than
three machine types.
4. What is a similar problem in which this application could be used with slight
modifications?
27
Case Study 19
Queuing Models
Chapter Overview
CS19.2 Spreadsheets
CS19.3 User Interface
CS19.4 Procedures
CS19.5 Resolve Options
The times at which each customer’s service begins and ends are then calculated. The
time at which a customer’s service begins is the maximum value between the arrival
time and the time at which the preceding customer’s service ends. The time at which a
customer’s service ends is simply the sum of the time at which the service begins and
the service time. The following formulas generate and store these values:
Once these values have been generated, they are listed and sorted on a hidden data
sheet to form a timeline, which steps through the simulation and animation of the
queuing system. (See Figure CS19.1.) For the animation, yellow cells represent
customers moving through the queue. Customers initially arrive to a queue line; if the
server is "Idle," then the customer moves to the server and begins service; if the server
is "Busy," then the customer stays in the queue until the server becomes "Idle" again.
(See Figure CS19.2.)
Figure CS19.1 The hidden data sheet with the simulation timeline.
When the simulation is complete, the user can begin a new problem or view a report.
This report contains histograms of customer waiting times and queue lengths along with
other statistics. For more details on queuing models, please see Introduction to
Operations Research by Winston.
CS19.1.2 Input
CS19.1.3 Output
CS19.2 Spreadsheets
This application requires four sheets: the welcome sheet, the simulation sheet, the report
summary sheet, and the report details sheet. (There is also a fifth hidden sheet, which
creates the timeline described in the previous section; see Figure CS19.1.) The welcome
sheet contains the title, the description of the application, and the “Start” button. (See
Figure CS19.3.) The “Start” button takes the user to the simulation sheet.
The simulation and animation are performed on the simulation sheet. (See Figure
CS19.4.) For the animation, we format cells to be yellow with customer numbers to
represent customers moving through the queue. The queue area is titled “Queue.”
Customers initially arrive to the “Queue.” If the server is "Idle," then the customer moves
to the server and begins service. If the server is "Busy," then the customer stays in the
queue until the server becomes "Idle" again. The “Time” is also updated as the
simulation is performed. A statement below the “Queue” describes to the user the
current action at the given time. For example, in Figure CS19.4 (a), a customer
(Customer 17) has just arrived to the queue; Customer 12 is currently being served.
When the simulation is complete, two new buttons appear to allow the user to view the
report or to solve a new problem. [See Figure CS19.4 (b).]
(a)
(b)
Figure CS19.4 The simulation sheet.
If the user presses the “View Report” button, then the report summary sheet appears.
(See Figure CS19.5.) This sheet summarizes the user’s input values and the maximum,
minimum, and average values of the waiting time per customer as well as the queue
length per unit time. There are also charts and histograms for the waiting times and
queue lengths. From this sheet, the user can press the “View Details” button to proceed
to the report details sheet.
The report details sheet provides the detailed list of waiting times per customer and
queue lengths per unit time that were recorded during the simulation. (See Figure
CS19.6.) Bin values used for the histograms are also stored here. The user can return to
the report summary sheet by pressing the “View Summary” button.
CS19.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and the initial sub procedures. (See Figure CS19.8.) The Main procedure is
called from the “Start” button. It calls the ClearPrevious procedure to clear the previous
values from all the sheets. The Main procedure then shows the user the simulation input
form and calls the CreateData and Simulation procedures.
Figure CS19.8 The variable declarations and the Main and ClearPrevious procedures.
The procedures for the simulation input form simply record the input value provided by
the user. (See Figure CS19.9.) Some error checking checks the values of the text boxes.
The CreateData procedure creates all of the data that performs the simulation. (See
Figure CS19.10.) Four arrays store this data: Arrival(), Service(), BeginServe(), and
EndServe(). We assume that the first customer arrives at time 0 and is immediately
served. We then use the mean inter-arrival and service time provided by the user to
generate the arrival times and service times for each customer. The time at which a
customer’s service begins is the maximum value between the arrival time and the time at
which the preceding customer’s service ends. The time at which a customer’s service
ends is simply the sum of the time at which the service begins and the service time. We
then print all of these values on the hidden data sheet and sort them to form a timeline
range.
The Simulation procedure performs the simulation and animation. (See Figure CS19.11.)
We step through the timeline created on the hidden data sheet, which has been sorted
by the cumulative time values. For each time value, we check the corresponding event
description: “Arrival,” “BeginServe,” or “EndServe.” Depending on the event, the
corresponding animation actions are taken. The queue length is also recorded for each
time period. When the simulation is complete (when all the customers have been
served), the application calls the CreateDetails and CreateSummary procedures.
The CreateDetails procedure records the detailed output (the waiting time per customer
and the queue length per unit time) on the report details sheet. (See Figure CS19.12.) It
then creates several ranges that are used in the CreateSummary procedure. It also
calculates the max, min, and average values for the waiting times and queue lengths.
The CreateSummary procedure prepares the report summary sheet. (See Figure
CS19.13.) It displays the user’s input values and the min, max, and average waiting time
and queue length values, which are calculated in the CreateDetails procedure.
CreateSummary then updates the chart source data for the waiting times and queue
lengths. It then creates the two histograms using calculated bin values and the ranges
named in the CreateDetails procedure.
“New Problem” button on Recalls the Main procedure and repeats the
the simulation sheet simulation.
Summary
This application also assumes exponentially distributed customer inter-arrival times and
customer service times. Each server is assumed to have equal service times. The user
provides the means of these density functions.
The simulation data is created similarly to the single server application. Arrays store the
generated arrival and service times with the user specified means. Arrays also store the
begin and end service times for each customer. However, now that there are multiple
servers, we must also store which server serves each customer. (Note that the number
of servers is a new input that the user must provide.) This server is determined by
keeping track of which is the first to finish servicing a preceding customer. The following
code generates all of these values:
'service, begin serve, end serve, and server IDs for each customer
For i = 1 To NCust
Earliest = 9999999
For j = 1 To NServ
If Earliest > NextAvailable(j) Then
Earliest = NextAvailable(j)
ServerNum = j
End If
Next j
BeginServe(i) = WorksheetFunction.Max(Arrival(i), Earliest)
Service(i) = -ServerMean * Log(Rnd())
EndServe(i) = BeginServe(i) + Service(i)
ServerID(i) = ServerNum
NextAvailable(ServerNum) = EndServe(i)
Next i
The animation for this simulation is again similar to what was done in the single server
application. The created timeline is examined for each time value, and the corresponding
event is animated.
For the optimization option, the user provides some new input: the customer waiting cost
per time unit, the server cost per time unit, and trial values for the optimal number of
servers. The user defines an interval for the trial number of servers by providing a
minimum, maximum, and step size value. The following loop code then performs the
simulation part of the application for each trial number of servers:
Call CreateData
Call Simulation
Call CreateDetails
Call CreateSummary
SummaryCount = SummaryCount + 1
Next SIter
The total costs are calculated, and the optimal number of servers is considered to be the
scenario with the minimum total costs. The application calculates these costs as follows:
Total customer waiting cost = customer waiting cost per time unit * sum of waiting
times over all customers
Total server cost = server cost per time unit * number of servers * sum of service
times over all servers
• Detailed output for service time per server (for the simulation option)
• Summary of waiting times, queue lengths, and costs per trial server
number (for the optimization option)
• Optimal number of servers and minimum total cost (for the optimization
option)
The simulation sheet is now updated to show multiple servers for the animation. (See
Figure CS19.16.) The report details sheet also now displays the recorded service time
for each server. (See Figure CS19.17.)
If the user chooses the simulation option, then the simulation input form appears. [See
Figure CS19.19 (a).]. This form has a new text box to ask the user for the number of
servers in the queuing system. Also, two frames now group the server and the customer
input. If the user chooses the optimization option on the main menu form, then the
simulation input form appears. [See Figure CS19.19 (b).] However, the user does not
specify the number of servers for this option, since a trial number of servers is used.
(a) (b)
Figure CS19.19 The updated simulation input form.
The optimization option has a new input form: the optimization input form. (See Figure
CS19.20.) This form prompts the user for the max, min, and step size for the trial
number of servers, the customer waiting cost per time unit, and the server cost per time
unit. This form requires two frames and several text boxes.
Main menu form Prompts the user to perform the simulation or the
optimization option.
Updated simulation Now also asks the user for the number of servers.
Summary input form
Optimization input Prompts the user for the max, min, and step size
form for the trial number of servers, the customer
waiting cost per time unit, and the server cost per
time unit.
The two new user forms also now have new procedures. The procedures for the main
menu form take the user to the sheet that corresponds to the selected option, and they
display the simulation input form. (See Figure CS19.22.) The procedures for the
optimization input form record the user’s input and perform some error checking. (See
Figure CS19.23.)
Figure CS19.22 The new procedures for the main menu form.
Figure CS19.23 The new procedures for the optimization input form.
The CreateData procedure has been modified to create additional data for which server
services each customer. (See Figure CS19.24.) Two new arrays are used:
NextAvailable() and ServerID(). The timeline is again created, but this time the
corresponding server ID is also recorded. The animation employs this server ID to bring
the customer to the correct server.
The Simulation procedure is also updated to include the optimizaiton option. (See Figure
CS19.25.) If the optimization option is being performed, then no animation is performed,
and only the queue length is recorded for each time unit.
The CreateSummary procedure is updated for the optimziation option as well. (See
Figure CS19.26.) This new code records the summary waiting time, the queue length,
and the cost values for the trial number of servers. Note that the service time per server
is also added to the detailed output; this change is made to the CreateDetails procedure.
Main menu The user can first choose the optimization option,
form determine the optimal number of servers, and then
return to the main menu form to choose the simulation
Summary option. This optimal number of servers can now be
inputted into the simulation option.
CS19.11 Summary
¾ The single server queuing application simulates a queue of customers serviced
by a single server. The multi server queuing application simulates a queue of
customers service by multiple servers and also contains an optimization option to
determine the optimal number of servers.
¾ This application requires four spreadsheets: the welcome sheet, the simulation
sheet, the report summary sheet, and the report details sheet.
¾ For this application’s user interface, we use navigational buttons and user forms.
For the single server application, there is one user form; for the multiple server
application, there are three user forms.
¾ Several procedures for this application perform the simulation and/or
optimization. The procedures generate the arrival and service times and create a
timeline of events to perform the simulation and animation.
¾ The user can resolve this application by pressing the “New Problem” button on
the simulation sheet. For the multiple server application, the user can also use
the results of the optimization option to return to the simulation option and input
the optimal number of servers.
CS19.12 Extensions
1. Allow the user to choose between a “fast” and “slow” simulation speed. Add this
option to the simulation input form.
2. Improve the “New Problem” resolve option by updating the simulation input form
with the user’s last values instead of the default values.
3. Allow the user to change the distribution for the inter-arrival and server times.
4. Perform the simulation over a period of time instead of for a certain number of
customers. (For example, run a one-day or one-week scenario.)
5. For the multi server application, create a histogram that can compare a server’s
performance. The user should be able to enter different mean server times for
each server and then compare the server times according to the new histogram.
6. For the multi server application, allow the user to consider a cost for lost
customers. Customers are assumed to be lost or to have left the queue if a given
amount of waiting time occurs while the customer is in the queue. Count the
number of lost customers and include the cost in the optimization analysis.
Chapter Overview
CS20.2 Spreadsheets
CS20.3 User Interface
CS20.4 Procedures
CS20.5 Resolve Options
CS20.6 Summary
CS20.7 Extensions
Case Study 20: Facility Layout 2
In this problem, we are given n departments, numbered 1, 2, ..., n, and n facilities, also
numbered 1, 2, .. , n. Distances between locations are given by an n x n distance matrix
{dij}. The interaction between departments is given by an n x n flow matrix {fij}, which may
represent the number of trips made between the departments per unit time. The
objective in this problem is to assign each facility i to a location s[i] so that the total
distance traveled, given by ∑i∑j fijd(s[i]s[j]), is minimum.
For simplicity, we assume: (i) the locations are arranged in a rectangular grid; (ii) a
facility can be assigned to any location; (iii) the distances between locations are
rectilinear distances; and (iv) the flow matrix is randomly generated. The user can,
however, overwrite the distance and flow matrices.
We also allow the user to change the default numbering assigned to the facilities. In
other words, he or she can begin with a specified layout to determine what
improvements are possible from that starting point. We also allow the user to fix some
facilities.
We include a local search method to solve the facility location problem. We start with a
feasible assignment of facilities to locations and improve it repeatedly by performing
pair-wise exchanges between facilities.
For more information on this facility location problem, please see Facility Layout and
Location by Francis and White.
CS20.1.2 Input
• Number of facilities
• Renumbering of the facilities if the user chooses this option
• Which facilities are fixed if the user chooses this option
CS20.1.3 Output
CS20.2 Spreadsheets
This application requires four spreadsheets: the welcome sheet, the layout sheet, the
data sheet, and the report sheet. The welcome sheet contains the title, the description of
the application, and the “Start” button. (See Figure CS20.1.) The “Start” button displays
to the user an input form and then takes him or her to the layout sheet.
The layout sheet contains the initial layout, created using the facility size provided by the
user. (See Figure CS20.2.) It has a detailed set of instructions to explain all of the user’s
options, which correspond to the buttons on the screen. The user can press the “Change
Layout” button to change the default numbering of the initial layout. This option updates
the layout sheet with a new set of instructions and buttons. (See Figure CS20.3.)
Figure CS20.3 The layout sheet for changing the layout option.
The user could also press the “Fix Facilities” button to amend some facilities before
solving the problem. This option also updates the layout sheet with new instructions and
buttons. (See Figure CS20.4.)
Figure CS20.4 The layout sheet for fixing the facilities option.
The user can also press the “View/Edit Data” button to proceed to the data sheet. The
data sheet contains the distance matrix calculated from the facility, the flow matrix with
the initial random values, and the cost matrix calculated from the distance and flow
matrices. (See Figure CS20.5.) The user can modify the flow matrix if desired and return
to the layout sheet.
Figure CS20.5 The data sheet with the distance, flow, and cost matrices.
The user can then press the “Show Each Iteration” or “Show Final Solution” button to
solve the problem. The “Show Each Iteration” button pauses each iteration by displaying
a form that the user can refer to in order to decide which facilities to switch; the updated
facility for each iteration is then shown. (See Figure CS20.6.)
When the local optimal solution has been found, the layout sheet is updated with new
buttons that allow the user to go to the report sheet. (See Figure CS20.7.) The “Show
Final Solution” button simply generates all the iterations and updates the layout sheet in
the same manner.
Figure CS20.7 The layout sheet after the local optimum has been determined.
The report sheet presents the final layout, a table, and a chart of the change in cost for
each iteration. (See Figure CS20.8.) The user can review the data or iterations by
clicking on the “View Data” or “View Iterations” buttons, respectively.
This form prompts the user for the size of the facility. (See Figure CS20.9.) This size is
represented by the number of rows and columns, such that one row by one column
represents one facility. We assume that the number of rows and columns is less than ten
each.
The layout sheet contains several navigational and functional buttons. The navigational
buttons include “End,” “Change Facilities,” “Fix Facilities,” and “View/Edit Data.” The
“View Iterations” button on the data sheet is another navigational button that brings the
user back to the layout sheet. The functional buttons on the layout sheet are “Show
Each Iteration” and “Show Final Solution.” On the updated layout sheet for the change
facilities and fix facilities options, the “Save All” button is a functional button that records
any changes made to the facility. After the solution is found, the “View Report” button
appears as another navigational button to take the user to the report sheet.
If the user views each iteration as the problem is solved, then he or she uses the
iteration form. (See Figure CS20.10.) This form lists the ten facility pairs with the largest
change in cost from switching. The user can select any of these facility pairs and press
the “Switch” button to perform the switch in the local search method. The default facilities
are the first pair listed that has the largest change in cost.
On the report sheet, the “View Data” and “View Iterations” buttons serve as two more
navigational buttons. The “Resolve” button is a functional button, which we discuss later.
CS20.4 Procedures
We will now outline the procedures for this application beginning with the initial sub
procedures and the variable definitions. (See Figure CS20.11.) The Main procedure is
called from the “Start” button. It calls the ClearPrevious procedure and initializes the
formatting of the layout sheet. It then displays the input form and calls the CreateLayout
procedure to create the initial layout using the facility size provided by the user. It then
calls the CreateData and CreateDataMatrices procedures to generate the data for this
layout.
The ClearPrevious procedure clears all the previous data values and the formatting for
the all sheets. (See Figure CS20.12.) It also initializes some variables. The procedures
for the input form receive the number of rows and columns provided by the user and,
with these values, calculate the total number of facilities. (See Figure CS20.13.)
The CreateLayout procedure formats each row and column of the facility according to
the size provided by the user. (See Figure CS20.14.) It also numbers each facility
sequentially as it is formatted. Then, it initializes an array for storing fix facilities so that
no facilities are fixed initially.
The CreateData procedure creates the data for the distance, flow, and cost matrices.
(See Figure CS20.15.) The application first calculates each facility’s x- and y-
coordinates, which are used to find the Euclidian distance between each pair of facilities.
The flow matrix values are generated randomly, and the cost matrix values are simply
the product of the distance and flow for each facility pair.
The CreateDataMatrices procedure places the matrix values in matrix form on the data
sheet. (See Figure CS20.16.) Each matrix is formatted as an upper-triangle matrix.
The ChangeLayout and FixFac procedures simply update the layout sheet with new
instructions and buttons for the change facilities and fix facilities options. (See Figure
CS20.17.) The SaveAll procedure records any changes made by the user for either of
these options. (See Figure CS20.17.) If the user has changed the facility numbering,
then the CreateData and CreateDataMatrices procedures must be called again to
update the distance matrix. If the user has fixed some facilities, then these are recorded
in an array and formatted on the layout. The layout sheet is then updated to display the
initial instructions and buttons again.
The ShowFinal and ShowEach procedures set a Boolean variable and then call the
LocalSearch procedure. (See Figure CS20.18.) The LocalSearch procedure is the main
procedure of the application, as it performs the local search method to find the problem
solution. (See Figures CS20.18 and CS20.19.)
Figure CS20.18 The ShowFinal and ShowEach procedures and the first part of the
LocalSearch procedure.
LocalSearch calculates what the change in cost is if two facilities are switched. Notice
that it does not perform this evaluation for any pair that includes a fixed facility. All pairs
that cause a negative change in cost, that is a decrease in the cost, are listed and
sorted. The top ten pairs from this list are displayed on the iteration form. If the user opts
to just show the final solution, then the top item from this list, the pair with the largest
decrease in cost, is selected. The Update procedure then updates the distance and cost
matrices, and then the LocalSearch procedure is recalled. If there are no facilities that
cause a decrease in the cost, then a local optimum has been reached and the
CreateReport procedure is called.
The procedures for the iteration form record which pair of facilities the user selects to
switch. (See Figure CS20.20.) It then calls the Update and LocalSearch procedures.
The Update procedure updates the distance and cost matrices and displays the new
layout. (See Figures CS20.21 and CS20.22.) The x- and y-coordinates are updated, and
the facilities are renumbered for the switched facilities. The switched facilities and any
fixed facilities are also highlighted. The application displays the updated cost and the
iteration number on the layout sheet and records them on the report sheet.
Figure CS20.22 The second part of the Update procedure and the entire
CreateReport procedure.
The CreateReport procedure displays the final solution on the report sheet. (See Figure
CS20.22.) It updates the final matrices by calling the CreateDataMatrices procedure. It
then copies and pastes the final layout to the report sheet and highlights the fixed
facilities.
Figure CS20.23 presents the navigational procedures for the “View Data,” “View Report,”
“View Iterations,” “End,” and “Resolve” buttons, as well as the procedures for showing
and hiding the buttons on the layout sheet.
CS20.6 Summary
¾ This application solves the facility location problem, which consists of placing n
facilities at n locations to minimize the total handling cost. A local search method
solves it.
¾ This application requires four spreadsheets: the welcome sheet, the layout sheet,
the data sheet, and the report sheet.
¾ For this application’s user interface, we use navigational and functional buttons
as well as two user forms.
¾ Several procedures in this application initialize and perform local search methods
by comparing changes in cost for the pair-wise switching of the facilities.
¾ The user can resolve the application by pressing the “Resolve” button on the
report sheet to return to the layout sheet in order to modify the input and resolve
the problem for the same facility.
CS20.7 Extensions
1. Allow the user to stop the local search method before a local optimum is
reached. In other words, the user should be able to stop the local search after
any iteration.
2. How could you modify this application to allow the user to provide text names to
the facilities? Instead of Facility 1, 2, etc, let the user name them “Jason’s Office,”
“Marjorie’s Office,” or some other descriptive names.
3. What are some other resolve options possible for this application?
4. Could other methods solve this problem? Try implementing one and begin the
application by prompting the user to select which method he or she wants to use
(for example, local search or genetic algorithms).
Topics Overview
CS21.2 Spreadsheets
CS21.3 User Interface
CS21.4 Procedures
CS21.5 Resolve Options
CS21.6 Summary
CS21.7 Extensions
Case Study 21: Warehouse Layout
For simplicity, we discretize the warehouse floor area into A unit grids, called bays; here,
A represents the total warehouse area. We are seeking to allocate these bays to various
product types. We will now describe this problem in greater detail. However, we will
present some notations first for the three entities in the problem: the warehouse bays,
the docks, and the product types.
Docks: The warehouse has n docks indexed by j = 1, 2, … , n. Products are brought into
the warehouse and travel out of the warehouse by way of these docks. We represent the
distance between each bay and each dock with dkj.
Product types: The warehouse layout problem has m product types indexed by i = 1, 2,
…, m. A product i requires an area of Ai bays in the warehouse. We assume that the
warehouse has enough floor area to store all of the product types:
∑i Ai ≤ A summed over i = 1 to m
In Figure CS21.1 we construct an example with five product types and two docks.
Dock 1
5 4
2 3
1 Dock 2
Figure CS21.1 Five different product types with different areas must be assigned to the
warehouse area. Two docks are adjacent to the warehouse area.
Each product type i has a demand Di. We assume that the demand for each product
type i is uniformly divided over the entire storage area that the product type requires.
Since product type i is stored on Ai bays, the total demand of product i per storage bay is
Di / Ai.
Each product type also interacts with the docks at a different frequency. The frequency
is the fraction of the demand that travels in or out of the warehouse through a particular
2
Case Study 21: Warehouse Layout
dock. We define these frequencies as Fij. The sum of the frequencies over all docks for
each product type should be 1:
We are seeking to assign product types to the warehouse area such that those with the
highest demand per storage bay are situated closest to the docks with which they have
the highest frequency. To enforce this policy during assignment, we define a weight Wij
to represent the amount of a particular product type i per storage bay that travels to and
from a particular dock j. These weights are defined as follows:
Wij = Fij * Di / Ai
Therefore, if product type i is stored at bay k, then the total material handling cost with
dock j due to this storage is dki * Wij. We define the assignment decision variables xki as
binary variables to represent whether or not product type i is assigned to bay k.
The warehouse layout problem can now be formulated as the following integer
programming (IP) problem:
Subject to:
This objective seeks to minimize the material handling cost between the assigned
storage areas and the warehouse docks over all product types. The constraint (1a)
states that only Ai bays are assigned for each product type i. Constraint (1b) states that
only one product type can be assigned per bay.
This formulation is an IP problem and can be solved with an IP algorithm. (For more
details, please see "Discrete Location and Layout Problems" by Francis.) In this
application, however, we will consider a special case of the objective function (1) that
can be solved very efficiently using a greedy method.
Recall that Wij represents the amount of a particular product type i per storage bay that
travels to and from a particular dock j. We assume that the m by n matrix W = {Wij}
factors; that is, there exist numbers αi and βj, such that:
We may point out that not every matrix W factors; only some do. When a matrix W
factors, we can provide an intuitive explanation to the factors αi and βj. Let αi denote the
3
Case Study 21: Warehouse Layout
total demand of product type i per storage bay over all docks, and let beta denote the
frequency with which each dock is used.
αi = Di / Ai
βj = Fj
We now have:
Wij = αi * βj = Di / Ai * Fj
The factoring assumption implies that each product type has the same dock frequency
for a dock j as any other product type. This frequency, formerly noted as Fij, is now noted
as Fj or βj. For example, if there are two docks with frequencies β1 = 0.3 and β2 = 0.6,
then the factoring assumption implies that each product type sends 30% of its demand
per storage area through dock 1 and 60% of it through dock 2.
In this case, when the matrix W factors, we can restate the objective function of the
warehouse layout problem as follows:
We can now observe that since αi denotes the total demand of product type i per storage
bay over all docks, the greater the value of αi for any product type, the greater is the
interaction this product will have with the docks. Therefore, we want to ensure that these
product types with the highest αi values are assigned to the bays with the minimum
distance from the docks.
We also observe that for any bay k, ∑j dkj * βj denotes the average distance traveled per
unit demand for any product type stored at that bay; in other words, with probability βj,
the assigned product type will travel to dock j incurring the distance dkj. We now define
the value γk to denote the weighted distances between each bay k and each dock j as
follows:
It should now be intuitively clear that to minimize the total material handling cost, product
types with high priority weight values should be assigned to bays with small distance
weights. This intuition suggests the following greedy algorithm for the warehouse layout
problem:
This algorithm ensures that the product types with the highest weight are assigned to the
bays with the smallest distance weights. Therefore, the overall distance traveled by each
product type to each dock should be minimized.
4
Case Study 21: Warehouse Layout
Let’s now illustrate this algorithm with a small numerical example. Consider a warehouse
of area A = 100 and two docks (see Figure CS21.2).
Dock 1
Dock 2
Figure CS21.2 The warehouse area is discretized into bay areas of value 1.
We assume that there are five product types. The product type area requirements and
demands are:
Product
Area: Ai Demand: Di
Type: i
1 10 300
2 15 250
3 20 400
4 25 500
5 30 450
Assuming that dock frequencies are the same for all product types, we use the following
frequency values for the two docks:
F1 = 35%
F2 = 65%
With these values, we can now calculate the αi values for each product type i and the γk
values for each bay k. If we sort the product types in descending order of their αi values
and the bays in ascending order of their γk values, applying the greedy algorithm yields
the following bay assignments:
5
Case Study 21: Warehouse Layout
Notice that the total number of bays assigned to each product type is equal to the
product type areas Ai. See Figure CS21.3 for the final layout for this example.
Dock 1
2
4
1
3
Dock 2
Figure CS21.3 The final warehouse layout for five products and two docks.
Algorithm
1. Sort the bays in ascending order of their weighted distances.
2. Sort the product types in descending order of their weights.
Summary
3. Assign the highest weighted product type to the first bays from the
sorted weighted distance list until the product type area requirement is
satisfied.
CS21.1.2 Input
Using the model described in the above section, we can define the following input:
6
Case Study 21: Warehouse Layout
We consider the area of the warehouse as the total number of bays available for
assignment. The area required per product type is basically the number of bays to
assign per product type.
We use one user form and two input sheets in this application to receive these input
values from the users. We do not place any bounds on these inputs. We do provide
default values for a warehouse area of 30: 3 product types and 2 docks. However, the
users may change these values.
CS21.1.3 Output
Our main output is the warehouse layout. We present this output to the users using
different color cells to represent the different product types. The product type color
representation is summarized in a legend next to the warehouse grid. The docks are
also displayed in their specified locations adjacent to the grid. We include several
navigational buttons as well as some resolve options, which we will discuss in more
detail later.
CS21.2 Spreadsheets
We use four spreadsheets in this application: a welcome sheet, two input sheets, and an
output sheet. In the welcome sheet, we describe the warehouse layout problem and
provide an overview of what the users will input and what output will be displayed (see
Figure CS21.4). We also include some images of a warehouse and a “Start” button. The
“Start” button is assigned to our Main sub procedure, which we will discuss later.
7
Case Study 21: Warehouse Layout
The next spreadsheet that the users see is the first of two input sheets. Before the users
arrive to this sheet, they will complete a user form with the area of the warehouse, the
number of product types, and the number of docks. (We will discuss the form in more
detail in the next section). From this information, we create a grid representing the
warehouse area on the first input sheet. The first input sheet allows the users to place
the docks adjacent to the warehouse grid. There are some event procedures associated
with the spreadsheet that allow the users to simply click on the location of a dock to
reveal the dock number and formatting. We will discuss the details of these procedures
later.
We also create a table, which the users must complete, for the dock information. Based
on the placement of the docks adjacent to the warehouse grid, the users must enter the
number of the bay that each dock is adjacent to. This information will help us determine
the distance from each bay to each dock. The users must also enter the dock
frequencies at this point. The frequencies should sum to 100%. In Figure CS21.5, we
present an example with two docks located near bays 1 and 19 with frequencies 20%
and 80%, respectively.
The users then press the “Continue” button to navigate to the next input sheet. The
second input sheet is for information about the product types (see Figure CS21.6). We
create a table in which the users enter the area requirement (number of bays) and the
demand for each product type. The sum of the number of bays required over all product
types must be less than or equal to the available number of bays in the warehouse.
In Figure CS21.6, we continue the example shown in Figure CS21.5. In this case, we
have three product types with area requirements of 2, 4, and 7 bays, respectively. The
product type demands are 300, 500, and 600, respectively. We may note here that the
weights for each product type are 150, 125, and 85.7, respectively (αi = Di / Ai).
Therefore, product type 1 has the highest priority, then product type 2, then product type
3.
8
Case Study 21: Warehouse Layout
The users then press the “Solve” button to run the main calculation procedures and
perform the greedy algorithm. The output sheet then appears (see Figure CS21.7). The
output sheet reveals the final layout for all of the product types in the warehouse area.
We color each product type differently and provide a legend for this representation. In
Figure CS21.7, we can observe that product type 1 (which had the highest priority) was
assigned to the bays closest to the second dock (which had the highest frequency).
Figure CS21.7 The output sheet with its navigational buttons and resolve options.
There are some resolve options and navigational buttons here as well. We will discuss
these in more detail in a later section.
9
Case Study 21: Warehouse Layout
We employ two frames to group similar text boxes together. The first frame contains the
text boxes for the number of product types and number of docks. The second frame
contains the warehouse dimensions values: the number of rows and the number of
columns. We have entered default values for three product types, two docks, and a
warehouse area of 30 bays. The “Cancel” button simply unloads the form, leaving the
users at the welcome sheet. The “OK” button performs some error checking and assigns
the input values to corresponding variables. It then takes the users to the first input
sheet.
Figure CS21.8 The user form asks for the first input values.
The first input sheet, discussed in the previous section, has two buttons: “End” and
“Continue” (see Figure CS21.5). The “End” button closes the sheet and returns the users
to the welcome sheet. The “Continue” button performs some error checking, records the
dock information that the users have entered in the table, closes the sheet, and takes
the users to the next input sheet.
On the second input sheet, also discussed in the previous section, there are two buttons:
“End” and “Solve” (see Figure CS21.6). The “End” button has the same functionality as
in the previous sheet. The “Solve” button performs some error checking, records the
10
Case Study 21: Warehouse Layout
product type information entered by the users in the table, and then calls the main
procedures to perform the greedy algorithm. It then closes the sheet and takes the users
to the output sheet.
The output sheet has a few more buttons (see Figure CS21.7). The “End” button is again
the same as the others. There are then two navigational buttons, which allow the users
to revisit the input sheets: “View Product Info” and “View Dock Info.” These buttons
simply close the output sheet and take the users to the respective input sheet. When the
users revisit an input sheet, a new button appears to “Go Back” to the output sheet. This
“Go Back” button replaces the “Continue” and “Solve” buttons.
There is also a “Resolve” button on the output sheet. This button is used to record any
changes that the users have made to either of the input sheets or the “Resolve Layout”
and then resolves the problem. We will discuss the meaning of the “Resolve Layout” and
other resolve functions in a later section.
CS21.4 Procedures
We will now outline the procedures for this application. We begin with the Main sub
procedure and the variable definitions (see Figure CS21.8). We define several variables
as public variables since they will be used in multiple procedures. We have variables to
represent the problem dimensions, such as the number of product types and the number
of docks. We also include some counting variables for loops, several arrays for the
preparation calculations, and several variables to be used in the algorithm.
The Main procedure begins by calling the ClearPrevious procedure, which clears the cell
values and all the sheets’ formatting in the application (see Figure CS21.9). It also
ensures that the original buttons on the input sheets are visible and hides the “Go Back”
buttons. Lastly, it initializes some variables.
11
Case Study 21: Warehouse Layout
The Main procedure then displays the input form (see Figure CS21.8). The main code
associated with this form is in the Click event procedure of the “OK” button (see Figure
CS21.10). This procedure performs some error checking to ensure that all of the input
12
Case Study 21: Warehouse Layout
values have been provided. It then assigns the input values to their corresponding
variables. At this point, we know the number of product types, the number of docks, and
the area of the warehouse. We refer to the area of the warehouse by the number of
bays, which is equal to the number of rows multiplied by the number of columns, as
provided by the users.
Figure CS21.10 The ClearPrevious procedure clears the values and the formatting
on all sheets; it also initializes some variables.
13
Case Study 21: Warehouse Layout
Figure CS21.11 The cmdOK_Click procedure assigns the input values to their
corresponding variables.
The Main procedure continues by resetting some arrays used for the resolve options and
then assigning colors to the product types. Then, we call the NumberingBay procedure,
which numbers the warehouse grid and computes the X and Y coordinates of each bay
(see Figure CS21.12).
The procedure begins by creating the initial warehouse layout. We move from row 1 to
the number of rows and from column 1 to the number of columns and then back to
column 1 and so forth, labeling each bay in the warehouse area. We set the X and Y
coordinates equal to the row and column values of each created bay. These coordinates
are stored in two arrays.
14
Case Study 21: Warehouse Layout
The NumberingBay procedure continues by preparing the input tables for the docks and
the product types based on the input provided by the users in the input form. The dock
table is created on the first input sheet while the product type table is created on the
second input sheet. The total number of bay areas is also recorded on the second input
sheet. Returning to the Main procedure, we now simply take the users to the first input
sheet.
(a)
15
Case Study 21: Warehouse Layout
(b)
Figure CS21.12 The NumberingBay procedure defines the initial warehouse
layout. (a) The beginning of the procedure. (b) The procedure continued.
On this sheet, the users are able to place the docks around the warehouse area simply
by clicking on a cell. To enable this feature, we have written an event procedure for the
SelectionChange event of the worksheet (see Figure CS21.13). We first check if the
active cell is in a range where the docks are allowed to be placed. This range can be
defined as the intersection of the warehouse area with an additional one-unit
circumference and all other cells. We define this intersection with a logical check and
two Union worksheet functions.
Once we ensure that the users have clicked a cell in the allowable dock location area,
we check that they have not already placed all of the docks. If this condition is false, then
we label the cell with “D” and the current dock number.
16
Case Study 21: Warehouse Layout
Figure CS21.13 The SelectionChange event procedure enables the users to click on
the sheet to place the docks.
We assign the next procedure, DockInfo, (see Figure CS21.14) to the “Continue” button
on the first input sheet. The procedure begins with two error checks to ensure that the
dock table has been completely filled and that the dock frequencies sum to zero. We
then determine the X and Y coordinates for each dock.
Knowing that the docks are placed adjacent to the warehouse area and given the bay
numbers to which each dock is adjacent, we can find the dock coordinates by
determining which border the adjacent bay is on. If the adjacent bay is on the top or
bottom border of the warehouse area (that is, on the first or last row), then the X
coordinate is zero or one more than the number of rows, respectively. The Y coordinate
is the same as the Y coordinate of the adjacent bay. If the adjacent bay is on the left or
right border of the warehouse area (that is, on the first or last column), then the Y
coordinate is zero or one more than the number of columns, respectively. The X
coordinate is the same as that of the adjacent bay.
We also record the dock frequencies into an array. Finally, we take the users to the
second input sheet.
17
Case Study 21: Warehouse Layout
18
Case Study 21: Warehouse Layout
The FinalSteps procedure performs the main calculations and calls the procedures that
execute the algorithm (see Figure CS21.15). This procedure is assigned to the “Solve”
button on the second input sheet. The procedure begins with two error checks to ensure
that the product type table has been completely filled and that the sum of the required
bays is less than or equal to the total number of bays in the warehouse.
Figure CS21.15 The FinalSteps procedure performs the main calculations and calls
the procedures that execute the algorithm.
19
Case Study 21: Warehouse Layout
Next, we call the ComputeF procedure (see Figure CS21.16). It computes the weighted
distances with the recorded dock frequencies. These distance weights are equivalent to
the γk values described in the model. We define γk = ∑j dkj * Fj and so compute
these values using a loop over each bay with their respective X and Y coordinates and a
sub loop over each dock with their respective frequency values and X and Y
coordinates.
Figure CS21.16 The ComputeF procedure computes the weighted distances based
on the dock frequencies.
The next procedure called from the FinalSteps procedure is SortItems (see Figure
CS21.17). The first step of the algorithm, this procedure calculates the product type
weights and use these values to sort the product types in a descending order of
importance. The product type weights are equivalent to the αi values described in the
model. We define αi = Di / Ai and so compute these values using the recorded demand
and area values; the weights are stored in a Ratio array.
We then sort the product types according to these Ratio values. We perform the sort on
an array called ItemOrder and initialize this array such that each product type i has an
ItemOrder value i. We then search for the largest Ratio value and move this product type
to the front of the list; in other words, we exchange its ItemOrder value with the product
type that has an ItemOrder value equal to 1. We continue this process but examine one
less value each time. Eventually, the ItemOrder array signifies the sorted order of the
product types.
20
Case Study 21: Warehouse Layout
Figure CS21.17 The SortItems procedure calculates the product type weights and
sorts them.
The next procedure that we call is the SortBays procedure (see Figure CS21.18); this is
the second step of the algorithm. SortBays sorts the bays in descending order of their
distance weights that were computed in the ComputeF procedure. The sorting is done in
the same manner in which the product types were sorted. This time, however, we search
for the minimum weight value in each pass. We use an array called BayOrder to store
the bay order.
21
Case Study 21: Warehouse Layout
Figure CS21.18 The SortBays algorithm sorts the bays in ascending order of their
distance weights.
The final procedure called from the FinalSteps procedure is the Assign procedure (see
Figure CS21.19). The last step of the algorithm, this procedure completes the
assignment of bays to each product type and creates the final layout.
The procedure begins with the final layout’s formatting. There is then a small section of
code that is related to the resolve options; we will discuss this in more detail in the next
section. The assignment loop then begins. We loop through the sorted list of product
types in descending order of their weight values found in the SortItems procedure. We
then assign bays to each product by looping through the list of sorted bays until the area
requirement for the product type has been met. To reflect that an assignment has been
made, we format the assigned bay with the color of the product type. When the
assignment is finished for the product type, we also update our legend with the product
type’s color and index.
22
Case Study 21: Warehouse Layout
(a)
23
Case Study 21: Warehouse Layout
(b)
Figure CS21.19 The Assign array completes the last step of the assignment
algorithm. (a) The first part of the procedure. (b) The last part of the procedure.
The last part of this procedure simply formats the resulting layout and creates a “Resolve
Layout” which we will discuss in the next section. We now return to the FinalSteps
procedure, which takes the users to the output sheet.
The only remaining procedures are for the navigational buttons (see CS21.20). These
include the “End” button, the “View Product Info” button, the “View Dock Info” button, and
the “Go Back” buttons. Each procedure simply hides and shows the appropriate sheets.
24
Case Study 21: Warehouse Layout
25
Case Study 21: Warehouse Layout
Figure CS21.21 Revisiting the first input sheet and changing some of the dock
information.
At this point, the user can press the “Resolve” button to observe the new layout with the
changed dock information. Or, he or she can choose the other navigational button to
revisit the second input sheet. Let’s suppose the user also revisits the second input
sheet to modify the product type information. In Figure CS21.22, we have changed the
area requirements for the product types from 2, 4, and 7 to 4, 10, and 10, respectively.
We have also changed the demand values from 300, 500, and 600 to 500, 600, and 800,
respectively. The user can again use the “Go Back” button to return to the output sheet.
Figure CS21.22 Revisiting the second input sheet and changing the product type
information.
If the user now presses the “Resolve” button, the main procedures will be rerun and a
new layout will be displayed. In Figure CS21.23, we reveal the result of the resolved
layout after making the above changes to the dock and product type information.
26
Case Study 21: Warehouse Layout
Figure CS21.23 The new layout is displayed after pressing the “Resolve” button.
The second resolve option allows the users to enforce bay assignments for any of the
product types. To do so, they can select the product type that they wish to enforce from
the legend. Then, they can click on the desired bay assignments in the “Resolve Layout”
table. For example, suppose a user wants to enforce the bay assignments for all of the
bays required for product type 2. The user would click on the “P2” cell in the legend and
then select the desired assignment bays in the “Resolve Layout” table (see Figure
CS21.24).
Figure CS21.24 The resolve options allow the users to specify a particular
product’s layout on the Resolve Layout grid.
The user can then press the “Resolve” button to observe the updated layout with these
enforced assignments. In Figure CS21.25, we display the modified layout after enforcing
the bay assignments for product type 2. The other assignments are now made, given
that product type 2 will be assigned to the specified bays.
27
Case Study 21: Warehouse Layout
Figure CS21.25 The layout has been resolved with the user’s specifications
enforced.
Not all of the required bays for any product type need to be enforced. For example,
instead of enforcing all four of the required bays for product 2, the user could have only
enforced the assignment of two of the bays. Multiple product types can also be enforced
at once. In Figure CS21.26, we enforce the same bay assignments for product type 2 as
well as six of the seven required bays for product type 3.
The layout after the “Resolve” button has been pressed appears in Figure CS21.27.
28
Case Study 21: Warehouse Layout
Figure CS21.27 The final layout is modified to honor the enforced bay
assignments.
To allow the users to enforce bay assignments with this interface, we have written a
SelectChange event procedure that is associated with the output sheet (see Figure
CS21.28). This procedure is similar to the SelectChange event procedure associated
with the first input sheet in that we have to first verify the location of the selected cell
using Union functions.
We first check if the users have clicked in the legend area. If so, then we record the
product type that they had selected. Once a product type is selected, the users may click
in the “Resolve Layout” area to specify the enforced assignments. We confirm that the
next cell clicked is indeed in this layout area and reformat the selected cell with the
product type’s color.
We also record that the selected bay has been fixed in a Boolean array called FixedBay.
We must ensure that the users do not specify more bay assignments than the required
number of bays for the selected product type. We do so by keeping track of the number
of bays that have been assigned for the selected product type in a FixedProd array.
29
Case Study 21: Warehouse Layout
Figure CS21.28 The SelectChange event procedure allows the users to enforce
particular bay assignments for selected product types.
We can now discuss the Resolve procedure that re-performs the calculations and re-
runs the algorithm for either of these two resolve options (see Figure CS21.29). Since
we have stored which bay assignments are fixed, we will skip those bays when we reach
the Assign procedure. Therefore, we begin the procedure by copying the “Resolve
Layout” to capture the users’ assignments.
We then clear the output sheet and recall the DockInfo and FinalSteps procedures.
These procedures re-record the information from the dock table and the product type
table on the input sheets to capture any changes that the users have made to the input
values. The assignment algorithm is then executed; this time, it ignores the bays that
have already been fixed. The resulting layout reflects the users’ enforced assignments.
We then reset the FixedProd and FixedBay arrays.
30
Case Study 21: Warehouse Layout
Figure CS21.29 The Resolve procedure records changes made to input values and
honors enforced bay assignments.
First resolve Use view buttons to return to input sheets and modify
option values; then, press the “Resolve” button to see the
new layout.
Summary Second resolve Enforce some product type bay assignments by
option clicking on a product type from the legend and
specifying enforced assignments on “Resolve
Layout”; then, press the “Resolve” button to see the
new layout.
Worksheet_ Event procedure to allow user to make assignment
SelectionChange enforcements; it records which bays are fixed and
how many bays have been fixed for each product
type.
Resolve Copies the “Resolve Layout” and recalls the DockInfo
and FinalSteps procedures; reassigns bays ignoring
those that were fixed by the user.
31
Case Study 21: Warehouse Layout
CS21.6 Summary
¾ The warehouse layout model assigns bays of a warehouse to different product
types in such a way that the total distance traveled to docks is minimized given
the weighting of the docks to bay distances and the product demand to area
ratio. This assignment is accomplished using a greedy algorithm.
¾ There are four main spreadsheets: welcome, first input sheet, second input
sheet, and output sheet.
¾ The user interface includes an input form, two input sheets, and several
navigational and functional buttons.
¾ There are several procedures for this application. They guide the user through
the input sheets and implement the greedy algorithm to make the assignment.
¾ The DSS components include two resolve options: the user can either modify
input values and resolve or enforce some product type bay assignments (or
both).
CS21.7 Extensions
1. Create an alternative method for sorting the items and bays according to their
weights. Hint: You do not have to use arrays.
2. How could you enforce adjacent bay assignments within a product type? Create
a small procedure that would enforce this.
3. What are some other problems similar to the warehouse layout problem in which
this DSS may be reused with slight modifications?
32
Case Study 22
Critical Path Method
with Time-Cost Tradeoff
Chapter Overview
CS22.2 Spreadsheets
CS22.3 User Interface
CS22.4 Procedures
CS22.5 Resolve Options
CS22.6 Summary
CS22.7 Extensions
Case Study 22: Critical Path Method 2
Related to the critical path method, the time-cost tradeoff problem entails crashing, or
reducing, the activity times to a crash time at a given crash cost. Depending on the
structure of the activities network, there may be several different ways in which the
activities can be crashed in order to reduce the overall project time at a minimal cost. As
activities are crashed and the project time is reduced, the total project cost is increased,
which creates the time-cost tradeoff. The time-cost tradeoff problem is to determine for a
desired overall project time which activities should be crashed in order to minimize the
total crash cost, or total project cost.
In this application, the user can either find the critical path of the project or create a
graph of the time-cost tradeoff from crashing activities. For the critical path method
option, the user creates a project network, provides the activity times and costs, provides
a precedence matrix of the events, and then views the critical path of the project with the
total project completion time. For the time-cost tradeoff option, the user creates a project
network, provides the activity times and costs along with the crash times and crash
costs, provides a precedence matrix of the activities, and then views the time-cost
tradeoff as the project is crashed for various project times.
For the CPM option, the user provides information about the activities. Here, we assume
that the project network is represented by nodes as events and arcs as activities that
connect those events. For example, in the network pictured below, there are six events
and seven activities. The numbers written above each activity and each arc are the
activity times.
3 5
2
4
8
1
2
3
4
5
6
5 6
To create the precedence matrix for this network representation, we create a matrix with
the number of rows and number of columns equal to the number of events. Then, if any
event in row i precedes an event in column j, then the activity that connects these two
events is written in cell (i, j). The table below is the precedence matrix corresponding to
the network above.
1 2 3 4 5 6
1 1 4
2 3 2
3 6
4 5
5 7
6
The CPM then computes the early start time (the earliest time the activity can start) and
the late start time (the latest time the activity can start) for each activity. Early start times
are calculated by analyzing the network activities from the beginning to the end of the
project. This analysis is performed using the precedence matrix; the matrix is scanned
over each row i = 1 to the number of events and over each column j = 1 to the number of
events. The following If, Then statement calculates the early start times based on the
activities found in the precedence matrix. [Here CurrentAct is the activity listed in a cell
(i, j).]
The application calculates the late times by analyzing the network activities from the end
back to the beginning of the project. This analysis is performed using the precedence
matrix; the matrix is scanned over each row i = number of events backwards to 1 and
over each column j = number of events backwards to 1. The following If, Then statement
calculates the late start times based on the activities found in the precedence matrix.
[Here CurrentAct is again the activity listed in a cell (i, j).]
From these times, the free float and total float for each activity are computed using the
precedence matrix. If there is an activity in a cell (i, j), then the following calculations are
performed:
The CPM can now determine the critical path of the project. Any activity with a total float
time of zero is on the critical path. The total project time and the total project cost are
updated based on these critical activities.
If TotalFloat(i) = 0 Then
ProjDur = ProjDur + ActDuration(i)
ProjCost = ProjCost + NormCost(i)
End If
For the time-cost tradeoff option, the user provides the project network and activity
information including the crash times and crash costs for each activity. Here, we assume
that the project network is represented by nodes as activities, and arcs represent the
ordering of those activities. For example, in the network pictured below, there are seven
activities. The nodes s and t are dummy sources and sink nodes respectively; they
represent the beginning and end of the project.
2 5
3
6
s t
4 7
To create the precedence matrix for this network representation, we develop a matrix
with the number of rows and columns equal to the number of activities + 2. (The
additional two activities are for the dummy s and t nodes.) Then, if any activity in row i
precedes an activity in column j, the number 1 is written in cell (i, j). The table below is
the precedence matrix that corresponds to the network above.
s 1 2 3 4 5 6 7 t
s 1 1
1 1 1
2 1
3 1
4 1
5 1
6 1
7 1
t
The application then solves a linear programming model to find the best selection of
activities that should be crashed to minimize the total project cost for a desired project
time. We solve this problem iteratively for various desired project times. The linear
programming problem is prepared in a hidden spreadsheet and solved by the Solver.
The parts of the model and the formulation for this problem are below. (Figure CS22.1
presents the hidden spreadsheet with the prepared model.)
Decision Variables:
Start time per activity, u(i)
Crash time per activity, B(i)
Constraints:
1. Crash times should be less than or equal to the difference between the activity
time and the crash time provided in the activity table.
B(i) ≤ ActDur(i) – CrashDur(i)
2. The total project time must be less than or equal to the desired project time.
Start time of end – Start time of beginning <= desired project time
u(t) – u(s) ≤ P
3. Using the precedence matrix for every cell (i, j) with a value of 1, the start time
of activity j should be greater than or equal to the start time of activity i plus the
activity time for i minus the crash time for i.
start time(j) >= start time(i) + ActDur(i) – CrashTime (i)
u(j) ≥ u(i) + ActDur(i) – B(i)
Objective Function:
Minimize total project cost = SUMPRODUCT(decision variables, crash slopes)
(crash slope (i) = ABS((CrashCost(i) – NormCost(i)) / (ActDur(i) - CrashDur(i))))
Model Formulation:
Minimize ∑i BiSi , Si = crash slopes or cost per time unit crashed
Subject to:
Bi ≤ Ai – Ci , Ai = activity time and Ci = crash time
ut – us ≤ P
uj ≥ ui + Ai – Bi
Bi ≥ 0
ui ≥ 0
The time-cost tradeoff graph shows the project cost achieved from the crashing model
for each iterative project time. In this graph, the total project cost includes the sum of the
normal activity costs. The resulting graph should be a piece-wise linear convex function.
For more details on the critical path method or time-cost tradeoff problem, please see
Introduction to Operations Research by Winston.
CS22.1.2 Input
CS22.1.3 Output
CS22.2 Spreadsheets
This application requires seven spreadsheets: the welcome sheet, the network sheet,
the activity table sheet, the precedence matrix sheet, the CPM output sheet, the time-
cost tradeoff output sheet, and the hidden crash LP model sheet. (Note: we also have
created an example sheet, which stores demo data and is available for the user to view
if extra help is needed.) The welcome sheet contains the title, the description of the
application, and the “Run Demo” and “Start” buttons. (See Figure CS22.2.) “Run Demo”
and “Start” both display an option form and take the user to the network sheet.
On the network sheet, the user creates the project network with the sample drawing
objects provided (the circle with the text box, the arrow with the text box, and the dashed
arrow). (See Figure CS22.3.) For the CPM option, the user creates the network such
that each node = event and each arc = activity. [See Figure CS22.3 (a).] The user also
needs to name the arcs in order to view the resulting critical path. Instructions on how to
name the arcs are provided in a comment box of the cell with the text “Help?” next to the
buttons on the sheet. For the time-cost tradeoff option, the user creates the network
such that each node = activity. [See Figure CS22.3 (b).] The buttons on the sheet are:
“End,” which takes the user back to the welcome sheet; “See Example,” which takes the
user to an example sheet; and “Continue,” which takes the user to the activity table
sheet.
(a)
(b)
Figure CS22.3 The network sheet.
On the activity table sheet, the user provides information about each activity in the
project. (See Figure CS22.4.) An activity number is automatically entered in the table;
the user then describes for each activity and inputs the normal duration, or time, of the
activity as well as the normal cost of the activity. [See Figure CS22.4 (a).] For the time-
cost tradeoff option, the user also provides the crash duration and crash cost for each
activity. [See Figure CS22.4 (b).] The same buttons found on the network sheet are
available here; this time the “Continue” button takes the user to the precedence matrix
sheet. There is also a “View Network” button, which takes the user back to the network
sheet; this may be useful if the user needs to check the project network to find the
corresponding activities times.
(a)
(b)
Figure CS22.4 The activity table sheet.
On the precedence matrix sheet, the user completes the project’s precedence matrix.
(See Figure 48.5.) Depending on which option the user has selected to solve, the
corresponding precedence matrix format is automatically created. In other words, for the
CPM option, a precedence matrix is created with the number of rows and columns =
number of events. [See Figure CS22.5(a).] Here, cell values equal the activity number in
the precedence relationship between the row and column events. For the time-cost
tradeoff option, a precedence matrix is created with the number of rows and columns =
number of activities plus two. [See Figure CS22.5 (b).] Here, cell values equal 1 if
precedence exists between the row and column activities. The “End,” “See Example,”
and “View Network” buttons are also on this sheet. The user can also press the “Solve”
button to solve the problem. He or she is then taken to the corresponding output sheet.
(a)
(b)
Figure CS22.5 The precedence matrix sheet.
The output sheets show the solution to the selected problem. (See Figure CS22.6.) For
the CPM option, the report presents the early time and late time for each event, the free
float and total float time of each activity, and which activities are on the critical path. [See
Figure CS22.6 (a).] The total project duration and total project cost are also provided.
The user can press the “View Network” button to return to the network sheet and see the
critical path highlighted on the network he or she drew. (See Figure CS22.7.) For the
time-cost tradeoff option, the report presents the time-cost tradeoff graph, the
corresponding desired project durations, the achieved project durations, and the project
costs for each time-crashing iteration. [See Figure CS22.6 (b).]
(a)
(b)
Figure CS22.6 The output sheets.
Figure CS22.7 The updated network sheet for the CPM option.
After the user creates the project network on the network sheet and presses the
“Continue” button, the following form appears before the activity table sheet. (See Figure
CS22.9.) This form, the network form, prompts the user for the number of events and
activities represented in the project network. This form requires two text boxes.
Main options form Where the user chooses between the CPM and
time-cost tradeoff options.
Network form Where the user enters the number of activities and
Summary events in the network.
Navigational “End,” “View Network,” “See Example,” “Return to
buttons Network Sheet,” “Return to Activity Table,” “Return
to Precedence Matrix.”
Functional buttons “Continue” and “Solve.”
CS22.4 Procedures
We will now outline the procedures for this application beginning with the variable
declarations and the initial sub procedures. (See Figure CS22.10.) The Main procedure,
which is called from the “Start” button, calls the ClearPrevious procedure to clear
previous values from all the sheets. It then displays the main options form and takes the
user to the network sheet.
The Demo procedure is called from the “Run Demo” button on the welcome sheet. (See
Figure CS22.11.) This procedure also begins by calling the ClearPrevious procedure
and showing the main optoins form. Then, depending on which problem the user has
selected to solve, the corresponding example data is copied from the example sheet to
the network sheet, the activity table sheet, and the precedence matrix sheet. The user is
then taken to the network sheet. A message box informs the user that the project
network and other values have already been created for the demo.
The procedures for the main options form are illustrated in Figure CS22.12. A Boolean
value records the user’s selection of either the CPM problem or the time-cost tradeoff
problem.
When the user presses the “Continue” button on the network sheet, the CreateTables
procedure is called. (See Figure CS22.13.) If the demo is not being run, then the
network form asks the user for the number of events and activities in the network. Using
these parameters, the activity table is created with the number of rows equal to the
number of activities. The precedence matrix is also created. For the CPM option, the
precedence matrix’s number of rows and number of columns equal the number of
events; for the time-cost tradeoff option, the precedence matrix’s number of rows and
number of columns equal the number of activities plus two. The application also formats
the precedence matrix to create an upper-triangle matrix. In other words, the user should
only enter values in the upper right-hand part of the matrix. If the demo is being
performed, neither the activity table nor the precedence matrix is created since each has
already been copied from the example sheet. At the end of this procedure, the user
views the activity table sheet.
The procedures for the network input form are presented in Figure CS22.14. These
procedures simply record the number of activities and number of events. The procedure
also performs some error checking to ensure that there are at least two activities in the
network.
When the user presses the “Continue” button on the activity table sheet, the ActivityList
procedure is called. (See Figure CS22.15.) In this procedure, arrays store the activity
times and costs that the user has entered into the table. If the user has selected the
time-cost tradeoff option, then the crash times and costs are also recorded and the crash
slopes are calculated. The user then views the precedence matrix sheet.
The “Solve” button on the precedence matrix sheet is assigned to the Solve procedure.
(See Figure CS22.16.) If the user is solving the CPM problem, then the CPM procedure
is called to determine the critical path. Then, the CreateReport procedure is called to
display the CPM results on the output sheet.
If the time-cost tradeoff problem is solved, then the application calls the CrashLP
procedure to prepare the LP model on the hidden crash LP sheet. Then, a “Do, While”
loop solves this model for different project times. The first time the model is solved, the
desired project time is set very high to ensure feasibility. The resulting achieved project
time then acts the first project time value in the performed iterations; each iteration
decreases the desired project time by one time unit. For every loop, the desired project
time and the resulting project cost are recorded on the output sheet. When the loop is
finished (when the model has no feasible solution for the desired project time), the time-
cost tradeoff graph is created from these recorded values.
For the CPM option, the CPM procedure finds the critical path. (See Figure CS22.17.) In
this procedure, the early times and late times are calculated for each event by scanning
the precedence matrix, as described in Section CS22.1.1. Then, the application
calculates the free float and total float times for each activity, again by using the
precedence matrix. The total float times are then reviewed to determine which activities
are on the critical path (those with a total float time of zero). The total project duration
and project cost are also calculated.
The CreateReport procedure outputs the values found in the CPM procedure to the
output sheet. (See Figure CS22.18.) The early and late times for each event are
recorded in an events table, and the free float and total float times for each activity are
recorded in an activities table. For each activity on the critical path, the corresponding
arc object on the project network (which the user created on the network sheet) is
colored red. The total project duration and project cost are also recorded. Then, the user
views the output sheet.
The CrashLP procedure loads the crashing LP model on the hidden sheet. (See Figure
CS22.19.) The values stored from the activity table and the precedence matrix create
the constraints. The decision variable cells are prepared for both the start times and the
crash times. The crash slopes are listed for the objective function. After all the required
ranges have been named, the Solver model is loaded. The Solver is run in the Solve
procedure’s loop.
Figure CS22.20 illustrates the ClearPrevious procedure. This procedure clears the
previous values on all the sheets. On the network sheet, the drawing objects that create
the project network are deleted. The values in the activity table are cleared, and the
entire precedence matrix is removed. Finally, the application clears the values on both of
the output sheets.
CS22.6 Summary
¾ In this application, the user can either find the critical path of the project or create
a graph of the time-cost tradeoff from crashing activities.
¾ This application requires seven spreadsheets: the welcome sheet, the network
sheet, the activity table sheet, the precedence matrix sheet, the CPM output
sheet, the time-cost tradeoff output sheet, and the hidden crash LP model sheet.
(There is also a hidden example sheet.)
¾ For this application’s user interface, we use navigational and functional buttons
and two user forms.
¾ Several procedures in this application record the information about the user’s
project network and solve either the CPM or time-cost tradeoff problem.
¾ The user can resolve this application by returning to the welcome sheet and
solving the model again.
CS22.7 Extensions
1. Make the network form dynamic so that if the time-cost tradeoff problem is being
solved, the form only prompts the user for the number of activities. In other
words, the label and text box for the number of events should disappear or be
grayed-out if the time-cost tradeoff option was selected in the first options form.
2. Create different resolve options:
a. Allow the user to preserve the project network but change the values on
the activity table.
b. Allow the user to keep the same network but solve a different problem.
For example, he or she may first solve the CPM problem and then want to
solve the time-cost tradeoff problem. What new values does the user
need to provide?
c. Can the user take the results of the time-cost tradeoff problem and find
the critical path for a particular set of activity times? How would this be
done?
3. Extend this application to include a third option to solve the PERT problem. What
features will be the same? What features will need to be added?
Chapter Overview
CS23.2 Spreadsheets
CS23.3 User Interface
CS23.4 Procedures
CS23.5 Resolve Options
CS23.6 Summary
CS23.7 Extensions
Case Study 23: Kruskal’s Algorithm 2
The intuition behind this algorithm is that if there is an arc cost cij > ckl for some non-tree
arc (k, l) and some tree arc (i, j) contained, adding arc (k, l) to the tree in place of arc (i, j)
gives us a spanning tree with a lower cost. The correctness of Kruskal's algorithm
follows from the fact that we discarded each nontree arc (k, l) with respect to T* at some
stage because it created a cycle with the arcs already in LIST. But observe that the cost
of arc (k, l) is greater than or equal to the cost of every arc in that cycle because we
examined the arcs in the nondecreasing order of their costs. Therefore, the spanning
tree T* satisfies the path optimality conditions and so it is an optimal tree.
cij
i j
10
2 4
35
1 25 20 30
40
3 5
15
(a)
10
2 4
10
2 4
35
35
1 25 20 30
1 25 20 30
40
40 3 5
5 15
3
15 (c)
(b)
10
10 2 4
2 4
35
35
1 25 20 30
1 25 20 30
40
40 3 5
3 5 15
15 (e)
(d)
10
10 2 4
2 4
35
35
1 25 20 30
1 25 20 30
40
40 3 5
3 5 15
15
(g)
(f)
We assume that the user creates their network and tells us the number of nodes and
arcs they have created. We also ask the user to complete a table listing all of the arcs in
the network, the nodes they connect, and their arc cost. We use this table to sort the
arcs in ascending order of their arc costs. We can then use the node pairs for each listed
arc to determine whether or not a cycle would be created if an arc was added to the tree.
For more information about Kruskal's algorithm, please refer to the book Network Flows:
Theory, Algorithm, and Applications by Ahuja, Magnanti, and Orlin.
CS23.1.2 Input
The input for this application is primarily the network that the user creates.
CS23.1.3 Output
CS23.2 Spreadsheets
We use three spreadsheets in this application: the welcome sheet, an example sheet,
and the network sheet. The welcome sheet has the title and description of the
application as well as an image (see Figure CS23.1). There are two buttons on this
sheet: “Run Demo” and “Start Program”. Both buttons will bring the user to the network
sheet.
The network sheet is the main sheet of the application (see Figure CS23.2). Detailed
directions are given to the user at the top of this sheet on how to create their network
and how to fill in the table with arc information. The user begins by creating their
network. They can copy and paste the node and arc images to create the network in the
area above the table. They can also copy and paste the text box to label their arcs with
the arc costs. The user must also name each arc using Excel’s name window. This
visual information is for the animation of Kruskal’s algorithm.
When the user is done creating the network, they will press the “Create Table” button.
This button will then prompt them with a user form to ask for the number of nodes and
arcs in the network they have just created. A table will then be drawn on the sheet for
the user to provide the arc information (see Figure CS23.3). This information includes
the arc number, node pair that the arc connects, and arc cost. The last column in the
table will be used by the application to display to the user which arcs are in and out of
the final minimum spanning tree. When the user has completed filling this arc table, they
can press the “Solve” button to begin the animation of Kruskal’s algorithm.
As the creation of the minimum spanning tree is being performed one arc at a time by
Kruskal’s algorithm, the user can see which arcs are being evaluated and whether or not
the evaluated arc has been put in the tree or kept out. As each arc is evaluated a “Yes”
or “No” will be displayed in the arc table to show whether or not the arc is in the
minimum spanning tree.
In Figure CS23.4, we show an example network in the middle of the algorithm animation.
Here, the first five arcs have been evaluated: the first three are in the tree, the fourth is
out of the tree, and the fifth is in the tree. The sixth arc is currently being evaluated. Note
that the animation of the arc evaluations in the network corresponds to the order of the
rows in the arc table.
The final minimum spanning tree for this example is shown in Figure CS23.5. Once the
algorithm animation is complete, the user can see which arcs are in and out of the
minimum spanning tree by viewing the colors of the arcs in the network and the last
column in the arc table. The total cost of the spanning tree is also displayed. This cost is
the sum of all arc costs of the arcs in the minimum spanning tree. The user can now
press “Reset” in order to change arc costs and resolve, or they can exit the application
by pressing the “End” button.
The example sheet is available to the user if they press the “See Example” button on the
network sheet. The example sheet has an example network and completed arc table
(see Figure CS23.6). This sheet is used to guide the user in creating their own network
and filling in their own arc table. The user can press the “Return to Network Sheet”
button to return to the network sheet. The data on this sheet is also used for running the
demo of the application.
The remainder of the interface includes the functional buttons “Solve” and “Reset” as
well as the navigational buttons “See Example”, “Return to Network Sheet”, and “End”.
Some formatting is done to highlight the button which should be pressed at the
necessary time. This is another clarification tool incorporated in the user interface.
CS23.4 Procedures
We will now outline the procedures for this application. We begin with the initial sub
procedures and variable definitions (see Figure CS23.8). The Main procedure, assigned
to the “Start Program” button, calls the ClearPrevious procedure and takes the user to
the network sheet. The Demo procedure, assigned to the “Run Demo” button, calls the
ClearPrevious procedure, takes the user to the network sheet, and copies the network
from the example sheet to the network sheet.
The ClearPrevious procedure clears previous data and initializes some variable values
(see Figure CS23.9). It also clears the formatting of the arc table and deletes the
network images. Some button formatting is also modified here.
When the user presses the “Create Table” button, the CreateTable procedure is called
(see Figure CS23.10). If the demo is being run, then the arc table is copied from the
example sheet and pasted in the network sheet. However, if the user has created their
own network, then the input form will now be shown to receive the values for the number
of nodes and arcs in the network. The arc table is then formatted as well as some of the
buttons.
The procedures for the input form simply record the number of nodes and arcs (see
Figure CS23.11). Some error checking is also performed.
When the “Solve” button is pressed, the Kruskal procedure is called (see Figure
CS23.12). This procedure implements and animates Kruskal’s algorithm for finding the
minimum spanning tree. The arc table is first sorted in ascending order of the arc costs.
Then arrays are populated with the arc names, tail nodes and head nodes (or node
pairs), and arc costs from the arc table. A labeling array is initialized; it will be used to
check for cycles as the spanning tree is created. DESCRIBE LABELING ARRAY.
The algorithm then simply checks the label array for each arc in its sorted order to
determine whether or not a cycle would be created if it were added to the spanning tree.
If no cycle would be created then it is added to the tree by calling the InTree function. If a
cycle would be created, then it cannot be put in the tree and the OutTree function is
called. We will discuss later why we check for arcs which are forced in the spanning tree
(see Section CS23.5). LABELING
The InTree function updates the label array used in the algorithm and then animates the
network (see Figure CS23.13). The animation changes the current arc format to dashed,
pauses using the Wait method, and then changes the color of the arc to show that it is in
the tree.
The OutTree function also animates the network (see Figure CS23.14). The animation
changes the current arc format to dashed, pauses using the Wait method, and then
changes the color of the arc to show that it is not in the tree.
The navigational procedures are used for the “End”, “Show Example” and “Return to
Network Sheet” buttons (see Figure CS23.15).
After pressing the “Reset” button and before pressing the “Solve” button to re-run
Kruskal’s algorithm, the user can use either or both of the two resolve options. The first
option is for the user to modify the arc costs and see how it affects the minimum
spanning tree. That is, if some arc costs are changed, when the algorithm is
implemented and the arcs are sorted and cycles are checked, the resulting minimum
spanning tree may differ from the original result.
The second option is for the user to force some arcs to be in the spanning tree. That is,
suppose some constraints require a particular arc to be in the spanning tree regardless
of its arc cost and node pairs. This resolve option uses part of the code in the Kruskal
procedure to check for these forced arcs before implementing the algorithm (see Figure
CS23.17). The user can force the arcs in the tree by typing “Yes” (or “yes” or “Y” or “y”)
in any row in the last column of the arc table.
For example, in Figure CS23.18, a minimum spanning tree is shown after the “Solve”
button was pressed the first time; this is the original solution. As you can see, the arc
with cost 8 has not been included in the minimum spanning tree. However, we can force
this arc to be in the tree. To do so, we have typed “Yes” in the row for this arc in the last
column of the arc table. We did this after we pressed the “Reset” button and before we
press the “Solve” button again.
Figure CS23.18 The user can enter “Yes” in the table to force arcs in the tree.
Once the “Solve” button is pressed this time, the Kruskal procedure will first identify this
arc as a forced arc and put in the tree. It will then continue implementing the algorithm
given that the label array has been updated for this first arc already. In Figure CS23.19,
you can now see that this arc, with cost 8, has been included in the minimum spanning
tree. The row for any arc forced in the tree is highlighted to inform the user that the
algorithm did indeed force this arc in the tree.
“Reset” and These buttons allow the user to user either or both
“Solve” buttons of the resolve options.
Changing arc costs The user can change arc costs and resolve.
Summary
Forcing arcs in tree The user can force some arcs to be in the
spanning tree and then resolve.
CS23.6 Summary
¾ This case study allows the user to specify a network and animate Kruskal's
Algorithm to identify a minimum spanning tree.
¾ We use three spreadsheets in this application: the welcome sheet, an example
sheet, and the network sheet.
¾ For this application, we use navigational and functional buttons as well as one
user form for the user interface.
¾ There are several procedures for this application which implement and animate
Kruskal’s algorithm.
¾ The user can resolve the application by using the “Reset” and “Solve” buttons on
the network sheet. They can change arc costs or force arcs to be in the spanning
tree.
CS23.7 Extensions
1. Allow user to also be able to change other network information before resolving.
For example, you should be able to record not only new arc costs, but also new
arcs and new nodes.
2. Design an alternative user interface for all of the options available to the user on
the network sheet.
3. Modify this application to implement and animate Prim’s algorithm for finding a
minimum spanning tree. (Refer to Network Flows: Theory, Algorithm, and
Applications by Ahuja, Magnanti, and Orlin.)
4. Modify this application to implement and animate Sollin’s algorithm for finding a
minimum spanning tree. (Refer to Network Flows: Theory, Algorithm, and
Applications by Ahuja, Magnanti, and Orlin.)
Chapter Overview
CS24.2 Spreadsheets
CS24.3 User Interface
CS24.4 Procedures
CS24.5 Resolve Options
CS24.6 Summary
CS24.7 Extensions
Case Study 24: Animating the Simplex Method 2
The simplex method, developed by George Dantzig in 1947, maintains a basic feasible
solution at every step. Given a basic feasible solution, the method first applies the
optimality criteria to test the optimality of the current solution. If it does not fulfill this
condition, then the algorithm performs a pivot operation to obtain another basis structure
with a lower or the same cost. The simplex method repeats this process until the current
basic feasible solution satisfies the optimality criteria. We will now describe the simplex
algorithm using a numerical example.
Let’s consider the following linear programming problem in which we seek to maximize a
value written in terms of four variables. This objective is limited by three constraints and
a non-negative variable requirement.
The problem must first be modified to canonical form before the simplex method can be
applied. The addition of slack variables and the transformation to canonical form restates
the problem as follows:
2x1 + 3x2 - x3 + x4 + x6 = 12
x1 + x3 + x4 + x7 = 4
x1, x2, x3, x4, x5, x6, x7 ≥ 0
Note that there are now seven variables and three constraints and that adding the slack
variables has not changed the value of the objective function or constraints. The
coefficients of all of the variables in the objective function and constraints can now be
written as a matrix, or tableau. Here is a representation of the initial tableau:
z x1 x2 x3 x4 x5 x6 x7 RHS
z 1 -2 -1 -5 3 0 0 0 0
x5 0 1 2 4 -1 1 0 0 6
x6 0 2 3 -1 1 0 1 0 12
x7 0 1 0 1 1 0 0 1 4
The coefficient values of the constraints’ slack variables form an identity matrix. This
tableau is used to perform the pivot operations for each iteration of the simplex method.
These operations identify a nonbasic entering variable that has the largest negative
value (for maximization problems) or the largest positive value (for minimization
problems) in the objective function. The applicatoin then compares the ratios of the
corresponding column coefficients to the RHS column coefficients to find the basic
variable with the minimum ratio. This variable is the leaving variable. Then, the
application performs pivot operations to make this column have 0 and 1 coefficient
values (to become part of the identity matrix) such that the 1 coefficient value is in the
row of the leaving variable. The leaving and entering variables are then switched to
complete the iteration.
For example, the first iteration of the simplex method for the problem defined above
selects x3 as the entering variable since it has the largest negative coefficient in the
objective function (-5). It then compares the ratios of 6/4, -12/1, and 4/1; excluding the
negative ratio, the 6/4 ratio can be declared as the minimum ratio, thus identifying x5 as
the leaving variable. Pivot operations is then performed and the variables are switched
to yield the resulting tableau for the first iteration:
z x1 x2 x3 x4 x5 x6 x7 RHS
z 1 -0.75 1.5 0 1.75 1.25 0 0 7.5
x3 0 0.25 0.5 1 -0.25 0.25 0 0 1.5
x6 0 2.25 3.5 0 0.75 0.25 1 0 13.5
x7 0 0.75 -0.5 0 1.25 -0.25 0 1 2.5
This application requires that the linear program is stated in canonical form. It allows the
user to select the entering nonbasic variable while the minimum ration and leaving
variable are determined. The application automatically checks for the unboundedness of
the optimal solution. For more details on the simplex method, see Introduction to
Operations Research by Winston.
CS24.1.2 Input
• Initial tableau
• Number of variables (after transformation to canonical form)
• Number of constraints
• Maximization or minimization objective
• Entering variable for each iteration
CS24.1.3 Output
CS24.2 Spreadsheets
This application requires four spreadsheets: the welcome sheet, the input sheet, the
example sheet, and the report sheet. The welcome sheet contains the title, the
description of the application, and the “Run Demo” and “Start” buttons. (See Figure
CS24.1.) The “Run Demo” button takes the user to the input sheet and creates the initial
tableau from the example sheet. (See Figure CS24.3.) The “Start” button takes the user
to the input sheet to create his or her own initial tableau.
The input sheet instructs the user how to create the initial tableau and animate the
simplex method iterations. A navigational form, which is always available to the user on
the input sheet, provides several options for performing the iterations. (We will discuss
this process in detail in the next section.) The user can also view the example sheet for
guidance in constructing the initial tableau by clicking the “See Example” button.
The example sheet contains an example of a linear programming problem. (See Figure
CS24.3.) It reveals how to transform a problem into canonical form by adding slack
variables. It also provides the initial tableau for this problem, which is the same initial
tableau used for the demo option.
The report sheet displays a summary report of the iterations performed. (See Figure
CS24.4.) A table lists the entering variable, the leaving variable, the minimum ratio, the
reduced cost, and the objective function value for each iteration. Additionally, the sheet
contains a chart of the objective function values over all the iterations.
(a) (b)
Figure CS24.5 The navigational form.
This second form is the input form. (See Figure CS24.6.) Here, the user inputs the
number of variables (including the slack variables) and the number of constraints in the
problem. He or she also specifies if the problem has a maximization or minimization
objective.
If the user selects to view each iteration, then the navigational form changes to display a
“Show Next Iteration” button. As the iterations are performed, the user can select an
entering variable by clicking on the variable name on the current tableau; the
corresponding minimum ratio and the leaving variable values are displayed on the
navigational form along with the change in the objective function. (See Figure CS24.7.)
When the optimal solution is found, the navigational form changes again to display a
“View Report” button. [See Figure CS24.6(b).] This button takes the user to the report
sheet. We discuss the “Resolve” button in Section CS24.5.
Figure CS24.7 The input sheet during the simplex method animation.
The “End” button on the input sheet and the report sheet takes the user back to the
welcome sheet. The “See Example” button takes the user to the example sheet, and the
“Return to Tableau” button on the example sheet and on the report sheet returns the
user to the input sheet.
CS24.4 Procedures
We will now outline the procedures for this application beginning with the initial sub
procedures and the variable definitions. (See Figure CS24.8.) The Main procedure,
which is called from the “Start” button, calls the ClearPrevious procedure and takes the
user to the input sheet. The Demo procedure calls the Main procedure and copies and
pastes the demo initial tableau on the input sheet; it also initializes the problem
parameters so the input form does not need to be shown.
Figure CS24.8 The variable declarations and the Main and Demo procedures.
The ClearPrevious procedure clears any previous data and initializes several variables.
(See Figure CS24.9.) It also initializes the input sheet formatting as well as the
navigational form buttons and default values.
The navigational form has several procedures. (We will discuss the “Resolve” button
procedures in Section CS24.5.) The “Show Each Iteration” button procedure begins by
displaying the input form. (See Figure CS24.10.) It then updates the buttons on the form
and calls the CreateData, Check, and DetermineEnteringVar procedures.
The procedure for the “Show Final Solution” button also begins by showing the user the
input form and updating the buttons on the form. (See Figure CS24.11.) It then also calls
the CreateData and Check procedures. Next, it performs a loop in which the iterations
occur until the solution is optimal. These iterations involve calling the
DetermineEnteringVar, DetermineLeavingVars, PerformPivotOps, and NewTableau
procedures. Once the optimal solution has been determined, the form buttons are again
updated and a message box is displayed to the user.
If the user initially selected the “Show Each Iteration” button, then the procedure for the
“Show Next Iteration” button is used. (See Figure CS24.12.) Note that this procedure is
called after the user selects the entering variable and a corresponding leaving variable
has been determined. This procedure therefore begins by updating the objective function
value and reporting the current iteration values on the report sheet’s summary table. It
then calls the PerformPivotOps and NewTableau procedures to progress to the next
iteration. The DetermineEnteringVar procedure selects a default entering variable, and if
no entering variables are found, then the solution is optimal and the report sheet is
updated.
The CreateData procedure is initially called to populate an array with the values provided
by the user in the initial tableau. (See Figure CS24.15.) It is then called for each iteration
to update this array with the new tableau values. The DetermineEnteringVar procedure
finds the default entering variable by scanning the objective function coefficients. (See
Figure CS24.15.) If no coefficient is found that meets the entering variable criteria for the
maximization or minimization problem, then the solution is considered to be optimal.
The DetermineLeavingVars procedure finds the leaving variable for the selected entering
variable by determining the minimum ratio. (See Figure CS24.16.) If no minimum ratio is
found, then the user is notified that the problem is unbounded. Otherwise, the change in
the objective function is determined, and this value along with the minimum ratio and
found leaving variable are displayed in the navigational form.
The PerformPivotOps procedure performs the pivot operations to switch the entering
variable and leaving variable in the tableau. (See Figure CS24.17.)
The NewTableau procedure creates the new tableau for the next iteration. (See Figure
CS24.18.) It enters the new values found after the PerformPivotOps procedure is
completed.
The Check procedure checks the feasibility of the initial tableau entered by the user.
(See Figure CS24.19.) It ensures that the RHS values are non-negative, that any
artificial variables are the initial basic variables, and that these artificial variables form an
identity matrix in the constraint coefficients. If the tableau does not reflect a canonical
problem format, then the tableau is transformed for the user.
The procedures for the input form simply record the number of variables and constraints
and the type of objective function. (See Figure CS24.20.)
The navigational procedures are for the “Return to Tableau” buttons, the “See Example”
button, and the “End” buttons. (See Figure CS24.21.)
CS24.6 Summary
¾ This application animates the simplex method to solve a user-defined linear
programming problem.
¾ This application requires four spreadsheets: the welcome sheet, the input sheet,
the example sheet, and the report sheet.
¾ We use navigational buttons and two user forms for the user interface.
¾ Several procedures in this application initialize and perform the simplex method
iterations.
¾ The user can resolve the application by pressing the “Resolve” button on the
navigational form to clear all iterations and modify the initial tableau.
CS24.7 Extensions
1. Allow the user to solve this problem with the dual simplex method.
2. Perform sensitivity analysis for the user.
3. Create a graph of the user’s problem and animate the selection of all the extreme
points as the iterations are performed.
Chapter Overview
CS25.2 Spreadsheets
CS25.3 User Interface
CS25.4 Procedures
CS25.5 Resolve Options
CS25.6 Summary
CS25.7 Extensions
Case Study 25: IMRT 2
Designing an optimal radiation density profile in the patient can be approached through
linear programming. The objective of this problem is to design a radiation therapy
treatment plan that delivers to the targets a specific level of radiation dose, or
prescription dose while sparing organs-at-risk by ensuring that the level of radiation does
not exceed a structure-specific tolerance dose.
Saliva Saliva
Gland Gland
Spinal
Cord
Tumor
Skin
Figure CS25.2 A two-dimensional view of the tumor and the organs-at-risk in the
head/neck cancer scenario.
The targets and organs-at-risk (together simply referred to as the structures) are
irradiated with a predetermined set of beams, each corresponding to a particular beam
angle. For dose calculation purposes, the patient body is divided into small cubicles
(squares in 2-dimensions), referred to as voxels. (See the Excel sheet.)
j = a voxel
JTumor = set of voxels in tumor
JOAR = set of voxels in OAR
In the treatment plan being studied here, the patient is irradiated from three equi-spaced
beams at angles of 00, 1200, and 2400. Each beam consists of multiple beamlets, and
the intensities of the beamlets can be controlled individually to obtain a desired plan.
Therefore, the intensities of the beamlets are the decision variables in the linear
program.
i = a beamlet
I = set of beamlets
The objective function is evaluated by a pre-defined function over the dose received by
the voxels. It is desirable to give a minimal dose to the organs-at-risk and to expose the
tumor to the dose prescribed.
Certain penalty functions ensure that we are sending a maximum dose to the target and
a minimum dose to the organs-at-risk.
With these notations, the formulation can be written as a linear programming problem:
minimize ∑
j∈JTumor
PjU w j + ∑
j∈J OAR
PjO v j + ∑
j∈J OAR
PjO v j
subject to
D j = ∑ dij xi , j ∈ J OAR , J Tumor
i∈I
D j ≤ UB j , j ∈ J OAR , J Tumor
D j ≥ LB j , j ∈ J Tumor
v j ≥ D j − TD j , j ∈ J OAR
v j ≥ D j − PD j , j ∈ J Tumor
w j ≥ PD j − D j , j ∈ J Tumor
xi ≥ 0, i ∈ I , v j ≥ 0, j ∈ J OAR , J Tumor , w j ≥ 0, j ∈ J Tumor
We solve this problem with the Excel Solver and prepare the spreadsheet with decision
variable cells, constraint formulas, and objective function formulas, as modeled in the
formulation above.
CS25.1.2 Input
This application has two types of input: fixed input and variable input. The fixed input is:
• Number of beams = 3
• Number of beamlets = 30, and number of beamlets per beam
• Number of structures = 4
• Number of voxels = 91, and number of voxels per structure
• Dose unit intensity from each beamlet to each voxel
The variable input for the application, which is entered by the user, is:
CS25.1.3 Output
CS25.2 Spreadsheets
This application requires six spreadsheets: the welcome sheet, a model description
sheet, the input sheet, a detailed input sheet, the model sheet, and the output sheet. The
welcome sheet contains the title, the description of the application, and the “Start”
button. (See Figure CS25.3.) The welcome sheet also contains a “View Model
Description” button that takes the user to the model description sheet. (See Figure
CS25.4.) The model description sheet simply describes the model to the user. The
“Start” button on the welcome sheet shows the user an options form and then navigates
him or her to other sheets. (We will describe this form, referred to as the main menu
form, in the next section.)
The input sheet shows the user the fixed input and allows him or her to change the
variable input. (See Figure CS25.5.) The first two tables on the sheet contain the fixed
values, and the cells in these tables are locked so the user cannot change the values.
The fixed values for the dose unit intensity from each beamlet to each voxel are so large
that they are stored on a separate sheet. (See Figure CS25.6.) The user can view this
sheet by pressing the “View Bixel-Voxel Effect” button. The variable input values, or
parameters, are shown in a table at the bottom of the input sheet. These values are also
locked on the spreadsheet so the user modifies them with a form. The user can view this
form by pressing the “Modify Values” button. We ensure that the user provides the input
with a form so that bounds are placed on the possible input values. (We will discuss
these input forms in the next section.)
The model sheet is the most complex sheet of the application as it contains all of the
components of the IMRT linear programming model. (See Figure CS25.7.) At the top of
the page, there are two sets of decision variable cells: bixel intensities and voxel doses.
The voxel dose decision variables are actually calculated variables; in other words, they
are defined in terms of the bixel intensity variables. The bixel-voxel fixed input values
and the SUMPRODUCT function create this definition.
The constraint formulas are contained in several cells on the spreadsheet. With the
variable input provided by the user, the application calculates the overdose penalties,
the tolerance/prescription dose penalties, the upper bound and lower bound penalties,
and the dose deviations for each structure. The objective function cell sums the total
penalties per structure, a value that we seek to minimize. Even though this model seems
large and slightly complex in its constraints, it is still a linear programming problem and
can easily be solved by the Excel Solver.
The output sheet shows the voxel dose per structure as well as a dose-volume
histogram. (See Figure CS25.8.) The voxel doses are displayed in a grid that maps the
voxels to their structures, and a legend is provided for the structures. The dose-volume
histogram presents the cumulative percentage of the dose per volume of each structure;
here, the volume is measured by number of voxels. For example, in Figure CS25.8, 36.6
percent of the tumor voxels are receiving a dose of 70 or higher.
to solve the problem and see the results. (See Figure CS25.9.) This form requires a
frame with three option buttons.
Several forms allow the user to enter or modify the input values. The first form is the
main input form shown in Figure CS25.10. In this form, the user chooses whether to
modify the input for the organs-at-risk (OAR) or the tumor. The main input form requries
a frame and two option buttons.
The OAR structure input and the tumor input each have a separate form. Figure
CS25.11 presents the form for the OAR structures. This form uses a tab structure to
allow the user to change the penalty coefficient, the tolerance dose, and the upper
bound for any of the OAR structures. The values and bounds for the scroll bar and text
box controls on the tabs vary per structure. [See Figures CS25.11(a), (b), and (c).]
(a)
(b)
(c)
Figure CS25.11 The OAR input form.
The input form for the tumor uses one set of scroll bars and text boxes to receive the
values for the overdose and underdose penalty coefficients, the prescription dose, and
the lower and upper bounds of the tumor dosage. (See Figure CS25.12).
Main menu form Where the user can navigate to the input sheet to
view or modify input values, the model sheet to
visualize the model, and the output sheet to solve
Summary the problem and see the results.
Main input form Where the user can choose to enter/modify the
input for the OAR structures or tumor.
OAR input form Where the user can use tabs to enter/modify the
input for any of the OAR structures.
Tumor input form Where the user enters/modifies the input for the
tumor.
Navigational “Start,” “View Model Description,” “View Bixel-
buttons Voxel Effect,” “Return to Menu,” “Return to Input,”
“End.”
Functional buttons “Modify Values,” “Resolve.”
CS25.4 Procedures
We will now outline the procedures for this application beginning with the initial sub
procedures. (See Figure CS25.13.) The Main procedure, which is called from the “Start”
button, shows the main menu form. The “Modify Values” button on the input sheet calls
the ModifyInput procedure, which displays the main input form.
The procedures for the main menu form simply navigate the user to the sheet that
corresponds to the selected option button choice. (See Figure CS25.14.) If the user
chooses to solve the model and see the results, then the SolveModel procedure is also
called.
The procedures for the main input form simply show the user the corresponding input
form for his or her option button choice. (See Figure CS25.15.)
There are several procedures for the OAR input form. (See Figures CS25.16, CS25.17,
and CS25.18.) Figure CS25.16 presents the procedures that record the input values
after the user has clicked the “OK” button on the form. Figure CS25.17 presents the
procedures that coordinate the scroll bars and text boxes for all of the input values on all
of the tabs. Figure CS25.18 presents the initialization procedure that sets the default
values for the text boxes and scroll bars on all the tabs.
Figure CS25.16 The first part of the procedures for the OAR input form.
Figure CS25.17 The second part of the procedures for the OAR input form.
Figure CS25.18 The third part of the procedures for the OAR input form.
Figures CS25.19 and CS25.20 present the procedures for the tumor input form. The
procedures in Figure CS25.19 record the final tumor values once the user has clicked
the “OK” button. The procedures in Figure CS25.20 coordinate the scroll bars and text
boxes and initialize the form values.
Figure CS25.19 The first part of the procedures for the tumor input form.
Figure CS25.20 The second part of the procedures for the tumor input form.
The SolveModel procedure solves the model prepared in the model sheet. (See Figure
CS25.21.) The decision variables are entered as a large non-adjacent range; note how
the names of the ranges are provided. The constraints have been grouped together
based on their inequalities so there are only three SolverAdd commands. The result of
the Solver solution is stored in case the input yields an infeasible result.
Figure CS25.22 presents the navigational procedures for the application’s buttons.
“Resolve” button Re-displays the input forms to the user and re-
solves the model.
CS25.6 Summary
¾ This application seeks to solve the IMRT problem by designing a radiation
therapy treatment plan that delivers a specific level of radiation dose, or
prescription dose, to the targets, while sparing organs-at-risk by ensuring that the
level of radiation they receive does not exceed a structure-specific tolerance
dose.
¾ This application requires six spreadsheets: the welcome sheet, a model
description sheet, the input sheet, a detailed input sheet, the model sheet, and
the output sheet.
¾ For this application’s user interface, we use navigational buttons and several
user forms.
¾ Several application procedures collect the input for the model and solve the
model with the Solver commands.
¾ The user can resolve the application by pressing the “Resolve” button on the
output sheet. The user can then modify the input values for any OAR structure or
for the tumor and view the updated results.
CS25.7 Extensions
1. Suggest an alternative way for the user to enter the variable input. Consider
designing one of the sheets to have controls on the spreadsheet.
2. Allow the user to change some of the fixed input values.
3. Create a grid on the output sheet that reflects the “hot” areas from the radiation.
In other words, create a color scheme that reflects high dosage areas on a voxel
grid for all the structures.