Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 86 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
86
Dung lượng
1,13 MB
Nội dung
You can use additional array formulas to calculate other measures for the data in this example. For instance, the following array formula returns the largest change (that is, the greatest improvement). This formula returns 23, which represents Linda’s test scores. {=MAX(C2:C15-B2:B15)} The following array formula returns the smallest change (that is, the least improvement). This formula returns –11, which represents Nancy’s test scores. {=MIN(C2:C15-B2:B15)} Using an Array in Lieu of a Range Reference If your formula uses a function that requires a range reference, you may be able to replace that range reference with an array constant. This is useful in situations in which the values in the referenced range do not change. A notable exception to using an array constant in place of a range reference in a function is with the database functions that use a reference to a criteria range (for example, DSUM).Unfortunately, using an array constant instead of a reference to a criteria range does not work. Figure 14-17 shows a worksheet that uses a lookup table to display a word that corresponds to an integer. For example, looking up a value of 9 returns Nine from the lookup table in D1:E10. The formula in cell C1 is: =VLOOKUP(B1,D1:E10,2,FALSE) Figure 14-17: You can replace the lookup table in D1:E10 with an array constant. Chapter 14: Introducing Arrays 395 4800-x Ch14.F 8/27/01 11:57 AM Page 395 You can use a two-dimensional array in place of the lookup range. The follow- ing formula returns the same result as the previous formula, but it does not require the lookup range in D1:E1: =VLOOKUP(B1,{1,”One”;2,”Two”;3,”Three”;4,”Four”;5,”Five”; 6,”Six”;7,”Seven”;8,”Eight”;9,”Nine”;10,”Ten”},2,FALSE) Summary This chapter introduced the concept of arrays, collections of items that reside in a range or in Excel’s memory. An array formula operates on a range and returns a single value or an array of values. The next chapter continues this discussion and presents several useful examples that help clarify the concept. 396 Part IV: Array Formulas 4800-x Ch14.F 8/27/01 11:57 AM Page 396 Chapter 15 Performing Magic with Array Formulas IN THIS CHAPTER ◆ More examples of single-cell array formulas ◆ More examples of multicell array formulas ◆ Returning an array from a custom VBA function THE PREVIOUS CHAPTER PROVIDED an introduction to arrays and array formulas, and presented some basic examples to whet your appetite. This chapter continues the saga and provides many useful examples that further demonstrate the power of this feature. I selected the examples in this chapter to provide a good assortment of the vari- ous uses for array formulas. Most can be used as-is. You will, of course, need to adjust the range names or references used. Also, you can modify many of the examples easily to work in a slightly different manner. Each of the examples in this chapter is demonstrated in a file on the com- panion CD-ROM. Working with Single-Cell Array Formulas As I described in the previous chapter, you enter single-cell array formulas into a single cell (not into a range of cells). These array formulas work with arrays con- tained in a range, or that exist in memory. This section provides some additional examples of such array formulas. 397 4800-x Ch15.F 8/27/01 11:57 AM Page 397 398 Part IV: Array Formulas Summing a Range That Contains Errors You’ve probably discovered that Excel’s SUM function doesn’t work if you attempt to sum a range that contains one or more error values (such as #DIV/0! or #N/A). Figure 15-1 shows an example. The SUM formula in cell C9 returns an error value because the range that it sums (C2:C8) contains errors. Figure 15-1: An array formula can sum a range of values, even if the range contains errors. The following array formula returns a sum of the values in a range named Data, even if the range contains error values: {=SUM(IF(ISERROR(Data),””,Data))} This formula works by creating a new array that contains the original values, but without the errors. The IF function effectively filters out error values by replac- ing them with an empty string. The SUM function then works on this “filtered” array. This technique also works with other functions, such as MIN and MAX. You may want to use a function other than ISERROR. The ISERROR function returns TRUE for any error value: #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL!. The ISERR function returns TRUE for any error except #N/A.The ISNA function returns TRUE only if the cell contains #N/A. Counting the Number of Error Values in a Range The following array formula is similar to the previous example, but it returns a count of the number of error values in a range named Data: {=SUM(IF(ISERROR(Data),1,0))} This formula creates an array that consists of 1s (if the corresponding cell con- tains an error) and 0s (if the corresponding cell does not contain an error value). 4800-x Ch15.F 8/27/01 11:57 AM Page 398 You can simplify the formula a bit by removing the third argument for the IF function. If this argument is not specified, the IF function returns FALSE if the con- dition is not satisfied (that is, the cell does not contain an error value). The array formula shown here performs exactly like the previous formula, but doesn’t use the third argument for the IF function: {=SUM(IF(ISERROR(Data),1))} Actually, you can simplify the formula even more: {=SUM(ISERROR(Data)*1)} This version of the formula relies on the fact that: TRUE * 1 = 1 and FALSE * 1 = 0 Summing Based on a Condition Often, you need to sum values based on one or more conditions. The array formula that follows, for example, returns the sum of the positive values (it excludes nega- tive values) in a range named Data: {=SUM(IF(Data>0,Data))} The IF function creates a new array that consists only of positive values and False values. This array is passed to the SUM function, which ignores the False val- ues and returns the sum of the positive values. The Data range can consist of any number of rows and columns. You can also use Excel’s SUMIF function for this example. The following for- mula, which is not an array formula, returns the same result as the previous array formula: =SUMIF(Data,”>0”) For multiple conditions, however, using SUMIF gets tricky. For example, if you want to sum only values that are greater than 0 and less than or equal to 5, you can use this non-array formula: SUMIF(data,”>0”,data)-SUMIF(data,”>5”,data) Chapter 15: Performing Magic with Array Formulas 399 4800-x Ch15.F 8/27/01 11:57 AM Page 399 This formula sums the values that are greater than zero, and then subtracts the sum of the values that are greater than 5. This can be confusing. Following is an array formula that performs the same calculation: {=SUM((Data>0)*(Data<=5)*Data)} This formula also has a limitation: It will return an error if the Data range con- tains one or more non-numeric cells. Contrary to what you might expect, you cannot use the AND function in an array formula.The following array formula,while quite logical, doesn’t return the correct result: {=SUM(IF(AND(Data>0,Data<=5),Data))} 400 Part IV: Array Formulas Illogical Behavior from Logical Functions Excel’s AND and OR functions are logical functions that return TRUE or FALSE. Unfortunately, these functions do not perform as expected when used in an array formula. As shown here, columns A and B contain logical values. The AND function returns TRUE if all of its arguments are TRUE. Column C contains non-array formulas that work as expected. For example, cell C3 contains the following function: =AND(A3,B3) The range D3:D6 contains this array formula: {=AND(A3:A6,B3:B6)} You might expect this array formula to return the following array: {TRUE,FALSE,FALSE,FALSE} 4800-x Ch15.F 8/27/01 11:57 AM Page 400 You can also write an array formula that combines criteria using an OR condi- tion. For example, to sum the values that are less than 0 or greater than 5, use the following array formula: {=SUM(IF((Data<0)+(Data>5),Data))} As with the AND function, you cannot use the OR function in an array for- mula.The following formula, for example, does not return the correct result: {=SUM(IF(OR(Data<0,Data>5),Data))} For an explanation of the workarounds required for using logical functions in an array formula, refer to the following sidebar, “Illogical Behavior from Logical Functions.” Chapter 15: Performing Magic with Array Formulas 401 Rather, it returns only a single item: FALSE. In fact, both the AND function and the OR function always return a single result (never an array). Even when using array constants, the AND function still returns only a single value. For example, this array formula does not return an array: {=AND({TRUE,TRUE,FALSE,FALSE},{TRUE,FALSE,TRUE,FALSE})} I don’t know if this is by design or if it’s a bug. In any case, it certainly is inconsistent with how the other functions operate. Column E contains another array formula, which follows, that returns an array of 0s and 1s. These 0s and 1s correspond to FALSE and TRUE, respectively. {=A3:A6*B3:B6} In array formulas, you must use this syntax in place of the AND function. The following array formula, which uses the OR function, does not return an array (as you might expect): =OR(A3:A6,B3:B6) Rather, you can use a formula such as the following, which does return an array comprised of logical OR using the corresponding elements in the ranges: {=A3:A6+B3:B6} 4800-x Ch15.F 8/27/01 11:57 AM Page 401 Summing the n Largest Values in a Range The following array formula returns the sum of the 10 largest values in a range named Data: {=SUM(LARGE(Data,ROW(INDIRECT(“1:10”))))} The LARGE function is evaluated 10 times, each time with a different second argument (1, 2, 3, and so on up to 10). The results of these calculations are stored in a new array, and that array is used as the argument for the SUM function. To sum a different number of values, replace the 10 in the argument for the INDIRECT function with another value. To sum the n smallest values in a range, use the SMALL function instead of the LARGE function. Computing an Average That Excludes Zeros Figure 15-2 shows a simple worksheet that calculates average sales. The formula in cell B11 is: =AVERAGE(B2:B9) Figure 15-2: The calculated average includes cells that contain a 0. This formula, of course, calculates the average of the values in B2:B9. Two of the sales staff had the week off, however, so this average doesn’t accurately describe the average sales per representative. The AVERAGE function ignores blank cells, but does not ignore cells that contain 0. 402 Part IV: Array Formulas 4800-x Ch15.F 8/27/01 11:57 AM Page 402 The following array formula returns the average of the range, but excludes the cells containing 0: {=AVERAGE(IF(B2:B9<>0,B2:B9))} This formula creates a new array that consists only of the non-zero values in the range. The AVERAGE function then uses this new array as its argument. You also can get the same result with a regular (non-array) formula: =SUM(B2:B9)/COUNTIF(B2:B9,”<>0”) This formula uses the COUNTIF function to count the number of non-zero values in the range. This value is divided into the sum of the values. Determining Whether a Particular Value Appears in a Range To determine whether a particular value appears in a range of cells, you can choose the Edit → Find command and do a search of the worksheet. But you also can make this determination by using an array formula. Figure 15-3 shows a worksheet with a list of names in A3:E22 (named NameList). An array formula in cell D1 checks the name entered into cell C1 (named TheName). If the name exists in the list of names, the formula displays the text Found. Otherwise, it displays Not Found. Figure 15-3: Using an array formula to determine if a range contains a particular value. Chapter 15: Performing Magic with Array Formulas 403 4800-x Ch15.F 8/27/01 11:57 AM Page 403 The array formula in cell D1 is: {=IF(OR(TheName=NameList),”Found”,”Not Found”)} This formula compares TheName to each cell in the NameList range. It builds a new array that consists of logical TRUE or FALSE values. The OR function returns TRUE if any one of the values in the new array is TRUE. The IF function uses this result to determine which message to display. A simpler form of this formula follows. This formula displays TRUE if the name is found, and returns FALSE otherwise. {=OR(TheName=NameList)} Counting the Number of Differences in Two Ranges The following array formula compares the corresponding values in two ranges (named MyData and YourData), and returns the number of differences in the two ranges. If the contents of the two ranges are identical, the formula returns 0. {=SUM(IF(MyData=YourData,0,1))} The two ranges must be the same size and of the same dimensions. This formula works by creating a new array of the same size as the ranges being compared. The IF function fills this new array with 0s and 1s (0 if a difference is found, 1 if the corresponding cells are the same). The SUM function then returns the sum of the values in the array. The following formula, which is simpler, is another way of calculating the same result. {=SUM(1*(MyData<>YourData))} This version of the formula relies on the fact that: TRUE * 1 = 1 and FALSE * 1 = 0 Returning the Location of the Maximum Value in a Range The following array formula returns the row number of the maximum value in a single-column range named Data: 404 Part IV: Array Formulas 4800-x Ch15.F 8/27/01 11:57 AM Page 404 [...]... Validation CHAPTER 20 Creating Megaformulas CHAPTER 21 Tools and Methods for Debugging Formulas 4800-x PO5.F 8/27/01 11:57 AM Page 424 4800-x Ch 16. F 8/27/01 11:57 AM Page 425 Chapter 16 Intentional Circular References IN THIS CHAPTER N General information regarding how Excel handles circular references N Why you might want to use an intentional circular reference N How Excel determines calculation and... use Excel s pivot table feature to summarize data in this way However, pivot tables do not update automatically when the data changes, so the array formula method described here has at least one advantage Working with Multicell Array Formulas The previous chapter introduced array formulas entered into multicell ranges In this section, I present a few more array multicell formulas Most of these formulas. .. Sales) The first method (column C) uses Excel s RANK function Column D uses array formulas to compute the ranks Figure 15-8: Ranking data with Excel s RANK function and with array formulas The following is the array formula in cell D2: {=SUM(1*(B2 . the concept. 3 96 Part IV: Array Formulas 4800-x Ch14.F 8/27/01 11:57 AM Page 3 96 Chapter 15 Performing Magic with Array Formulas IN THIS CHAPTER ◆ More examples of single-cell array formulas ◆ More. contains non-array formulas that work as expected. For example, cell C3 contains the following function: =AND(A3,B3) The range D3:D6 contains this array formula: {=AND(A3:A6,B3:B6)} You might expect. expect): =OR(A3:A6,B3:B6) Rather, you can use a formula such as the following, which does return an array comprised of logical OR using the corresponding elements in the ranges: {=A3:A6+B3:B6} 4800-x