You are on page 1of 6

Chapter 2 2- 1

Chapter 2 Form Controls and Random Numbers

Form Controls

There are several ways in which a user may interact with a worksheet in Excel. We have already
been using the most obvious way – entering values, data or formulae into the cells of a worksheet.
While this may seem easy and trivial, sometimes we want a bit more control over how data is entered,
or what kind of data should be entered. For instance, we may wish to ensure that the user enters
only integers within a certain range as input values into a cell, or we may require the user to choose
from a given set of possible inputs.

On an Excel worksheet, it is possible to include features such as scroll bars, spin buttons and list
boxes that help to control the kind of input or interactions required from the user. These are usually
called controls. These controls are found in the ribbon under the Developer tab. Therefore, in order
to use these controls, the Developer tab has to be enabled 1.

To do so, on the Excel worksheet (Excel 2010 and later), click File followed by Options. This
brings up the Excel Options window, from which we would choose the Customize Ribbon option.

Under the “Main Tabs” section on the right of the Customize Ribbon window, select and check the
Developer checkbox. In some cases, the Developer checkbox may not have been added to the
main tabs. In this case, look for it under the list on the left of the Customize Ribbon window, select
it and click the “Add” button to add to the main tabs on the right.

The Developer tab should now appear on your Excel worksheet, and clicking it should bring up a
ribbon as shown below.

1 Note that the Developer tab may have already been enabled in certain versions of Excel during installation.

MSM817 Computing and Programming Techniques K.C.Ang  2018


Chapter 2 2- 2

There are
many commands, buttons and options on the Developer tab, but for now, we shall focus on the
Insert button under the Controls panel. We will use the Insert button to insert a form control.

Clicking the Insert button on the Developer tab invokes a drop-down menu from which one may then
insert a control. In Excel, there are two kinds of controls, known as Form Controls and ActiveX
Controls. Form controls have been included in earlier versions (as early as Excel 97) of Excel and
are simpler to use and implement. They have been integrated into Excel as part of Excel’s suite of
tools. On the other hand, ActiveX controls are more flexible and allow users to customize and re-
program them to suit their needs.

In this chapter, we will use only form controls (in fact, only the scroll bar control).

Scroll Bar

Suppose we wish to change the value of a cell in an Excel worksheet to observe how the value of
this cell may affect other values or portions of the worksheet. This happens quite often especially if
we wish to carry out some scenario modelling or to simulate certain conditions when studying a
problem. Instead of directly typing values into the cell, it may be more appropriate to effect this
change or input of values via a scroll bar. A scroll bar allows you to slide forward and backward,
and change the associated value accordingly.

To create a scroll bar in Excel, click the Insert button on the Developer tab, and then select the Scroll
Bar in the Form Control section.

Scroll Bar (Form Control)

MSM817 Computing and Programming Techniques K.C.Ang  2018


Chapter 2 2- 3

Having selected the control, one may then


“draw” the required scroll bar on the sheet.
If we wish to use this scroll bar to control
Cell A1, then it would be logical to place
the scroll bar somewhere near the cell as
shown. At the moment, the scroll bar does
nothing as it is not associated or linked to
any cell.

To link the scroll bar to a cell (say, A1), first


right-click the scroll bar to bring up the
scroll bar Edit menu.

From the drop down menu, select the


Format Control … option.

A Format Control window will then appear


and this will allow the user to set certain
values or fields under the various tabs.

Perhaps the first thing that should be set is


the “Cell link” value under the “Control” tab.
If we wish to control the value of cell A1
using the scroll bar, we may either enter
the cell address (in this case, using an
absolute address “$A$1”) or click the
“select cell” button (circled) and then click
the cell A1 to select it.

Click OK to accept the changes.

Scroll bars are useful in controlling input to a cell. We may set other options in the Format Control
window such as the maximum and minimum values, and incremental and page changes. An
incremental change is the change in value when one clicks on the ends (left or right arrows) of the
scroll bar, while a page change is the change in value when the space on the scroll bar is clicked.

Click here to increase Click to increase value by


value by page change incremental change

MSM817 Computing and Programming Techniques K.C.Ang  2018


Chapter 2 2- 4

Example 2.1 Boxes (Again)

Modify the “Boxes” worksheet (in Example 1.2 from Chapter 1) so that now, instead of entering the
values of m and n in the worksheet, the user may now use two scroll bars to control the values of m
and n.

Example 2.2 Shortest Distance (Again)

Consider the line L given by 𝑦𝑦 = 𝑥𝑥 + 1, and a given point P. Plot the line and the point on an
Excel worksheet. On the worksheet, add two scroll bars to vary the coordinates of P. Use
another scroll bar to vary the position of a point on the line. Calculate the distance between
this point on the line and P, and the angle between the line joining these two points and the
original line L. Your worksheet should look something like this:

Use your worksheet to find the shortest distance between point P and line L.

Example 2.3 Quadratic Curve

Use three scroll bars to vary the coefficients of a quadratic expression, 𝑦𝑦 = 𝑎𝑎𝑥𝑥 2 + 𝑏𝑏𝑏𝑏 + 𝑐𝑐, and plot
the resulting graph with a scatter plot (with smooth lines).

Example 2.4 Slope Slider

Construct a “slope slider” on an Excel


worksheet. Two scroll bars are used to
vary the slope and y-intercept of a
straight line, which is plotted using
scatter plot.

A third scroll bar varies the position of a


point on the line, and slides up and down
the given line.

A typical screenshot is shown on the


right.

MSM817 Computing and Programming Techniques K.C.Ang  2018


Chapter 2 2- 5

Random Numbers

It is not easy to provide a precise definition of a random number. However, for our purpose, we may
consider a random number as one selected purely by chance from some distribution. If the
distribution is not specified, it is normally assumed to be a uniform distribution. For instance, suppose
we consider the integers in the interval [1, 10] as being distributed uniformly. Then, if we are to
randomly pick an integer from the interval [1, 10], then every one of the ten integers would have an
equal chance of being picked. The concept of randomness lies in the fact that we cannot quite
predict which one of the integer will be picked, and thus refer to it as a random(ly chosen) number.

Another way to think of random numbers is to consider a sequence of numbers, 𝑥𝑥1 , 𝑥𝑥2 , 𝑥𝑥3 ,…. If there
appears to be no pattern leading from one number to the next, we can say that these numbers are
randomly generated.

Random numbers can be very useful in solving or investigating certain types of problems. For
instance, they have been applied in generating data encryption keys in cryptography, as well as in
simulation models in mathematical modelling and in problems solving involving the use of the Monte
Carlo method.

Many computers now have what is known as a “random number generator” 2. A random number
generator basically runs a procedure that gives a user a “random number”. These numbers are
usually generated based on some algorithm or rule, and the numbers appear to be randomly chosen.
In reality, because there are rules governing the generation of these numbers, they are not truly
random. In fact, we frequently call them “pseudo-random numbers”. However, for all our intents
and purposes, these computer generated random numbers are usually good enough for us to use
in our problem-solving.

Excel has a built-in random number function, rand(). This function works like any other function in
Excel and returns a random number between 0 and 1. To use it, one simply enters the function as
a formula in a cell (e.g. Cell A1) as shown in the screenshot below.

Once the formula is entered and the RETURN key is hit, a number between 0 and 1 is randomly
chosen and placed in Cell A1. Pressing the function key F9 produces a new random number. One
can assume that every number inside the interval (0, 1) and that may be represented by the
computer has an equal chance of being selected.

2 In fact, many handheld calculators possess such a function.

MSM817 Computing and Programming Techniques K.C.Ang  2018


Chapter 2 2- 6

Another Excel random number function is the randbetween(m,n) function, which returns a
random integer in the interval [𝑚𝑚, 𝑛𝑛]. Note that 𝑚𝑚 and 𝑛𝑛 need not be integers but the condition 𝑚𝑚 ≤
𝑛𝑛 must hold in order for the function to be usable.

Example 2.5 Sum of squares

What is the probability the sum of the squares of two numbers randomly chosen from the interval
[0, 1] is less than 1? Use Excel’s random number and the countif functions to construct and carry
out an experiment to estimate this probability.

Example 2.6 1-D Random Walker

Suppose a man walks along a straight line, starting at some origin. Each time before he takes a
step, he flips a coin; if the coin shows heads, he takes a step to the right and if it shows tails, he
takes a step to the left. Simulate this random walk for a 500-step walk on an Excel worksheet and
record the final position of the man at the end of his walk.

Example 2.7 1-D Random Walker with counting

Modify the simulation in Example 2 by considering a 100-step random walk, running it many times
and then constructing a frequency table for the final position. Create a frequency chart for the final
position. What do you observe?

MSM817 Computing and Programming Techniques K.C.Ang  2018

You might also like