Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Excel_2010_Bible.pdf
Скачиваний:
26
Добавлен:
13.03.2015
Размер:
11.18 Mб
Скачать

Part II: Working with Formulas and Functions

Working with Array Formulas

This section deals with the mechanics of selecting cells that contain arrays and entering and editing array formulas. These procedures differ a bit from working with ordinary ranges and formulas.

Entering an array formula

When you enter an array formula into a cell or range, you must follow a special procedure so that Excel knows that you want an array formula rather than a normal formula. You enter a normal formula into a cell by pressing Enter. You enter an array formula into one or more cells by pressing Ctrl+Shift+Enter.

Don’t enter the curly brackets when you create an array formula; Excel inserts them for you. If the result of an array formula consists of more than one value, you must select all the cells in the results range before you enter the formula. If you fail to do so, only the first element of the result is returned.

Selecting an array formula range

You can select the cells that contain a multicell array formula manually by using the normal cell selection procedures. Or you can use either of the following methods:

Activate any cell in the array formula range. Display the Go To dialog box (choose Home Editing Find & Select Go To, or just press F5). In the Go To dialog box, click the Special button and then choose the Current Array option. Click OK to close the dialog box.

Activate any cell in the array formula range and press Ctrl+/ to select the entire array.

Editing an array formula

If an array formula occupies multiple cells, you must edit the entire range as though it were a single cell. The key point to remember is that you can’t change just one element of a multicell array formula. If you attempt to do so, Excel displays the message shown in Figure 16.7.

FIGURE 16.7

Excel’s warning message reminds you that you can’t edit just one cell of a multicell array formula.

364

Chapter 16: Introducing Array Formulas

The following rules apply to multicell array formulas. If you try to do any of these things, Excel lets you know about it:

You can’t change the contents of any individual cell that makes up an array formula.

You can’t move cells that make up part of an array formula (but you can move an entire array formula).

You can’t delete cells that form part of an array formula (but you can delete an entire array).

You can’t insert new cells into an array range. This rule includes inserting rows or columns that would add new cells to an array range.

You can’t use multicell array formulas inside of a table that was created by choosing Insert Tables Table. Similarly, you can’t convert a range to a table if the range contains a multicell array formula.

To edit an array formula, select all the cells in the array range and activate the Formula bar as usual (click it or press F2). Excel removes the brackets from the formula while you edit it. Edit the formula and then press Ctrl+Shift+Enter to enter the changes. All the cells in the array now reflect your editing changes.

Caution

If you accidentally press Ctrl+Enter (instead of Ctrl+Shift+Enter) after editing an array formula, the formula will be entered into each selected cell, but it will no longer be an array formula. And it will probably return an incorrect result. Just reselect the cells, press F2, and then press Ctrl+Shift+Enter. n

Although you can’t change any individual cell that makes up a multicell array formula, you can apply formatting to the entire array or to only parts of it.

Expanding or contracting a multicell array formula

Often, you may need to expand a multicell array formula (to include more cells) or contract it (to include fewer cells). Doing so requires a few steps:

1.Select the entire range that contains the array formula.

2.Press F2 to enter Edit mode.

3.Press Ctrl+Enter. This step enters an identical (non-array) formula into each selected cell.

4.Change your range selection to include additional or fewer cells, but make sure the active cell is in a cell that’s part of the original array.

5.Press F2 to re-enter Edit mode.

6.Press Ctrl+Shift+Enter.

365

Part II: Working with Formulas and Functions

Array Formulas: The Downside

If you’ve followed along in this chapter, you probably understand some of the advantages of using array formulas. The main advantage, of course, is that an array formula enables you to perform otherwise impossible calculations. As you gain more experience with arrays, however, you undoubtedly will also discover some disadvantages.

Array formulas are one of the least understood features of Excel. Consequently, if you plan to share a workbook with someone who may need to make modifications, you should probably avoid using array formulas. Encountering an array formula when you don’t know what it is can be very confusing.

You might also discover that you can easily forget to enter an array formula by pressing Ctrl+Shift+Enter. (And don’t forget: If you edit an existing array, you must remember to use this key combination to complete the edits.) Except for logical errors, this is probably the most common problem that users have with array formulas. If you press Enter by mistake after editing an array formula, just press F2 to get back into Edit mode and then press Ctrl+Shift+Enter.

Another potential problem with array formulas is that they can slow your worksheet’s recalculations, especially if you use very large arrays. On a faster system, this delay in speed may not be a problem. But, conversely, using an array formula is almost always faster than using a custom VBA function. See Chapter 40 for more information about creating custom VBA functions.

Using Multicell Array Formulas

This section contains examples that demonstrate additional features of multicell array formulas (array formulas that are entered into a range of cells). These features include creating arrays from values, performing operations, using functions, transposing arrays, and generating consecutive integers.

Creating an array from values in a range

The following array formula creates an array from a range of cells. Figure 16.8 shows a workbook with some data entered into A1:C4. The range D8:F11 contains a single array formula:

{=A1:C4}

The array in D8:F11 is linked to the range A1:C4. Change any value in A1:C4, and the corresponding cell in D8:F11 reflects that change. It’s a one-way link, of course. You can’t change a value in D8:F11.

366

Chapter 16: Introducing Array Formulas

FIGURE 16.8

Creating an array from a range.

Creating an array constant from values in a range

In the preceding example, the array formula in D8:F11 essentially created a link to the cells in A1:C4. It’s possible to sever this link and create an array constant made up of the values in A1:C4:

1.Select the cells that contain the array formula (the range D8:F11, in this example).

2.Press F2 to edit the array formula.

3.Press F9 to convert the cell references to values.

4.Press Ctrl+Shift+Enter to re-enter the array formula (which now uses an array constant).

The array constant is

{1,”dog”,3;4,5,”cat”;7,False,9;”monkey”,8,12}

Figure 16.9 shows how this looks in the Formula bar.

FIGURE 16.9

After you press F9, the Formula bar displays the array constant.

367

Part II: Working with Formulas and Functions

Performing operations on an array

So far, most of the examples in this chapter simply entered arrays into ranges. The following array formula creates a rectangular array and multiplies each array element by 2:

{={1,2,3,4;5,6,7,8;9,10,11,12}*2}

Figure 16.10 shows the result when you enter this formula into a range:

FIGURE 16.10

Performing a mathematical operation on an array.

The following array formula multiplies each array element by itself:

{={1,2,3,4;5,6,7,8;9,10,11,12}*{1,2,3,4;5,6,7,8;9,10,11,12}}

The following array formula is a simpler way of obtaining the same result. Figure 16.11 shows the result when you enter this formula into a range:

{={1,2,3,4;5,6,7,8;9,10,11,12}^2}

If the array is stored in a range (such asB8:E10), the array formula returns the square of each value in the range, as follows:

{=B8:E10^2}

FIGURE 16.11

Multiplying each array element by itself.

368

Chapter 16: Introducing Array Formulas

Using functions with an array

As you may expect, you also can use worksheet functions with an array. The following array formula, which you can enter into a 10-cell vertical range, calculates the square root of each array element in the array constant:

{=SQRT({1;2;3;4;5;6;7;8;9;10})}

If the array is stored in a range, a multicell array formula such as the one that follows returns the square root of each value in the range:

{=SQRT(A1:A10)}

Transposing an array

When you transpose an array, you essentially convert rows to columns and columns to rows. In other words, you can convert a horizontal array to a vertical array (and vice versa). Use the TRANSPOSE function to transpose an array.

Consider the following one-dimensional horizontal array constant:

{1,2,3,4,5}

You can enter this array into a vertical range of cells by using the TRANSPOSE function. To do so, select a range of five cells that occupy five rows and one column. Then enter the following formula and press Ctrl+Shift+Enter:

=TRANSPOSE({1,2,3,4,5})

The horizontal array is transposed, and the array elements appear in the vertical range.

Transposing a two-dimensional array works in a similar manner. Figure 16.12 shows a twodimensional array entered into a range normally and entered into a range by using the TRANSPOSE function. The formula in A1:D3 is

{={1,2,3,4;5,6,7,8;9,10,11,12}}

The formula in A6:C9 is

{=TRANSPOSE({1,2,3,4;5,6,7,8;9,10,11,12})}

You can, of course, use the TRANSPOSE function to transpose an array stored in a range. The following formula, for example, uses an array stored in A1:C4 (four rows, three columns). You can enter this array formula into a range that consists of three rows and four columns.

{=TRANSPOSE(A1:C4)}

369

Part II: Working with Formulas and Functions

FIGURE 16.12

Using the TRANSPOSE function to transpose a rectangular array.

Generating an array of consecutive integers

As you see in Chapter 17, generating an array of consecutive integers for use in an array formula is often useful. The ROW function, which returns a row number, is ideal for this. Consider the array formula shown here, entered into a vertical range of 12 cells:

{=ROW(1:12)}

This formula generates a 12-element array that contains integers from 1 to 12. To demonstrate, select a range that consists of 12 rows and one column and enter the array formula into the range. You’ll find that the range is filled with 12 consecutive integers (as shown in Figure 16.13).

FIGURE 16.13

Using an array formula to generate consecutive integers.

370

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]