Вы находитесь на странице: 1из 24

Appendix

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).

Installing Excel Features H.2


We assume that you already have Excel installed on your personal computer (PC)it
comes with many different versions of Microsoft Office. If not, you may purchase a
stand-alone copy or a version of Office that includes it (and note that your college
bookstore will have heavily discounted academic versions available!); simply follow
the default instructions for installation.
Although spreadsheets were initially developed for use in business for such things
as tabulating and analyzing financial data, they are now so computationally powerful
that engineers and scientists are increasingly using them. Because the majority of
users are still people such as accountants, the standard or default installation of Excel
does not include some features that engineers and scientists find useful. (In a similar

w.33

w.34

Appendix H A Brief Review of Microsoft Excel


way default installation of the word processor Microsoft Word does not include the
equation-creating component, which in Word is called the Equation Editor.) As an
engineer you will find useful some features of Excels Analysis Toolpak, Visual Basic
for Applications (VBA), and we will use its Solver, so we will install them. The
Analysis Toolpak has some useful mathematical features such as regression analysis;
VBA is used for creating custom functions; the Solver will be very useful for solving
equations, optimizing a function, and so on.
To install these, use the following steps (again, based on Office 2003the procedure with other versions will vary slightly):
 Quit all programs.
 On the Windows Start menu, point to Settings, and then click Control Panel.
 Double-click the Add/Remove Programs icon.
You will obtain a window similar to the one below.

Double-click on the Add or Remove Programs icon to obtain a window similar to

H.2

Installing Excel Features w.35

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

Appendix H A Brief Review of Microsoft Excel

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

Excel Fundamentals w.37

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.

Excel Fundamentals H.3


Excel is an application for manipulating and performing calculations on data that are
in a tabular format. Originally developed for financial calculations that lend themselves to this format, Excel is also an extremely powerful tool for performing engineering calculations, whether or not data are tabular.

Entering Data into Cells


To enter data (numeric or text), simply click on a cell or use the arrow keys on
the keyboard to maneuver to the cell of interest, and type in the data. To conclude the
entry, press the Enter key. We can make some comments here:
 You can do only limited editing while entering data into a cell: You can use the
Backspace key to delete, but cannot use the left arrow to move to earlier digits or
letters (using this or any other arrow key leaves the cell).
 Excel internally recognizes whether a cells contents are numeric or text, and by
default left-aligns text and right-aligns numbers.
 Spaces should not be used in entering a number (e.g., 123 456) because Excel will
interpret this as text. Commas are allowed (but are not necessary) (e.g., 123, 456).
Excel will use a general number format for numerical data; if the number is too large
to fit in a cell it will change to scientific format, e.g., 1.23E45 (or an error format
######, which can be changedsee below for how to widen columns). The font and

w.38

Appendix H A Brief Review of Microsoft Excel


many other formatting properties of cells can be done by selecting the cell or cells and
then using the Formatting toolbar:

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.

H.4 Entering Formulas into Cells


The real power of Excel is its computational abilities. This involves creating formulas
in cells for automatic calculation. As an example, suppose we wish to compute any
number to any (reasonable!) power, e.g., 23, 121.5, e2, etc. The crude way is to type in
a new formula each time we have two new numbers. Lets do this first. The key rule
in Excel is that you must use the equals sign ( 5 ) at the beginning of a formula so that
Excel knows its a formula and not numeric or text data. Hence to compute 23, select a
cell and type 5243 and press Enter (the exponential is created with 4). The result (8)
will be displayed. The disadvantage of this formula is that we need to retype the formula
for each new computation, e.g., for 121.5 we would need to type 51241.5 in a cell. A
much better approach is to use formulas with cell references.
A cell reference is simply the cell coordinates of the cell. For example, type 2 in cell
B3 and 3 in cell C3; B3 and C3 are cell references. Now, in cell D3, instead of
computing 23 by typing 5243, we can type 5B34C3. Excel interprets this formula to
say take the contents of cell B3 and raise it to the value of cell C3, and displays the
result. You should have the following:

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

Entering Formulas into Cells

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):

A formula on one worksheet can contain references to cells on other worksheets. To


do this you would start creating the formula, and when you needed to reference a cell
on another worksheet, still in formula creation mode, click on the appropriate tab,
then the cell, and then continue typing the formula.
We can copy and paste cells using any of the standard Windows techniques such as
using menu items, right-clicking, etc., and Excel has additional methods [1], [2]. When
we copy and paste a cell containing a formula, we need to be careful about how Excel
interprets the pasting. Excel pastes the logic of the formula, not the specific cell references. For example, if we copy and paste our cell D3 to cell D5, we get (depending on
what else you have done to the worksheet), 0 or #NUM!, or some other result, not 8!
If you click on cell D5 to see the formula, you will see that instead of the original
formula 5B34C3 we have 5B54C5. Excel has copied the logic, which in this case is
take the contents of the cell two cells to the left and raise it to the power of the
contents of the cell one cell to the leftwhich in this example doesnt compute
because in cells B5 and C5 we currently have zeros, and 00 is undefined. You only begin
to get a result that makes sense if you now type numbers into cells B5 and C5.
In many cases we want this kind of pasting to happen. For example, suppose we
wish to compute the mass of air in a container when the air is pressurized over a range
of pressures. The formula for this is
m5

--pV
RT

H:1

--- is the container volume, T is the air temperature, and R 5


where p is the pressure, V
--- 5 1 m3 and T 5 20 C 5 293K, and
287 J/kg-K is the air gas constant. Suppose V
p varies from 10 to 100 kPa in steps of 10 kPa.
Hence,
m5

1
p
287 3 293

H:2

In this formula p is the pressure in Pa and m will be the mass in kg.


We can start a new worksheet (e.g., by clicking on the top left icon on the toolbar or
by using menu item File . . . New) and type in column headings for the pressure and
mass, and enter the pressure data (and note that there are ways in Excel to automatically fill in the p sequence, and that weve done some formatting, and added
borders):

w.39

w.40

Appendix H A Brief Review of Microsoft Excel

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

Entering Formulas into Cells

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

Appendix H A Brief Review of Microsoft Excel

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:

Lets see how we can now graph the data we computed.

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

Entering Formulas into Cells

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

Appendix H A Brief Review of Microsoft Excel

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

More Advanced Features

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.!).

More Advanced Features H.5


Excel has a number of more advanced features. We will consider several of them:
Goal Seek, Solver, use of macros, and creation of custom functions.

Goal Seek and Solver


Many problems in engineering end in an equation or equations to be solved for an
unknown or unknowns. In some cases the equation or equations are not directly
solvable, and we need to use numerical techniques to obtain a solution. For example,
suppose we wish to solve the following equation for x:
ex/2 5 x2

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

Appendix H A Brief Review of Microsoft Excel

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

More Advanced Features w.47

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

Appendix H A Brief Review of Microsoft Excel


Finally, lets consider an example of two variables:
fx; y 5 x2 2 6x 1 y2 2 5
Suppose we wish to find its minimum value. We can set up a new worksheet, with cells
for x and y (guess values of 0), and a formula cell (in the worksheet below the formula
is 5B34226*B31C34225):

Then all we need to do is use Solver with the following information:

The solution is f(x, y) has a minimum value of 214 when x 5 3 and y 5 0.

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

More Advanced Features


H:6

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

Appendix H A Brief Review of Microsoft Excel

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:

More Advanced Features w.51

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

Keyboard Shortcut: Ctrl 1 i

Range(D6).Select

w.52

Appendix H A Brief Review of Microsoft Excel


Selection.Copy
Sheets(Calculations).Select
ActiveWindow.SmallScroll ToRight: 5 25
Range(D7:D17).Select
ActiveSheet.Paste
Range(J7:J27).Select
ActiveSheet.Paste
Range(P7:P47).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight: 58
Range(V7:V67).Select
ActiveSheet.Paste
Sheets(Results).Select
Range(D10).Select
Application.CutCopyMode 5 False
End Sub

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

More Advanced Features w.53

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

Appendix H A Brief Review of Microsoft Excel


unit system conversions such as lb to kg), but also such things as complex math, and
advanced functions such as Bessels functionsExcels Math & Trig group does not
contain these last two items but does contain equally advanced functions such has
hyperbolic functions! When searching for a function in Excel, it is a good idea to
search under All functions so you do not accidentally fail to find a built-in function.
Even with this impressive collection, there will be occasions when you wish to
define your own functions. This can be easily done in Excel. We will illustrate the
method for defining a customized function by considering the following example.
Suppose we wish to compute the friction factor f for pipe-flow problems:
f 5

64
Re

2:51
p 5 22:0 log
p
1
3:7
f
Re f

Re , 2300 Laminar flow

H:7a

Re $ 2300 Turbulent flow

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

More Advanced Features w.55

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

Appendix H A Brief Review of Microsoft Excel


The code works as follows:
 We first perform the test for Reynolds number.
 If Re , 2300, we compute f from Eq. H.7a and go to the end of the function.
 If Re $ 2300, we go to the Else line and define the error (Er) and initial values for
fn (oldf) and f(deltaf), the error between successive values of f. Between the
Do While and Loop lines we iterate to converge on an accurate value of f. For each
iteration:
 We compute a value for fn11 (newf) from the current value of fn and Re and
(represented by Re and e, respectively). [Note that we multiply the log by
0.434294482 because Visual Basic treats Log as a natural log, so we convert to
base 10 by multiplying by log(l0)/ln(10).]
 Next we compute the change f (deltaf) in the value of f, and update the value
of f.
 Finally, we return to the Do While line to perform the accuracy test: If jf/ f j $
0.1% we repeat the iteration; if jf/f j , 0.1% we finish iterating and output the
value of f.
We can now close the Visual Basic window and return to the spreadsheet. We now
have a new function f that computes Eq. H.7a or H.7b as appropriate! We can test the
function for laminar (Re , 2300) and turbulent (Re $ 2300) flows, e.g., something
like:

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.

3. McKeown, P. G., and C. A. Piercy, Learning to Program


with Visual Basic, 2nd ed. New York: John Wiley, 2001.

Вам также может понравиться