Often, spreadsheets require intermediate formulas to produce a desired result. In other words, a formula may depend on other formulas, which in turn depend on other formulas. After you get all these formulas working correctly, it's often possible to eliminate the intermediate formulas and use what I refer to as a single megaformula instead. The advantages? You use fewer cells (less clutter), and recalculation may be faster. Besides, people in the know will be impressed with your formula-building abilities. The disadvantages? The formula may be impossible to decipher or modify.
Here's an example: Imagine a worksheet with a column of people's names. And suppose that you've been asked to remove all the middle names and middle initials from the names — but not all the names have a middle name or initial. Editing the cells manually would take hours, so you opt for a formula-based solution. Although this is not a difficult task, it normally involves several intermediate formulas.
Figure 3-9 shows the results of the more conventional solution, which requires six intermediate formulas shown in Table 3-5. The names are in column A; the end result goes in column H. Columns B through G hold the intermediate formulas.
Table 3-5 INTERMEDIATE FORMULAS WRITTEN IN THE FIRST ROW OF SHEET1 IN FIGURE 3-9
Cell Intermediate Formula
What It Does
Removes excess spaces. Locates the first space.
Locates the second space. Returns #VALUE! if there is no second space.
Uses the first space if no second space exists. Extracts the first name. Extracts the last name. Concatenates the two names.
You can eliminate all the intermediate formulas by creating a megaformula. You do so by creating all the intermediate formulas and then going back into the final result formula and replacing each cell reference with a copy of the formula in the cell referred to (without the equal sign). Fortunately, you can use the Clipboard to copy and paste. Keep repeating this process until cell H1 contains nothing but references to cell A1. You end up with the following megaformula in one cell:
(" ",TRIM(A1),1))&RIGHT(TRIM(A1),LEN(TRIM(A1))-IF(ISERROR(FIND(" ",TRIM(A1),FIND(" ",TRIM(A1),1)+1)), FIND(" ",TRIM(A1),1),FIND(" ",TRIM(A1),FIND (" ",TRIM(A1),1)+1)))
When you're satisfied that the megaformula is working, you can delete the columns that hold the intermediate formulas because they are no longer used.
The megaformula performs exactly the same tasks as all the intermediate formulas — although it's virtually impossible for anyone to figure out, even the author. If you decide to use megaformulas, make sure that the intermediate formulas are performing correctly before you start building a megaformula. Even better, keep a single copy of the intermediate formulas somewhere in case you discover an error or need to make a change.
The only limitation to the megaformula technique is that Excel formulas can contain no more than 1,024 characters. Another way to approach this problem is to create a custom worksheet function in VBA.Then you could replace the megaformula with a simple formula,such as =NOMIDDLE(A1)
In fact, I wrote such a function to compare it with intermediate formulas and megaformulas.
Because a megaformula is so complex, you may think that using one would slow down recalculation. Actually, that's not the case. As a test, I created a worksheet that used a megaformula 65,536 times. Then I created another worksheet that used six intermediate formulas. I compared the results with the VBA function that I wrote. I recorded the statistics regarding the two methodologies; the results are shown in Table 3-6.
Table 3-6 INTERMEDIATE FORMULAS VERSUS MEGAFORMULA VERSUS VBA FUNCTION
Method Recalculation Time File Size
Intermediate formulas 10.8 24.4MB
Megaformula 6.2 8.9MB
VBA function 106.7 8.6MB
The actual results will vary significantly, depending on system speed and amount of memory installed.
As you can see, using the megaformula resulted in faster recalculations as well as a much smaller workbook. The VBA function was much slower—in fact, it wasn't even in the same ballpark. This is fairly typical of VBA functions; they are always slower than built-in Excel functions.
The three files used in this time test are available on the companion CD-ROM.
Was this article helpful?