
|
|
|
Make Nested Functions Work in Excel
Getting nested functions to work is easy when
you trick Excel features into mimicking functions and
subroutines
[Discuss (8) | Link to this hack] |
You're either a genius or a fool if you select an Excel
spreadsheet cell, type an equals sign, pound out a multilevel nested
function start to finish—and expect everything to work just the
way you planned. It's easier to write code in even
the most cryptic of programming languages. There, you often have a
development environment that provides tips for function parameters;
you can code and separately test subroutines and objects, or you can
construct code stubs to gradually build up a working program. At
first glance, Microsoft Excel appears to offer no more than the
Insert Function command, which
shows you the parameters only for a top-level function. However, you
can simulate subroutines and stubs in out-of-the-way worksheet cells,
and check the accuracy of your formulas every step of the way.
Suppose you want to create a formula that indicates whether you
should change your diversification in growth, value, and bond
investments, and you want to perform the following actions:
-
Calculate the formula only if the investment value is a valid number
(not text or blank).
-
Display a warning when the investment is a valid number and is more
than 25 percent of your total portfolio.
-
Use only half the investment value in the calculation if the
investment is in a blend-style mutual fund, because the fund splits
its investments between growth and value.
-
Extract the second word in the fund-style cell to determine the style.
Using Named Cells
Instead of using
cell references, such as B24, which can make
formulas quite inscrutable, assign descriptive names
to individual worksheet cells, ranges of cells, formulas,
or constants. Names are like variables and make formulas easier to
read, as illustrated in .
Example 1. Names make Excel formulas easy to decipher
= balance * interest_rate
Names are also more portable. You can reference a name in an Excel
worksheet throughout the entire workbook, and move formulas to other
cells or even to other worksheets within the workbook without
changing the name references they contain. If you're
going to use a value more than once, it's much
easier to calculate that value in a named cell and refer to the name.
Creating Stubs
For logical functions,
it's easy to
build a stub to check your logic before you plug
in calculations. For example, the Excel IF
function uses three parameters: the test condition, the result when
the test is true, and the result when the test is false, shown in
.
Example 2. The Excel IF function with its three parameters
= IF(test_condition, result_if_true, result_if_false)
You can type in a test condition along with hardcoded strings or cell
references to show the true and false results, as shown in .
Example 3. An example of the IF function
action = IF(ISNUMBER(investment),test_diversification,"Investment isn't a number.")
TIP
In this example, investment and
test_diversification are name references to cells
C2 and sheet2!A2, respectively.
Here's how the parameters work:
- Test condition
-
For the first parameter, specify a condition test that checks if
the investment is a number:
ISNUMBER(investment).
- Result if test is true
-
If the test condition is true (the investment is a number), you want
to test for diversification. For this parameter, you simply type the
name of the cell that performs the diversification test.
- Result if test is false
-
If the investment cell doesn't contain a number, you
want to display a warning. For this parameter, then, type the string:
Investment isn't
a number.
To test the formula, type the string Check
diversification in the
test_diversification cell. When you type a valid
number in the investment cell, the
action cell should display the string
Check diversification. When you type a string or
leave the investment cell blank, the
action cell should display Investment
isn't a number.
This formula is done! Because it works and refers to other cells, it
won't change, regardless of future calculations. For
example, you can replace the text in the
test_diversification cell with the formula that
actually tests your portfolio diversification. shows another example of simplifying logical
tests using strings and cell references.
Example 4. Named cells and text strings simplify the diversification test
= IF(InvestPercent > 0.25,"Check diversification","Leave it alone")
Creating Excel Subroutines
To create a true subroutine in Excel—one that you can call with
parameters—you must use macros
and
Visual Basic for Applications (VBA). However, you can mimic the
behavior of a subroutine using intermediate calculations in several
cells, where each cell uses the results of other cells. By combining
this divide-and-conquer approach with named cells, each intermediate
calculation is easy to decipher and test.
TIP
You can nest functions only to seven levels in Excel. If you have a
particularly deep nesting problem, you can sidestep this limitation
by calculating portions of your problem in other cells.
The three components of the test for diversification show how this
subroutine technique works. In this example, we'll
develop the lower level tasks first:
-
Determine the fund style.
-
Calculate the amount of the investment to use.
-
Calculate the percentage that the investment represents of your total
portfolio.
Hacking the Hack
If you plan to use the top-level formula more than once—for
instance, to evaluate each holding in your portfolio—you
won't want to name dozens of cells or create
separate formulas for each application of the formula. Not only does
it take too much time, it's mind-numbingly tedious
as well. However, after you get your calculations working properly,
it's easy to go back and paste the intermediate
calculations into the top-level formula. You can then copy the
complete formula to other cells. To incorporate subroutine
calculations into a top-level formula, start at the top and work your
way down, replacing cell references with the contents of the cell, as
shown here.
The original top-level formula is:
=IF(ISNUMBER(Investment),test_diversification,"Investment isn't a number.")
You can replace the named cell references with the formulas in those
cells or, for references to values, the cell address, as shown in the
following steps:
-
Select the test_diversification cell and select
the formula without the equals sign in the formula bar.
-
Press Ctrl-C to copy the formula to the Windows Clipboard. Be sure to
press Esc to break out of edit mode in the
test_diversification cell.
TIP
If you don't press Esc to stop editing, Excel will
add every cell you select and every character you type to the formula
in that cell.
-
Next, replace the cell reference Investment with
its cell address, C2. The resulting formula looks like this:
=IF(ISNUMBER(C2),IF(InvestPercent> 0.25,"Check diversification",
"Leave it alone"),"Investment isn't a number.")
At this point, you can repeat Steps 1 through 4 to replace the cell
reference InvestPercent with the formula in that
cell. Then, repeat Steps 1 through 4 until you either replace all
cell references with their corresponding calculations or run out of
nesting levels. If you replace every intermediate calculation, the
diversification formula looks like the one in .
Example 8. The diversification test as a nested function
=IF(ISNUMBER(C2),IF(IF(MID(B2,FIND(" ",B2,1)+1,
LEN(B2)-FIND(" ",B2,1))="Blend",C2*0.5,C2)
/Portfolio> 0.25,"Check diversification","Leave it alone"),
"Investment isn't a number.")
TIP
In , you can leave the
Portfolio cell reference in place, because each
holding calculation uses the total
portfolio value.
Showing messages 1 through 6 of 6.
-
help me plz
2006-09-02 06:20:34
miaanfaiz
[View]
-
help me plz
2007-02-08 03:18:01
suvarnasonu
[View]
-
nested if Condition
2006-08-28 04:17:30
nestedifcondition
[View]
-
IF conditions
2005-03-26 04:34:25
judith_sk
[View]
-
How to write more than seven IF statements in Ms-Excel
2005-01-09 22:57:30
Google_Gopi
[View]
-
How to write more than seven IF statements in Ms-Excel
2005-11-19 07:59:25
lksseven
[View]
|
Showing messages 1 through 6 of 6.
|
|
O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website:
| Customer Service:
| Book issues:
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
|
|
data
male
if age <20
score>75 marks=5
score>72 marks=4
score>70 marks=3
score>67 marks=2
score>64 marks=1
score<62 marks=0
if age <25
score>73 marks=5
score>70 marks=4
score>67 marks=3
score>65marks=2
score>63 marks=1
score<60 marks=0
if age <30
score>70 marks=5
score>68 marks=4
score>65 marks=3
score>63marks=2
score>60 marks=1
score<57 marks=0
if age <35
score>67 marks=5
score>65 marks=4
score>62 marks=3
score>60marks=2
score>58 marks=1
score<55 marks=0
if age <40
score>65 marks=5
score>63 marks=4
score>60 marks=3
score>57marks=2
score>55 marks=1
score<53 marks=0
if age <45
score>63 marks=5
score>61 marks=4
score>58 marks=3
score>55marks=2
score>52 marks=1
score<50 marks=0
if age <50
score>60 marks=5
score>58 marks=4
score>55 marks=3
score>53marks=2
score>51 marks=1
score<48 marks=0
if age >51
score>57 marks=5
score>55 marks=4
score>51 marks=3
score>48marks=2
score>43 marks=1
score<40 marks=0
female
if age <20
score>70 marks=5
score>68 marks=4
score>65 marks=3
score>62 marks=2
score>60 marks=1
score<58 marks=0
if age <25
score>68 marks=5
score>65 marks=4
score>62 marks=3
score>59marks=2
score>57 marks=1
score<55 marks=0
if age <30
score>65 marks=5
score>62 marks=4
score>60 marks=3
score>57marks=2
score>53 marks=1
score<50 marks=0
if age <35
score>63 marks=5
score>60 marks=4
score>58 marks=3
score>54marks=2
score>51 marks=1
score<48 marks=0
if age <40
score>61 marks=5
score>58 marks=4
score>55 marks=3
score>53marks=2
score>51 marks=1
score<48 marks=0
if age <45
score>57 marks=5
score>54 marks=4
score>51 marks=3
score>48marks=2
score>45 marks=1
score<41 marks=0
if age <50
score>55 marks=5
score>53 marks=4
score>51 marks=3
score>48marks=2
score>45 marks=1
score<43 marks=0
if age >51
score>53 marks=5
score>50 marks=4
score>48 marks=3
score>46marks=2
score>43 marks=1
score<59 marks=0
plz male formula to calculate marks