In this chapter
Using 3D Formulas to Spear Through Many Worksheets 824
Combining Multiple Formulas into One Formula 828
Calculating a Cell Reference in the Formula by Using the INDIRECT
Function 830
Assigning a Formula to a Name 833
Replacing Multiple Formulas with One CSE Formula 838
Excel offers an amazing variety of formulas. This chapter covers some of the unorthodox formulas that you can build in Excel. In this chapter, you will learn about the following:
INDIRECT
functionRow()
or Column()
to return an array of numbersIt is common to have a workbook composed of identical worksheets for each month or quarter of the year. Every worksheet needs to have the same arrangement of rows.
If you want to total a particular cell across all of the worksheets, you might try to write a formula with one term for each sheet; for example, =Sheet1!A1+Sheet2!A1+Sheet3!A1.... However, Excel supports a special type of formula that will spear through several worksheets to add a particular cell from each worksheet. The syntax of the formula is =SUM(Sheet1:Sheetn!A1).
As shown in Figure 29.1, Net Income is in Row 22 on the January worksheet and is in the same row on the December worksheet. You cannot see this in Figure 29.1, but the arrangement of rows is identical on every worksheet.
When creating a worksheet, you might be tempted to write a formula like the one in Figure 29.2 that adds up each of the 12 worksheets, but doing so would be rather tedious.
Instead, you can write a formula that totals Cell B4 from each worksheet, Jan through Dec. The syntax of the formula is =SUM(Jan:Dec!B4)
. After you enter this formula in Cell B4, you can easily copy it to all the other relevant cells in the worksheet, as shown in Figure 29.3.
When you have an arrangement of several sequential worksheets, you might wish to keep a running total. This total would be calculated as the total on this sheet plus the running total from the previous sheet.
It is somewhat difficult to build a formula that will always point to the previous sheet. Many try this wrong approach: build a formula on Sheet2 that points to Sheet1. When you make copies of Sheet2, to Sheet3, Sheet4, and so on, the formula continues to always point back to Sheet1. This is rarely what you want.
The solution involves a tiny user-defined function that can be written in Excel’s macro editor.
This specific example shows you how to build a general purpose function that will return a value from a previous worksheet. This function can work in any situation.
Figure 29.4 shows a formula that returns the value from the previous month. On the Feb worksheet, this would refer to =Jan!B4
. You could easily copy this formula to other cells within the Feb worksheet. However, if you copy the formula to Mar or Apr, the formula still points to the Jan worksheet, which is not what you want.
Excel offers a very cool solution to this problem. The solution requires a few lines of VBA macro code. Don’t be afraid. I will get you there and back without any problems. Here’s what you do:
Your screen should look similar to Figure 29.5.
To realize the power of this function, you can put the workbook in Group mode and enter the function in 11 worksheets at once:
=PrevSheet(B4)
, as shown in Figure 29.6.
PrevSheet
solves the prior month problem seamlessly across all the worksheets.Tip From
To examine worksheets from the same workbook, select View, Window, New Window to create a second window of the workbook. Then select View, Window, Arrange, Vertical, OK to arrange the windows vertically. You now have two views of the same workbook, and you can change the right pane to be a different worksheet. The screenshot in Figure 29.7 reflects four new window commands before the windows are arranged vertically.
With more than 350 functions available in Excel, it is possible to perform just about any calculation. Many times, however, it is easier to break the task down into many subformulas as you are trying to solve the problem.
For example, fellow Excel MVP and guru Bob Umlas once taught me that I could use the Substitute
function to locate the last space in a word. This is handy for finding the last word in a sentence or name. However, unlike Bob, I always need to build this formula over the course of several columns. It takes me seven columns to do a trick that Bob can do in one. Figure 29.8 shows all the formulas used to replicate the trick.
After you have puzzled out a complicated set of interrelated functions to achieve a result, you can begin consolidating the formulas into one monster formula.
There is an easier way to combine many formulas into one formula. In general, follow these steps:
I realize this may be difficult to follow in the abstract. If you would like to follow along with a real example, follow these steps:
G2
in the formula bar, as shown in Figure 29.11.
G2
with the formula from Cell G2, as shown in Figure 29.12
G2
and C2
in the Cell H2 formula. Note that pasting the formula from Cell G2 introduces references to Cell F2.INDIRECT
FunctionUsually a formula points to a particular cell or range of cells. Sometimes, though, you want a formula to point to a different cell as the result of a calculation. You can do this by using the INDIRECT
function.
In general, this process involves writing a text-based formula that evaluates to a cell address. Although your particular situation will certainly be different, here are some examples of formulas that evaluate to a cell address:
After you have a formula that evaluates to text that looks like a cell reference, you can use that formula as the argument to the INDIRECT()
function. Excel will return the value in the cell indicated by the text formula.
One concrete example: if cell Z99 contains the value 1, then the formula of =INDIRECT("Z99")
will return a value of 1. A more practical concrete example follows.
Say that in Figure 29.14, you would like to build a table to copy the current-month totals from each worksheet to a summary table on the Total worksheet. Without the INDIRECT
function, you would have to separately enter 12 different formulas in Row 4—one for each month (for example, =Jan!$B4
for January, =Feb!$B4
for February, and so on).
You can solve this problem with a single formula that you can copy to the entire total worksheet. Follow these steps:
=E$3&"!"
. Note that the $
before the 3
ensures that as the formula is copied to lower rows in the summary table; it will always point to the month heading in Row 3.CELL
. The CELL
function can return many bits of information about a reference, including the address of the cell. For example, =CELL("address",$B4)
returns the text $B$4
.=E$3&"!"&CELL("Address",$B4)
into the table.
The last step is to wrap the INDIRECT
function around the formula in Cell E4. This tells Excel to evaluate the function inside INDIRECT
to learn that Excel should return the value from Cell B4 on the Jan worksheet.
The whole trick to being efficient in Excel is being able to write one formula that can be copied to an entire range. Rather than going through the tedium of entering 12 different formulas in Row 4, you can use the INDIRECT
function to enter just one formula everywhere in the range (see Figure 29.16).
INDIRECT
function to allow one formula to fill the entire table.Note
Excel gurus will point out another benefit of the INDIRECT function. If you have a formula such as =SUM(A1:A10)
, and you insert a new Row 5, the formula normally expands to =SUM(A1:A11)
. However, there might be a time when you really want to sum only the first 10 records on the sheet. In this case, you can use the formula =SUM(INDIRECT("A1:A10"))
to always point to Rows 1 through 10, no matter what rows are inserted or deleted.
Caution
There is an important limitation with INDIRECT
functions. If you build an INDIRECT
function that points to an external workbook, the formula works only when the external workbook is open.
When you first read the Help topic on the OFFSET
function, you might wonder what would be the point of such a function. The OFFSET
function allows you to describe a range by specifying five parameters:
It would be difficult to imagine why you would ever use =OFFSET(A1,2,3,4,5)
to refer to the range D3:H6. However, when you consider that these arguments can be functions that calculate the size of a range, it starts to make sense.
Say that you start entering invoice amounts in Cell B2 and proceed down Column B. In order to write a sum formula that can expand to include any number of entries in Column B, you could count the number of numeric entries in Column B by using =COUNT(B:B)
. If you then use the COUNT
function as the fourth argument in the OFFSET
function, you have set up a dynamic formula that will always expand as new items are entered, as shown in Figure 29.17.
OFFSET
function allows you to describe a rectangular range that starts a calculated number of rows and columns from a starting point.When you set up a named range, the Names box shows that the name has a value like =Sheet1!A1
. Because this value contains an equals sign, you know that this value is actually a formula.
It is possible to assign a very complex formula to a name. Say that you have a workbook with 100 worksheets, with 20 columns of X and Y data in each worksheet, as shown in Figure 29.18. You want to continually update a transformation formula used on all the X and Y points. Every time the formula changes, you have to copy the new formula to all 20 columns on the 20 worksheets.
The technique involves writing a relative formula that will carry out the same transformation as your original formula. Assign this formula to a name. In each of the cells, use =NamedFormula instead of the formula.
The advantage is that you can now edit the formula in the Edit Name box and the new formula will be used throughout the workbook.
The following specific examples walk through the steps for one particular formula.
While you usually write formulas in A1 notation made popular by VisiCalc and Lotus 1-2-3, Microsoft still supports the old R1C1 notation that was used in Microsoft Multimate back in 1982. In the R1C1 notation, a cell such as E2 is referenced as R2C5, meaning Row 2, Column 5. The following tip makes use of R1C1 notation.
You can use an R1C1 version of the INDIRECT
function to create a formula to take the cosine of a cell two cells to the left of the formula cell and divide it by the sine of the cell to the left of the formula cell. To do so, you follow these steps:
→ For a complete discussion of R1C1 style references, see “Using R1C1 Style Formulas,” page 987, in Chapter 36.
=COS(SUM(INDIRECT("RC[-2]",False)))
.=SIN(SUM(INDIRECT("RC[-1]",False)))
.=COS(SUM(INDIRECT("rc[-2]",FALSE)))/SIN(SUM(INDIRECT("rc[-1]",FALSE)))
to a name such as MyFormula, as shown in Figure 29.19.
=MyFormula
, as shown in Figure 29.20.
The Transpose option in the Paste Special dialog is great for changing values that span across several columns into values that go down a column. Here’s an example:
However, there is not a good way to copy the calculation for profit from Row 4 to the new table. You normally have to enter 12 different formulas in the range B7:B17 as shown in Figure 29.23.
But there are two ways to easily enter a single formula that will turn those results on their side.
First, you can use the OFFSET
function you learned about earlier in this chapter. You can set up an OFFSET
function that points to $A$4
and offsets by an additional column as you copy the formula down the rows. Try it:
=ROW(A1)
. The result is the number 1.=ROW(A1)
formula down a column to create a sequence of integers.=ROW(A1)
as the third argument in the OFFSET
function. A formula of =OFFSET($A$4,0,ROW(A1))
will achieve the perfect result, as shown in Figure 29.25.ROW(A1)
trick as the Column Offset parameter in the OFFSET
function to turn a range on its side.One danger exists with just about every method described in this chapter: They produce results that the average person does not understand. So if you want to end up with straightforward formulas in B7:B18, you can use the following method:
=B4
in Cell B5.=
to !
. This converts the formulas to text, as shown in Figure 29.26.
!
back to =
to change to formulas.!
back to =
. It now looks as if you actually typed all 12 formulas individually.There exists a wildly powerful type of formula that most Excel users have never experienced. This formula can do thousands of calculations in a single formula.
The formula is known as an array formula. You must use Ctrl+Shift+Enter when entering an array formula to tell Excel to evaluate the formula as an array.
Here is an example of the power of an array formula.
Consider the dataset shown in Figure 29.28. This database has transactional data showing quantity, unit price, and unit cost. The goal is to enter a single formula that will multiply each quantity by each price and sum the result.
Most people would add a new column with formulas to multiply quantity times price as shown in Figure 29.29. This is easy to do. However, it adds 5,000 new formula cells to your worksheet.
It is possible to enter a calculation in one cell that will do the 5,000 calculations and total them. With tables in Excel 2007, you have two choices for writing this formula:
Follow these steps:
As shown in Figure 29.30, Excel will correctly calculate the result.
If you fail to use Ctrl+Shift+Enter, Excel will return a #VALUE error, as shown in Figure 29.31.
#VALUE!
error.SUMPRODUCT
It is possible to convert the previous array formula to a SUMPRODUCT
formula:
You can then enter this as a regular formula instead of an array.
3.141.47.163