Excel's forte is spreadsheet work, but its row-and-column layout also makes it a natural flat-file database manager. In Excel, a list is a collection of related information with an organizational structure that makes it easy to find or extract data from its contents. Specifically, a list is a worksheet range that has the following properties:
For example, suppose that you want to set up an accounts-receivable list. A simple system would include information such as account name, account number, invoice number, invoice amount, due date, and date paid, as well as a calculation of the number of days overdue. Figure 2.22 shows how this system would be implemented as an Excel list. Figure 2.22. Accounts-receivable data in an Excel worksheet.Excel lists don't require elaborate planning, but you should follow a few guidelines for best results. Here are some pointers:
Converting a Range to a ListExcel has a number of commands that enable you to work efficiently with list data. To take advantage of these commands, you must convert your data from a normal range to a list by following these steps:
When you convert a range to a list, Excel makes three changes to the range, as shown in Figure 2.23:
Figure 2.23. The accounts-receivable data converted to a list.If you ever need to change the list back to a range, select a cell within the list and choose Data, List, Convert to Range. Basic List OperationsAfter you've converted the range to a list, you can start working with the data. Following is a quick look at some basic list operations:
Sorting a ListOne of the advantages of a list is that you can rearrange the records so that they're sorted alphabetically or numerically. This feature enables you to view the data in order by customer name, account number, part number, or any other field. You even can sort on multiple fields, which would enable you, for example, to sort a client list by state and then by name within each state. The following procedure shows you how to sort a list:
Sorting on More Than Three KeysYou're not restricted to sorting on only three fields in an Excel list. By performing consecutive sorts, you can sort on any number of fields. For example, suppose that you want to sort a customer list by the following fields (in order of importance): Region, State, City, ZIP Code, and Name. To use five fields, you must perform two consecutive sorts. The first sort uses the three least important fields: City, ZIP Code, and Name. Of these three, City is the most important, so it's selected in the Sort By field; ZIP Code is selected in the first Then By field, and Name is selected in the second Then By field. When this sort is complete, you must run a second sort using the remaining keys, Region and State. Select Region in the Sort By list and State in the first Then By list. By running multiple sorts and always using the least important fields first, you can sort on as many fields as you like. Sorting a List in Natural OrderIt's often convenient to see the order in which records were entered into a list, or the natural order of the data. Normally, you can restore a list to its natural order by choosing Edit, Undo Sort immediately after a sort. Unfortunately, after several sort operations, it's no longer possible to restore the natural order. The solution is to create a new field, called, for example, Record, in which you assign consecutive numbers as you enter the data. The first record is 1, the second is 2, and so on. To restore the list to its natural order, you sort on the Record field.
Follow these steps to add a new field to the list:
Figure 2.26 shows the Accounts Receivable list with a Record field added and the record numbers inserted. Figure 2.26. The Record field tracks the order in which records are added to a list.
Sorting on Part of a FieldExcel performs its sorting chores based on the entire content of each cell in the field. This method is fine for most sorting tasks, but occasionally you'll need to sort on only part of a field. For example, your list might have a ContactName field that contains a first name and then a last name. Sorting on this field orders the list by each person's first name, which is probably not what you want. To sort on the last name, you need to create a new column that extracts the last name from the field. You can then use this new column for the sort. Excel's text functions make it easy to extract substrings from a cell. In this case, assume that each cell in the ContactName field has a first name, followed by a space, followed by a last name. Your task is to extract everything after the space, and the following formula does the job (assuming that the name is in cell D2): =RIGHT(D2, LEN(D2) - FIND(" ", D2)) Figure 2.27 shows this formula in action. Column D contains the names, and column A contains the formula to extract the last name. I sorted on column A to order the list by last name. Figure 2.27. To sort on part of a field, use Excel's text functions to extract the string you need for the sort.
Sorting Without ArticlesLists that contain field values starting with articles (A, An, and The) can throw off your sorting. To fix this problem, you can borrow the technique from the preceding section and sort on a new field in which the leading articles have been removed. As before, you want to extract everything after the first space, but you can't just use the same formula because not all the titles have a leading article. You need to test for a leading article using the following OR() function: OR(LEFT(A2,2) = "A ", LEFT(A2,3) = "An ", LEFT(A2,4) = "The ") Here I'm assuming that the text being tested is in cell A2. If the left two characters are A, or the left three characters are An, or the left four characters are The, this function returns TRUE (that is, you're dealing with a title that has a leading article). Now you need to package this OR() function inside an IF() test. If the OR() function returns trUE, the command should extract everything after the first space; otherwise, it should return the entire title. Here it is (Figure 2.28 shows the formula in action): =IF( OR(LEFT(A2,2) = "A ", LEFT(A2,3) = "An ", LEFT(A2,4) = "The "), RIGHT(A2, LEN(A2) - FIND(" ", A2, 1)), A2) Figure 2.28. A formula that removes leading articles for proper sorting.Filtering List DataOne of the biggest problems with large lists is that it's often hard to find and extract the data you need. Sorting can help, but in the end, you're still working with the entire list. What you need is a way to define the data that you want to work with and then have Excel display only those records onscreen. This action is called filtering your data. Fortunately, Excel offers several techniques that get the job done. Using AutoFilter to Filter a ListExcel's AutoFilter feature makes filtering out subsets of your data as easy as selecting an option from a drop-down list. In fact, that's literally what happens. When you convert a range to a list, Excel automatically turns on AutoFilter, which is why you see drop-down arrows in the cells containing the list's column labels. (You can toggle AutoFilter off and on by choosing Data, Filter, AutoFilter.) Clicking one of these arrows displays a list of all the unique entries in the column. Figure 2.29 shows the drop-down list for the Account Name field in an Accounts Receivable database. Figure 2.29. For each list field, AutoFilter adds drop-down lists that contain only the unique entries in the column.If you click an item in one of these AutoFilter lists, Excel takes the following actions:
To continue filtering the data, you can select an item from one of the other lists. For example, you could choose a date from the Due Date list to see only those Brimson Furniture invoices due on that date. AutoFilter Criteria OptionsThe items you see in each drop-down list are called the filter criteria. Besides selecting specific criteria (such as an account name), you have the following choices in each drop-down list:
Setting Up Custom AutoFilter CriteriaIn its basic form, AutoFilter enables you to select only a single item from each column drop-down list. AutoFilter's custom filter criteria, however, give you a way to select multiple items. In the Accounts Receivable list, for example, you could use custom criteria to display all the invoices with the following:
Before you learn the steps required to create a custom AutoFilter criterion, let's go through an overview of what happens. When you click the Custom option in an AutoFilter drop-down list, Excel displays the Custom AutoFilter dialog box, shown in Figure 2.32. Figure 2.32. Use the Custom AutoFilter dialog box to enter your custom criteria.
You use the two drop-down lists across the top to set up the first part of your criterion. The list on the left contains a list of Excel's comparison operators (such as Equals and Is Greater Than). The combo box on the right enables you to select a unique item from the field or enter your own value. For example, if you want to display invoices with an amount greater than or equal to $1,000, click the Is Greater Than or Equal operator and enter 1000 into the text box. For text fields, you can also use wildcard characters to substitute for one or more characters. Use the question mark (?) wildcard to substitute for a single character. For example, if you enter sm?th, Excel finds both Smith and Smyth. To substitute for groups of characters, use the asterisk (*). For example, if you enter *carolina, Excel finds all the entries that end with "carolina."
You can create compound criteria by clicking the And or Or buttons and then entering another criterion in the bottom two drop-down lists. Use And when you want to display records that meet both criteria; use Or when you want to display records that meet at least one of the two criteria. For example, to display invoices with an amount greater than or equal to $1,000 and less than or equal to $5,000, you fill in the dialog box as shown in Figure 2.33. Figure 2.33. A compound criterion that displays the records with invoice amounts between $1,000 and $5,000.
The following procedure takes you through the official steps to set up a custom AutoFilter criterion:
Showing Filtered RecordsWhen you need to redisplay records that have been filtered via AutoFilter, use any of the following techniques:
Using Complex Criteria to Filter a ListThe AutoFilter should take care of most of your filtering needs, but it's not designed for heavy-duty work. For example, AutoFilter can't handle the following Accounts Receivable criteria:
To work with these more sophisticated requests, you need to use complex criteria. Setting Up a Criteria RangeBefore you can work with complex criteria, you must set up a criteria range. A criteria range has some or all of the list field names in the top row, with at least one blank row directly underneath. You enter your criteria in the blank row below the appropriate field name, and Excel searches the list for records with field values that satisfy the criteria. This setup gives you two major advantages over AutoFilter:
You can place the criteria range anywhere on the worksheet outside the list range. The most common position, however, is a couple of rows above the list range. Figure 2.34 shows the Accounts Receivable list with a criteria range. As you can see, the criteria are entered in the cell below the field name. In this case, the displayed criteria will find all Brimson Furniture invoices that are greater than or equal to $1,000 and that are overdue (that is, invoices that have a value greater than 0 in the Days Overdue field). Figure 2.34. Set up a separate criteria range (B1:D2, in this case) to enter complex criteria.Filtering a List with a Criteria RangeAfter you've set up your criteria range, you can use it to filter the list. The following procedure takes you through the basic steps:
Entering Compound CriteriaTo enter compound criteria in a criteria range, use the following guidelines:
Finding records that match all the criteria is equivalent to activating the And button in the Custom AutoFilter dialog box. The sample criteria shown earlier in Figure 2.34 match records with the account name Brimson Furniture and an invoice amount greater than $1,000 and a positive number in the Days Overdue field. To narrow the displayed records, you can enter criteria for as many fields as you like.
Finding records that match at least one of several criteria is equivalent to activating the Or button in the Custom AutoFilter dialog box. In this case, you need to enter each criterion on a separate row. For example, to display all invoices with amounts greater than or equal to $10,000 or that are more than 30 days overdue, you would set up your criteria as shown in Figure 2.37. Figure 2.37. To display records that match one or more of the criteria, enter the criteria in separate rows.
Entering Computed CriteriaThe fields in your criteria range aren't restricted to the list fields. You can create computed criteria that use a calculation to match records in the list. The calculation can refer to one or more list fields, or even to cells outside the list, and must return either trUE or FALSE. Excel selects records that return trUE. To use computed criteria, add a column to the criteria range and enter the formula in the new field. Make sure that the name you give the criteria field is different from any field name in the list. When referencing the list cells in the formula, use the first row of the list. For example, to select all records in which the Date Paid is equal to the Due Date in the accounts receivable list, enter the following formula: =G5=F5 Note the use of relative addressing. If you want to reference cells outside the list, use absolute addressing.
Figure 2.38 shows a more complex example. The goal is to select all records whose invoices were paid after the due date. The new criterionnamed Late Payerscontains the following formula: =IF(ISBLANK(G5), FALSE(), G5 > F5) Figure 2.38. Use a separate criteria range column for calculated criteria.If the Date Paid field (column G) is blank, the invoice hasn't been paid, so the formula returns FALSE. Otherwise, the logical expression G5 > F5 is evaluated. If the Date Paid (column G) is greater than the Due Date field (column F), the expression returns trUE and Excel selects the record. In Figure 2.38, the Late Payers cell (B2) displays FALSE because the formula evaluates to FALSE for the first row in the list. Summarizing List DataBecause a list is just a special kind of worksheet range, you can analyze list data using many of the same methods you use for regular worksheet cells. Typically, this task involves using formulas and functions to answer questions and produce results. To make your analysis chores easier, Excel enables you to create automatic subtotals that can give you instant subtotals, averages, and more. Excel goes one step further by also offering many list-specific functions. These functions work with entire lists or subsets defined by a criteria range. The rest of this chapter shows you how to use all these tools to analyze and summarize your data. Creating Automatic SubtotalsAutomatic subtotals enable you to summarize your sorted list data quickly. For example, if you have a list of invoices sorted by account name, you can use automatic subtotals to give you the following information for each account:
You can do all this and more without entering a single formula. Excel does the calculations and enters the results automatically. You also can just as easily create grand totals that apply to the entire list.
Setting Up a List for Automatic SubtotalsExcel calculates automatic subtotals based on data groupings in a selected field. For example, if you ask for subtotals based on account name, Excel runs down the account name column and creates a new subtotal each time the name changes. To get useful summaries, then, you need to sort the list on the field containing the data groupings you're interested in. Figure 2.39 shows the Accounts Receivable database sorted by account name. If you subtotal the Account Name field, you get summaries for Brimson Furniture, Chimera Illusions, Door Stoppers Ltd., and so on. Figure 2.39. A sorted list ready for displaying subtotals.
Displaying SubtotalsTo subtotal a list, follow these steps:
Figure 2.41 shows the Accounts Receivable list with the Invoice Amount field subtotaled. Figure 2.41. A list showing Invoice Amount subtotals for each Account Name.Adding More SubtotalsYou can add any number of subtotals to the current summary. The following procedure shows you what to do:
For example, Figure 2.42 shows the Accounts Receivable list with two new subtotals that count the invoices and display the maximum number of days overdue. Figure 2.42. You can use multiple subtotals in a list.Removing SubtotalsTo remove the subtotals from a list, choose Data, Subtotals to display the Subtotal dialog box, and then click Remove All. Excel's List FunctionsTo get more control over your list analysis, you can use Excel's list functions. These functions are the same as those used in subtotals, but they have the following advantages:
About List FunctionsTo illustrate the list functions, consider an example. If you want to calculate the sum of a list field, for instance, you can enter SUM(range), and Excel produces the result. If you want to sum only a subset of the field, you must specify as arguments the particular cells to use. For lists containing hundreds of records, however, this process is impractical. (It's also illegal, for two reasons: Excel allows a maximum of 30 arguments in the SUM() function, and it allows a maximum of 255 characters in a cell entry.) The solution is to use DSUM(), which is the list equivalent of the SUM() function. The DSUM() function takes three arguments: a list range, a field name, and a criteria range. DSUM() looks at the specified field in the list and sums only records that match the criteria in the criteria range. The list functions come in two varieties: those that don't require a criteria range and those that do. List Functions That Don't Require a Criteria RangeExcel has two list functions that enable you to specify the criteria as an argument rather than a range: COUNTIF() and SUMIF(). Using COUNTIF()The COUNTIF() function counts the number of cells in a range that meet a single criterion: COUNTIF(range, criteria)
For example, Figure 2.43 shows a COUNTIF() function that calculates the total number of invoices that are more than 30 days overdue. Figure 2.43. Use COUNTIF() to count the cells that meet a criterion.Using SUMIF()The SUMIF() function is similar to COUNTIF(), except that it sums the range cells that meet its criterion: SUMIF(range, criteria [, sum_range])
Figure 2.44 shows a Parts database. The SUMIF() function in cell F16 sums the total cost (F7:F14) for the parts where Division (A7:A14) is equal to 3. Figure 2.44. Use SUMIF() to sum cells that meet a criterion.List Functions That Require a Criteria RangeThe remaining list functions require a criteria range. These functions take a little longer to set up, but the advantage is that you can enter compound and computed criteria. All these functions have the following format: Dfunction(database, field, criteria)
Table 2.1 summarizes the list functions.
You enter list functions the same way you enter any other Excel function. You type an equal sign (=) and then enter the functioneither by itself or combined with other Excel operators in a formula. The following examples all show valid list functions: =DSUM(A6:H14, "Total Cost", A1:H3) =DSUM(List, "Total Cost", Criteria) =DSUM(AR_List, 3, Criteria) =DSUM(1993_Sales, "Sales", A1:H13) The next two sections provide examples of the DAVERAGE() and DGET() list functions. Using DAVERAGE()The DAVERAGE() function calculates the average field value in the database records that match the criteria. In the Parts database, for example, suppose that you want to calculate the average gross margin for all parts assigned to Division 2. You set up a criteria range for the Division field and enter 2, as shown in Figure 2.45. You then enter the following DAVERAGE() function (see cell H3): =DAVERAGE(A6:H14, "Gross Margin", A2:A3) Figure 2.45. Use DAVERAGE() to calculate the field average in the matching records.Using DGET()The DGET() function extracts the value of a single field in the database records that match the criteria. If there are no matching records, DGET() returns #VALUE!. If there is more than one matching record, DGET() returns #NUM!. DGET() typically is used to query the list for a specific piece of information. For example, in the Parts list, you might want to know the cost of the Finley Sprocket. To extract this information, you would first set up a criteria range with the Description field and enter Finley Sprocket. You would then extract the information with the following formula (assuming that the list and criteria ranges are named Database and Criteria, respectively): =DGET(Database, "Cost", Criteria) A more interesting application of this function would be to extract the name of a part that satisfies a certain condition. For example, you might want to know the name of the part that has the highest gross margin. Creating this model requires two steps:
Figure 2.46 shows how this is done. For the criteria, a new field called Highest Margin is created. As the text box shows, this field uses the following computed criteria: =H7 = MAX($H$7:$H$14) Figure 2.46. A DGET() function that extracts the name of the part with the highest margin.The range $H$7:$H$14 is the Gross Margin field. (Note the use of absolute references.) Excel matches only the record that has the highest gross margin. The DGET() function in cell H3 is straightforward: =DGET(A6:H14, "Description", A2:A3) This formula returns the description of the part that has the highest gross margin. From Here
|