• Aucun résultat trouvé

Ordering Up Your Results

Dans le document Microsoft Excel 2010 (Page 70-78)

Digression number 2. There’s another tricky set of rules you will need to understand—or review, as the case may be—because you probably had to put up with some of these in school: the order of operation.

For example, what’s the answer to this formula?

=4*5+7

It could be 27—that is, 4x5 plus 7—or 48—4x12. Which is it? In this particular case you can resolve the problem by surrounding the relevant numbers with parentheses, e.g.,

=(4*5)+7 Or

=4*(5+7)

You get the idea. Excel resolves this kind of ambiguity with a set of orders of operation—a kind of priority listing which declares which operation takes precedence—that is, is calculated first—in a formula. (In the cases above, the values flanked by parentheses are treated as a unit.) The order reads like this: Let’s illustrate this hierarchy with a few cases. This formula:

=4+5/2

results in an answer of 6.5. It divides 5 by 2 and then adds 4—because priority goes to division over addition.

This formula:

=4*5/2

results in 10, because the multiplication—4 times 5—is carried out first. That result—20—is then divided by 2.

This formula, however:

=4*(5/2)

also yields 10, but this time because the parenthetical expression—(5/2) that is, 2.5—has priority over any other operation.

Now let’s get back to our regularly scheduled program—this matter of adding numbers via a formula. While the method we recounted above surely works, it conceals a problem. What if you need to add 20,000, or even 200, numbers? You won’t want to click on each and every one of those cells as per our initial method—and, given Excel’s cell character limit, you may not be able to do it anyway. So what’s the alternative?

Good question. The answer takes us to the first and most important of Excel’s built-in operations, or functions, called SUM. How does SUM work? For introductory purposes, we’ll demonstrate the standard way to implement SUM in a worksheet—with the AutoSum command, which is actually stored in two different tabs (remember that term?) in slightly different guises—Home and Formulas, shown in Figures 3–4 and 3–5:

Figure 3–4. The location of AutoSum in the Home tab

Figure 3–5. AutoSum in the Formulas tab

And once you appreciate how AutoSum works—and it’s rather simple—you’ll go a long way toward firming your grasp of the whole function genre.

So let’s try the following: click on the Home tab, if you’re not already there. Delete your answer in cell B15, stay in that cell, and click AutoSum. You should see Figure 3–6:

Figure 3–6. A more efficient way to add all those cells

Then press Enter, and presto—your answer should materialize.

You see what’s happened. AutoSum installed a function—SUM—into the cell in which you clicked, a cell that happens to be positioned directly below the range of numbers we wanted to add; and that’s a range SUM automatically identified. That’s why it’s called AutoSum.

But before we return to the workings of SUM in particular, note some very basic principles of Excel functions. First, apart form the equals sign (=), an open parenthesis always follows the name of the function—here, SUM. Then some additional information—which could be a range and/or some other entries, as you’ll see—follows, after which the expression is concluded with a close parenthesis. To summarize the basic syntax for any function:

=NAME(various data in here)

Remember that the equals sign always appears at the outset of any formula, e.g,

=67+SUM(B6:B15000). (Here we’re describing the basic syntax of a function considered alone.) What kind of data gets interposed between the parentheses depends on the function, as you’ll see; here, in our current case, SUM identifies the range to be added.

Now back to AutoSum. We see that AutoSum indeed correctly identified the range we wanted to add—and had that range, for example, been B6:B15000 instead, we could have clicked on cell B15001, and proceeded to click AutoSum. We’d then see this in B15001:

=SUM(B6:B15000)

We’d go on to press Enter, and the numbers in all those cells would be added.

Thus we’re beginning to infer what exactly it is that AutoSum does. It’s programmed to

automatically identify a range of consecutive numbers to be added in the column or row in which you’ve clicked. And you needn’t click in the cell immediately below the column or immediately to the right of the row containing the numbers—just somewhere in that column or row. Thus AutoSum will work in the example shown in Figure 3–7, too:

Figure 3–7. This time it’s a row being added

Starting to get the point? Here we’re adding a row of numbers, even as we’ve spaced the AutoSum cell two columns away from the last number—and Excel doesn’t mind. And look at the range SUM identified—

it’s included the two empty cells to the left of AutoSum, as the formula itself appears in H16.

In sum (pun intended) and by default, AutoSum will begin to total all consecutive numbers in the column or row in which it’s been positioned. But what if you see something like Figure 3–8 in cells C18 through C24?

Figure 3–8. Mind the gap…what about that blank cell?

If you click on cell C25 and turn to AutoSum, the resulting formula will read (Figure 3–9):

Figure 3–9. The blank cell interrupts the range to be added

What’s wrong with this picture? You want to add C18 through C24—and what you get is C22 through C24.

But you may now understand why. AutoSum designates ranges consisting only of consecutive number-bearing cells; and in the case before us there’s an empty cell—C21—which breaks the

continuity. And so AutoSum frames a range that extends only as far as the consecutive string of number-bearing cells closest to it. That’s why we see C22:C24.

But we want to add cells C18 through C24. When I’ve presented students with this problem, many have replied that a zero could be entered in the vacant cell—a worthy suggestion, because a zero naturally won’t alter the sum we want to compute, and because it contributes a longer, gap-free range to the formula—C18 through C24. But I strongly advise against this tack—even though the answer it proposes is correct. Because if you go ahead and also compute the average of the numbers in C18 through C24, the zero will heavily skew the result—because zero is a number, and a blank cell isn’t (as we’ll soon see).

The by-the-book way to solve the puzzle, then, is to click in cell C25, click AutoSum, and then drag cells C18 through C24—in effect, overriding AutoSum’s original (C22:C24) recommendation, as seen in Figure 3–10:

Figure 3–10. Drawing a blank: or rather, drawing your range over a blank cell separating two sets of numbers

As an alternative, nothing prevents you from typing the correct expression in the Formula Bar; but dragging the desired cells may be visually easier to track. Then press Enter, and all the desired cells are added. (Don’t, by the way, accidentally over-drag into cell C25—because that cell contains the formula itself, and you can’t incorporate it into its own result. That kind of miscue is called a circular reference (and Excel will deliver an onscreen error message to you to that effect), whereby the cell adds itself, as it were—and that causes way too many problems).

Thus we see that the initial range drawn up by AutoSum is merely a friendly suggestion, yours to accept or reject, and one you can replace with a different range if events warrant.

And along these lines, suppose you wanted to add the numbers in Figure 3–11, deposited in cells C14:C16 and E14:E16 respectively:

Figure 3–11. Adding multiple ranges

First, we need to decide the cell in which we want our answer to appear. In the interests of simplicity, let’s choose cell C17. We click AutoSum, and are presented with the range selection seen in Figure 3–12, of course:

Figure 3–12. Feeling left out: we want to add 4, 5, and 6 too

Now we have a decision to make. Because we want to add both ranges, we could next drag across all the cells in question, as in Figure 3–13:

Figure 3–13. Kind of a drag: spanning two ranges to be added in one SUM. Note, however, that the function treats these values as one range for formula purposes: C14:E16

And that’s what you probably would do, after which you’d press Enter and return your answer. But suppose there were an additional set of numbers in those in-between cells D14:D16—and you don’t want these incorporated in your answer. If we proceed with the range selection we’ve just named—

C14:E16—the numbers in D14:D16 will be brought along, because they too inhabit the selected range.

So how do we exclude these unwanted cells?

We can do the following:

Click AutoSum, yielding the initial C14:C16 range selection. Then type a comma (don’t add a space) and drag range E14:E16 (you can also hold down the Ctrl key after selecting the first range, and then select E14:E16 (Figure 3–14):

Figure 3–14. Adding two ranges-here the two sets of values are treated as two distinct ranges.

Then press Enter.

You get the picture. The comma splices our cell selections into two distinct ranges, thus leapfrogging the D14:D16 cells.

We’ve now learned a deeper truth about SUM, one that can be projected to all functions: you can identify any cell or range(s) you want in any formula, and place your formulas in any cell in the worksheet (so long as the formula isn’t dropped into the very range you’re referencing in the formula).

Remember that AutoSum was devised in order to execute a simplest-case scenario, the spreadsheet equivalent of a hanging curveball—that classic, add-one-row-or-one-column chore, where the row or column to be added consists of a range, all of whose cells are filled with numbers. But your formula requirements may be more nuanced, and the reality is that you can recruit cells and ranges from all across the worksheet—and even beyond it, as remains to be seen.

Note as well that nothing whatever stops you from typing SUM if you’re so moved, or any other function in Excel, for that matter. As long as you know the syntax, you’re free to tickle the keys as wish;

and as we’ll see, Excel offers you a number of ways to see to it that whatever you’re writing turns out correct.

And here are some AutoSum shortcuts: The keyboard equivalent for calling up the command is Alt+=. And if you’re working conventionally, and you know you want to add a standard range of consecutive numbers in a column: just click in an empty cell at the foot of the column and double-click the AutoSum button (as it appears in either the Home or Formulas groups). Double-clicking cuts immediately to the answer, without requiring you to make any additional range decisions.

And one more observation about AutoSum: if you rest your mouse over either of its buttons, the resulting caption tells you that AutoSum helps you “Display the sum of the selected cells directly after the selected cells”. But we know that isn’t quite true; as we saw earlier you can leave some space between the numbers (as long as these are consecutive) and the formula itself in that column or row and still get AutoSum to work for you, though those empty cells will also be referenced in the formula.

Now, you’ll note that both AutoSum buttons on either tab are accompanied by one of those drop-down arrows we alerted you to a good many pages ago (Figure 3–15):

Figure 3–15. What you see when you click that arrow

Click the arrow and you’ll see a small list of additional functions, and pretty important ones, ones I’ve used a million times:

How do these work and what do they do? To answer those questions in order—these functions behave virtually identically to SUM, in the sense that they’re written in the same manner, with the same kinds of range references and issues as described above. Moreover, each of these has an “Auto”

character—meaning that if you click beneath a column or to the right of a row of numbers, you can click on their names and install them in the desired cell—just as with AutoSum.

Now let’s explain what these functions actually do.

Dans le document Microsoft Excel 2010 (Page 70-78)