Академический Документы
Профессиональный Документы
Культура Документы
A Brief Review
of Microsoft Excel
Introduction H.1
Microsoft Excel is one of several available spreadsheet applications. All of these
spreadsheets work in a very similar fashion, with minor variations in syntax and builtin features, so that most of our discussion of Excel will be directly applicable to other
spreadsheet applications.
There are several versions of Excel in widespread use (e.g., Excel 2010, Excel 2003);
we will use Excel 2003.
In this brief review we will discuss such fundamental features as how to create
formulas, how to format cells, how to create graphs, and so on. The review of these
fundamentals will presuppose that you are familiar with the Windows environment,
and it will be briefmore detailed explanations are available from a number of
sources, e.g., [1], [2], as well as from Excels built-in Help. We will then go on to
explore use of Excels Goal Seek and Solver features, and more sophisticated, less
commonly used features such as use of macros and custom functions using Excels builtin Visual Basic for Applications (VBA).
w.33
w.34
H.2
Next, scroll to Microsoft Office 2003 (or Microsoft Excel if thats all you installed) and
click on Change to obtain a window similar to
Select Add or Remove Features and click on Next to get a window similar to
In this window, a check will appear next to applications currently installed. We wish to
customize our Excel (probably Word, too!), so check the Choose advanced customization of applications checkbox and click on the Next that now appears to get
something like
w.36
Expanding the Excel tree, we can select the Add-Ins that we wish to install, as illustrated below.
You should select Run from My Computer for the Add-Ins you want, and then
click on Update. (You might also want to look at Office Tools and select Equation
Editor for use with all your Office applications!) At this point you may or may not be
asked for your original Office CD; in any event, simply follow the instructions to
complete the installation.
The add-ins should now be available the next time you use Excel; depending on the
version, you may first have to restart your computer.
H.3
To proceed with this brief review, you should have Excel running. It will look
something like:
Note that your collections of icons may be different. You can view, hide, or customize
toolbars (collections of icons) using the same procedure used for other Office applications.
w.38
or by right-clicking and choosing the cell-formatting option. To select several contiguous cells, simply drag over them with the mouse (or hold down the Shift key while
using the arrow keys); for noncontiguous cells, hold down the Ctrl key while selecting
with the mouse. If a text data entry is too large to fit into its cell, it will appear to spill
into the cell to the right. If you have several cells with text data, Excel will display
them as if they were a deck of cards. For example, the words Data in a cell typed
into three adjacent cells looks like:
even though Excel still remembers all three cells contents. There are several ways to
widen a cell to display the full contentsone is to select the cell or cells and click on
menu item Format . . . Column . . . Autofit Selection.
Once data have been entered in a cell (i.e., after pressing Enter), to edit the data
simply double-click on them, or select the cell and press F2. You will now be in edit
mode and can use the left and right arrow keys, the Del and BackSpace key, etc., and
finish by pressing Enter.
We have a live worksheetwe can type new numbers into cells B3 and C3 and get,
after pressing Enter, the result immediately (e.g., 12 in B3 and 1.5 in C3 should
produce 41.56922 in D3).
Typing formulas with cell references can get tedious and can produce errors,
especially when there are many such references. A very useful mouse technique is to
H.4
start creating a formula, and instead of typing in a cell reference, use the mouse to
click on the cell you wish to reference. As long as you dont press Enter, Excel will
remain in this formula creation mode, and you can continue typing the rest of the
formula, using the mouse to click on additional cells while building the formula. For
example, we can select cell D3 and replace its current contents by typing 5, then
clicking on cell B3 with the mouse, then typing 4, then clicking on cell C3 with the mouse,
and nally pressing Enter (you can also use the arrow keys for this kind of formula building).
This is obviously a trivial example, but very complex spreadsheets can be developed in
which we have multiple formulas with multiple cell references. In addition, a workbook can have multiple worksheets, indicated by the tabs (and you can add more,
rename them, and so on):
--pV
RT
H:1
1
p
287 3 293
H:2
w.39
w.40
We can now create Eq. H.2 in cell C4, for the mass due to the first pressure: The
formula should be 51/287/293*B4. (Note that Excel follows the conventional precedents of exponentiation, then multiplication/division, then addition/subtraction, etc.)
We could then create similar formulas for the other cells; e.g., cell C5 should have 51/
287/293*B5. A better approach is simply to copy cell C4 into cells C5 through C13.
The result is (after a little formatting):
H.4
We have just introduced what is called the idea of a relative referencethe reference
is to a cell relative to the cell containing the formula. Sometimes we want instead an
absolute reference. This is a reference to a fixed cell, so that if the formula is copied to
another cell the reference does not change. An example in which this would be
desired is as follows: Suppose we wanted to be able to repeat the above computations
for a different temperature or container volume. We want to have cells containing
these so that we can easily change them. Lets put them in cells E4 and F4 (with
titlescan you create m3?):
We now need the following formula in cell C4 (corresponding to Eq. H.1): 5B4*E4/
287/F4. The problem now is that if we copy this formula to the other cells in the mass
column, we will get errors (try it, then click Undo!): The formula logic says that
each cell should obtain the volume and temperature data two and three cells to
the immediate right, e.g., cell C5s formula as pasted would be 5B5*E5/287/F5. The
reference to B5 (the pressure) is correct, but E5 and F5 refer to blank cells! What we
wish to have in cell C5 is 5B5*E4/287/F4, i.e., we wish to copy and paste our
formula, but not have the E4/287/F4 part change. To do this we make these
references absolute. Before copying our original cell C4, we edit it (e.g., by doubleclicking on it) and place $ signs as follows: 5B4*$E$4/287/$F$4. Excel understands
that any cell reference that has the $ sign in front of it is absolute, and does not change
it when copied and pasted. Note that E4, for example, has two $ signs, so that neither
E nor 4 will change. (Strictly speaking, in this example E does not change anyway, so we
don't really need to $ it). If this formula is copied and pasted to the mass column, we get the
correct results: Each computation obtains the pressure from the cell to the left, and the volume
and temperature from cells E4 and E5. We can now change the temperature to, say, 100 C 5
373K, and obtain new data (don't worry about the circle and square shown belowwe'll
discuss them shortly):
w.41
w.42
Note that Excel will automatically create the $ signs in a reference if you press the F4
key during either formula creation or editing.
Our last comment on formula creation is that Excel has many mathematical
functions built in, but you need to use the correct syntax and spelling. For example, to
create 2, you must type 2*pi(). Many functions are obvious, e.g., to obtain sin(/4)
type sin(pi()/4); others are less so (e.g., !). Note that trigonometric functions in
Excel use arguments in radians by default.
You can always search for functions while creating a formula by clicking on the
Paste Function icon (circled above) to get a useful menu:
Creating Graphs
Excel has powerful graphing features. There are a number of ways to access these, but
the main decision is whether you select the data to graph first and then go into
graphing mode, or vice versa. Both approaches work, but beginners will probably find
it easier to start graphing mode without first having selected the data. As in all
H.4
Windows applications, there are several ways to perform any action; here the simplest
is to click on the Graph Wizard icon (squared in the spreadsheet image shown above).
If we do this we get the first of four self-explanatory windows:
Most of the time engineers will plot x-y data using XY (Scatter) (not Line, which
forces data to appear uniformly on the x axis!). There is nothing to preview yet so
click Next to get:
Click in Data Range and then drag the entire window out of the way a little as
necessary so that you can select the range to plot (or alternatively click on the little
icon in Data Range on the right to go directly to the worksheet); select the cells
containing our data (cells B4 to C13); and proceed with the rest of the steps. We end
up with a graph something like:
w.43
w.44
It is not too interesting! It can be improved a lot by customizing the trace, adding
titles, etc. To do this we can right-click on any graph feature (e.g., a data point) for a
menu of options, and we can also use the Formatting toolbar, and end up with
something like:
We have created a basic x-y graph: Excel can create many sophisticated graph types
such as bar charts, pie charts, radar plots, and surface plots. In addition, it can be used
to fit a curve to experimental data. We will not do this here, but it involves: plotting
the data on an x-y plot; right-clicking any point on the curve to get a menu that
includes Add Trendline . . . ; selecting this (leading to the window shown next);
choosing the type of curve (Linear, Logarithmic, etc.), and specifying options with the
Options tab; clicking OK and observing the results.
H.5
This concludes our introduction to Excel basics. As you use Excel you will discover
many features as the need arises, and the references have additional help. We
mention one final feature to illustrate the usefulness of some of these features.
We earlier copied and pasted a formula from a single cell to a column using conventional Windows copy and paste techniques. Excel has an additional convenient
shortcut for this: Simply click once on the cell to be copied, then move the mouse
(without clicking) over the cells lower right corner until the Excel pointer changes
to a fill handle (1); you can then press and drag down a column (or a block of
cells) to immediately copy the cell contents. Even neater is that if you have two or
more cells with contents that make a sequence (e.g., 12, 24, or Jan, Feb) and select
them, then move the mouse to the lower right to get the fill handle (1) and finally
drag down or across, Excel will fill with the sequence (e.g., 36, 48, . . . etc., or Mar,
Apr, . . . etc.!).
H:3
We cannot solve explicitly for x. We could make guesses for x until we converged on a
solution, or we could use one of several classic methods to converge to a solution in
a more systematic way. Most scientific calculators, and certainly all spreadsheets,
including Excel, also have equation solution techniques built in.
To use Excels Goal Seek feature, we first need to convert Eq. H.3 into a function,
the root of which we need to find:
f x 5 ex/2 2 x2
H:4
The problem then becomes, find the root (or roots) of f(x). While not necessary for
using Goal Seek, the best procedure for this is first to plot the function over a range of
x values to see the locations of the roots. After some experimentation with x ranges,
we obtain, using Excel
w.45
w.46
It looks like we have roots at approximately x 5 20.8 and x 5 1.4. We would like more
accurate answers. To use Goal Seek, set up another table containing a single x and f(x):
Note that cell B23 contains 0.0, an arbitrary starting point, and cell C23 contains
the formula 5EXP(B23/2)-B2342. Goal Seek is accessed by using menu item
Tools . . . Goal Seek . . . , which brings up the following dialog box:
This dialog box is almost self-explanatory: Click on the Set cell: window, then on the
cell we wish to find the root of (C23); click on the To value: window and type 0; click
on the By changing cell: window and click on the cell we wish to vary to find the root
(in this example, B23); click OK. If Excel can find a root, it tells us so:
H.5
and we have:
We can repeat this process by typing in another guess value for x (try 1) to find the
second solution (x 5 1.4296). When using this method you sometimes have to try
several guess values in order to find all the roots (in this example there is a third
root . . . can you find it?)and of course, as with all numerical methods, sometimes it
simply fails.
Note that we can also use Goal Seek to find an x at which f(x) attains any given
value, e.g., when does f(x) 5 2?
Goal Seek is quick and easy. A more powerful feature is Solver. Solver is accessed
by using menu item Tools . . . Solver . . . , which brings up this dialog box:
As for Goal Seek, we can still select an x cell that we will allow Excel to vary in order
to make a formula-connected f(x) cell attain a certain valuebut we can do much
more! We can
Find the minimum or maximum of f(x).
Find the roots (or any other value) of a function of several variables, e.g., f(x,y),
f(x,y,z) etc.
Do all of the above while applying constraints, e.g., while x . 2 and y , x2.
The window is fairly self-explanatory, so we will not go into the details here. We will
demonstrate its use by finding the maximum of our function f(x) in Eq. H.4, graphed
earlier. In the Set Target Cell: window select cell C23; for the Equal To: select Max;
click on the By Changing Cells: window and click on cell B23; click Solve. We end up
with:
w.48
Macros
A macro is nothing more than a set of instructions for automating a sequence of
actions in Excel (or Word or any number of other applications). We will illustrate the
use of macros by considering a fairly complicated but ultimately very useful example:
We will create a worksheet that can be easily used to numerically evaluate a definite
integral. The formula we will use to approximate the definite integral is a form of
Simpsons Rule:
Z
I5
x
f x0 1 4f x1 1 2f x2 1 4f x3
3
a
1 2f x4 1 . . . 1 4f xN21 1 f xN
b
f xdx
H:5
where N (which must be even) is the number of segments of size x 5 b 2 a/N that
the range (b 2 a) is divided into, and x0 5 a with xi11 5 xi 1 x, leading to xN 5 b.
Within limits, the accuracy of the evaluation increases with N. As a good check, we
would like to have the worksheet automatically evaluate the integral for N 5 10, 20,
40, and 60 segments.
Before we automate this task with a macro for use with an arbitrary integral, lets
see how we create such a worksheet entirely from scratch for a specific integral.
Suppose we wish to numerically evaluate the following:
H.5
Z
I 5
sinx dx
Obviously we can easily evaluate this analytically and find I 5 2; we can use this result
to test whether or not our worksheet is working. The following worksheet shows the
calculations for N 5 10, and part of the calculations for the other N values:
Lets discuss in detail the construction of the calculations for N 5 10 (the others are
very similar). In cell C4 we compute the value of x: The cell contains the formula
5pi()/10. The first x value is generated by entering the lower integration limit (0), and
the other x values are created with formulas that add x to each previous x value;
e.g., cell C8 has 5C71$C$4. The f(x) column contains the integrand formula,
with x assumed to be in the cell to the left; e.g., cell D7 has 5sin(C7). The w
column contains the weightings 1, 4, 2, 4, 2, . . . 4, 1 used in Eq. H.5, and the
Prod. column contains the products of the f(x) and w columns (e.g., cell F7 contains
the formula 5D7*E7). We now need to sum the Prod. column, shown in cell F18 (it
contains the formula 5SUM(F7:F17)), and this is the series in brackets in Eq. H.5.
Finally, the integral I is given in cell D21 (it contains the formula 5C4/3*F18). The
value we obtain for the integral, 2.00011, is very close to the exact result of 2.
This entire procedure needs to be done for the other computations, for N 5 20, 40,
and 60 (with obvious changes, such as the computation of x). Your four values of I
for Eq. H.6 should agree closely.
We now wish to modify the workbook so we can evaluate other integrals (different
integrands and limits).
First, we can tidy up the entire workbook by relegating these calculations to a
secondary Calculations worksheet (double-click the tab to edit its name; click and
drag the worksheet tab to move the worksheet), and create a user-friendly Results
worksheet as shown next:
w.49
w.50
In this worksheet we enter the formula for the integrand, assuming x is to the left (in the
present case 5sin(C6)), and the lower and upper limits, as shown. The Calculations
sheet must be modified so that x in four locations is now automatically calculated
from the limits on the Results sheet (e.g., cell C4 now contains 5(Results!D11Results!D10)/10), and the first x value in each column is now computed from the
lower limit (these cells contain the formula 5Results!D10).
The workbook is now almost finished. To use it to evaluate the integral, we must
copy the formula for f(x) in cell C6 from the Results sheet and paste it to the four f(x)
columns in the Calculations sheet. Finally, the four cells in the Results sheet showing
the integration results contain formulas for picking up these results from the Calculations sheet (e.g., cell F15 contains the formula 5Calculations!D21). If all goes
according to plan, the four evaluations appear as shown above.
It took quite a bit of effort, but we now have a workbook for numerically evaluating definite integrals! The workbook is fairly easy to use to evaluate an integral. We
use the following steps:
1. Enter the integrand formula in cell D6, assuming x is to the immediate left.
2. Enter the lower and upper limits.
3. Copy the formula in cell D6 to the four f(x) columns in the Calculations sheet, and
return to the Results sheet to view the answers.
This procedure works: You can test whether your worksheet is working correctly by
trying the following integral:
Z
I 5
x2 ex dx
0
You should get the following results (the exact result is e 2 2 5 0.71828):
H.5
10
20
40
60
segments:
segments:
segments:
segments:
0.71830
0.71828
0.71828
0.71828
We are now (finally!) ready to see how we can use a macro to automate this procedure. Steps 1 and 2 are straightforward, but step 3, involving a lot of copying and
pasting from one sheet to another, is tedious and prone to error. We can automate this
step by creating some Visual Basic code. If you are familiar with Visual Basic you can
write your own code, but Excel has a built-in macro-recording feature that will generate the code for you. The idea is that you switch on macro recording, and then
perform exactly the actions you wish to record that accomplish a task, and finally
switch off recording. Lets do this now.
Click on menu item Tools . . . Macro . . . Record New Macro . . . to get the following
window:
You may type in a name if you wish (e.g., Integration) and even assign a shortcut key
(such as Ctrl 1 i), then click OK. You will now be in macro-record mode, as indicated
at the lower left of the screen:
You should be careful now because every action will be recorded! Select and copy cell
D6 on the Results sheet (try Ctrl 1 C); switch to the Calculations sheet, select the f(x)
column for N 5 10, and paste (try Ctrl 1 V); select each of the other f(x) columns and
paste; to end up at the Results sheet, click on it and select a convenient cell, e.g., the
lower limit cell D10. Now stop recording, by selecting menu item Tools . . . Macro . . .
Stop Recording . . . . We have our macro! Your code should look something like the
following (to see it, use menu item Tools . . . Macro . . . Macros . . . and select your code
for editing; close the window when youre done):
Sub Integrate()
Integrate Macro
Macro recorded 12/6/02 by Philip J.
Pritchard
Range(D6).Select
w.52
The code is almost self-explanatory and shows the steps used to accomplish our task. If
you make a procedural error you can simply stop recording and use menu item Tools . . .
Macro . . . Macros . . . where you can delete the macro and start over.
How do we use this macro? Whenever we enter a new integrand in cell D6, we can
run the macro by using menu item Tools . . . Macro . . . Macros . . . and selecting and
running our macro. The macro will do all the copying and pasting for us! A more
convenient procedure is to assign an accelerator keystroke combination to run the
macro (the code above runs whenever Ctrl 1 i is typed). An even better idea is to
create an object such as a picture or WordArt piece of text (click menu item Insert . . .
Object . . . and choose WordArt); we can then right-click on it and choose Assign
Macro . . . and assign our macro to the object. Then, whenever you click on the object
the macro will run!
Before finishing this example, we can protect the Results and Calculations sheets
(after unlocking cells we wish to be able to copy and paste to, or enter data into) so a
future user is not able to wreak havoc with the formulas we have hard-wired, and
hide the grid lines, etc., and end up with something like the sheet shown next. To use
this worksheet we use the following steps:
1. Enter the integrand formula in cell D6, assuming x is to the immediate left.
2. Enter the lower and upper limits.
3. Click on the Integrate! icon (or type, if you created them, the shortcut keys, e.g.,
Ctrl 1 i).
The user does not need to worry about complicated copying and pasting procedures, and
has a very convenient integrator! Most of the needed formulas are hard-wired, and the
needed copying and pasting is done by our macro.
H.5
In this example we saw how to create a simple macro. The potential of macros is
obviousyou can include automatic graph generation, file saving, etc.in fact, virtually
any actions you can perform in Excel can be recorded and hence made into a macro.
Customized Functions
Excel comes with a plethora of useful built-in functions, including many mathematical,
logical, database, and statistical functions. To see the list of available functions, click
the menu item Insert . . . Function . . . (or click on the Paste Function icon we discussed earlier) to get a window like the following:
Note that we have scrolled down to the collection of Engineering functions, which
Microsoft has apparently decided should contain not only obviously engineeringoriented functions such as CONVERT(number,from_unit,to_unit) (for performing
w.54
64
Re
2:51
p 5 22:0 log
p
1
3:7
f
Re f
H:7a
H:7b
where is the pipe relative roughness and Re is the Reynolds number of the flow. If
Re , 2300, the computation is simple; for Re $ 2300 we have a slight problem,
because f is implicit in Eq. H.7b. To solve Eq. H.7b for f we need to iterategiven
and Re, we guess a value for f (e.g., 1) on the right side, and compute f on the left.
This value is fed back into the right side, and the computation is repeated, until
convergence to a suitable accuracy is obtained.
Lets define a function in Excel that computes f from given values of and Re,
using Eq. H.7a or Eq. H.7b as needed. (Note that we could just define a function
that solves Eq. H.7b, and then use a cell containing an If function to decide which
equation to use, but it is neater to define a function that incorporates both equations
automatically.)
In Excel we use its built-in Visual Basic editor to write the code for a function. To
access this, use menu item Tools . . . Macro . . . Visual Basic Editor, to get a window
looking something like:
Note that your window may be slightly different, showing more or fewer subwindows
you can make subwindows visible or not using menu item View. You may or may not
have a Module subwindow visibleif not, create a module (a storage area for code)
using menu item Insert . . . Module.
We now need to create code for computing f from Eq. H.7a or H.7b. A possible
flowchart for the logic of the computation is:
H.5
Note that we have two decision boxes: The first is obvious; the second involves iterating Eq. H.7b until a suitable convergence has been obtained. We will iterate
Eq. H.7b until the change in f is less than 0.1%.
We need to write Visual Basic code for this flowchart. In this introduction to Excel,
we cannot discuss in detail the Visual Basic syntax and grammar we must use to
accomplish the tasks described in the flowchart, but there are several texts available, e.
g., [3]. However, next we present the code we need, which you can now type into the
module subwindow (and note that as we type the first line the Declarations tab
changes to f, our new function):
w.56
The procedure we just outlined showed how to create a custom function in Excel. We
demonstrated this with a function that was fairly complicatedEqs. H.7a and H.7b,
with two decision branches neededbut the procedure can obviously be used for
defining simple functions, or ones of unlimited complexity.
References
1. Gottfried, B. S., Spreadsheet Tools for Engineers Using
Excel, 3rd ed., New York: McGraw-Hill, 2007.
2. Bloch. S. C., Excel for Engineers and Scientists, 2nd ed.,
New York: John Wiley, 2003.