Feb. 20, 2011, 10:52 a.m.
posted by donnie
Hack One of Excel's Database Functions to Take the Place of Many Functions
Excel's database functions—DSUM, DCOUNT, etc.—can take the place of potentially thousands of functions, thereby reducing both recalculation time and workbook space.
When using Excel's database functions, you can specify up to 256 different criteria. You might, for example, want to sum amounts in column A where the corresponding amount in column B is greater than 100 and the corresponding age in column C is less than 40. If, however, you want to sum amounts where corresponding amounts in column B are less than 50, you need to use another function and a different range of criteria. It would much easier if you had a single function and could easily and quickly change the criteria! If you have never used Excel's database functions before, we strongly recommend that you familiarize yourself with them, as they are very good for extracting statistical information from an Excel database or table.
To see how this works, set up your data as shown in Figure. Keep the column headings the same, but the data that resides in it can be any fictitious data. Name this table of data, including all column headings, AllData. Name the sheet Data.
Insert another worksheet and call this worksheet Results. In cell A2, enter the following formula:
Copy this across to cell F2 so that you have a mirror image of your table headings. In cell A3, enter any name that exists in your table on the data sheet, such as John D. Then, in cell B3, enter the following formula:
At this point, most people would follow the same concept for all names for which they need information extracted from the table. However, this effort is unnecessary.
As you are always referencing cell A3 for the name, it would make a lot more sense in most cases if you could simply have a drop-down list in cell A3 containing all the names that are in the table. You can use Excel's standard validation feature to create such a list. However, as the original list of names resides on another worksheet, you cannot reference the list in the same way as you would a list residing on the same sheet—i.e., a standard range reference. You can overcome this easily by naming the Name column in the original table, then using that name as the list source for the validation.
Ensure that the In-Cell drop-down checkbox is checked and then click OK. Now you can select any name from the list in cell A3, and your data to the right will display the appropriate information automatically.
To do this, first you need to create a dynamic named range for both the Full Cost column and the Percent Paid column. In the Names in Workbook: box, enter FullCost. In the Refers To: box, type the following formula, and click Add:
This time, in the Names in Workbook: box, enter PercentPaid. In the Refers To: box, type the following formula and click Add:
Activate the Results sheet, select cell A11, and then select Data Validation. Select List from the Allow: box and enter =Full_Cost in the Source: box. Click OK.
Select cell B11, then select Data Validation. Select List from the Allow: box and enter =Percent_Paid in the Source: box. Click OK.
In cell A12, enter the following:
Select cell B12, and enter the following:
Select cell A13, and enter the following:
Select cell B13, and enter the following:
In cell A15, enter the following:
Select any Full Cost amount from cell A11 and any percent paid amount from cell B11, and the DCOUNT function will give you a count of all the people who meet that criteria. For instance, if you select 65 and 100%, you will be extracting a count of people that have a Full Cost greater than 65 and a Percent Paid less than 100.
As you can see, you can use this one DCOUNT function to extract any combination of criteria for the Full Cost and Percent Paid columns. With a little more work, you can take this to yet another level and make the comparison operators used in the criteria interchangeable.
The first thing you need to do is create a list of comparison operators that you can use in a validation list. Scroll across to an out-of-the-way column on the Results sheet, and on any row in that column, enter the heading Operators. Below this and moving down one cell at a time, enter =, >=, >, <, and <=, as shown in Figure.
To name this range, select the heading and all operators below it and then select Insert Name Create. Ensure that Top Row Only is selected, and click OK. Excel automatically will name the range based on the heading— in this case, Operators.
Select cell G7 and enter the heading Select a Criteria.
Go back to the Data sheet and create a dynamic named range for the Dates column. Select Insert Name Define, and in the Names in Workbook: box, type Dates; in the Refers To: box, type the following formula and click Add:
Select cell G7, copy it, and paste it into cell G9. Change the word Criteria to Date. Select cells G10:H10, select Data Validation, and then select List from the Allow: box. In the Source: box, enter =Dates. Ensure that the In-Cell drop-down box is checked and click OK. Select cell G11, and enter the following:
Copy this across to cell H11. Select cell G12, enter the following formula (you should use the date format applicable to your particular region), and copy it across to cell H12:
In cell F13, enter the word Result and center it across the selection, with F13 and G13 selected. In cell H13, enter the following function:
The end result should look like Figure, which, for the sake of demonstration, has all formulas displayed.
Hide rows 11 and 12, as you do not need to see them. You will end up with a simple-to-use table that looks like Figure, which has had formatting applied for ease of reading.
By using this principle, you can easily have either one or a few database functions doing the work of what usually would require hundreds.
For a working example of this exercise, as well as similar examples, visit http://www.ozgrid.com/download/default.htm and click the heading DFunctionsWithValidation.zip.