Page 259 - Excel 2007 Bible
P. 259
17_044039 ch12.qxp 11/21/06 11:05 AM Page 216
Part II
Working with Formulas and Functions
Replacing text with other text
In some situations, you may need to replace a part of a text string with some other text. For example, you
may import data that contains asterisks, and you need to convert the asterisks to some other character. You
could use Excel’s Home ➪ Editing ➪ Find & Select ➪ Replace command to make the replacement. If you
prefer a formula-based solution, you can take advantage of either of two functions:
n SUBSTITUTE replaces specific text in a string. Use this function when you know the character(s)
to be replaced but not the position.
n REPLACE replaces text that occurs in a specific location within a string. Use this function when
you know the position of the text to be replaced but not the actual text.
The following formula uses the SUBSTITUTE function to replace 2006 with 2007 in the string 2006
Budget. The formula returns 2007 Budget.
=SUBSTITUTE(“2006 Budget”,”2006”,”2007”)
The following formula uses the SUBSTITUTE function to remove all spaces from a string. In other words, it
replaces all space characters with an empty string. The formula returns 2007OperatingBudget.
=SUBSTITUTE(“2007 Operating Budget”,” “,””)
The following formula uses the REPLACE function to replace one character beginning at position 5 with
nothing. In other words, it removes the fifth character (a hyphen) and returns Part544.
=REPLACE(“Part-544”,5,1,””)
Finding and searching within a string
Excel’s FIND and SEARCH functions enable you to locate the starting position of a particular substring
within a string:
n FIND finds a substring within another text string and returns the starting position of the sub-
string. You can specify the character position at which to begin searching. Use this function for
case-sensitive text comparisons. Wildcard comparisons are not supported.
n SEARCH finds a substring within another text string and returns the starting position of the sub-
string. You can specify the character position at which to begin searching. Use this function for
non-case-sensitive text or when you need to use wildcard characters.
The following formula uses the FIND function and returns 7, the position of the first m in the string. Notice
that this formula is case-sensitive.
=FIND(“m”,”Big Mama Thornton”,1)
The formula that follows, which uses the SEARCH function, returns 5, the position of the first m (either
uppercase or lowercase):
=SEARCH(“m”,”Big Mama Thornton”,1)
You can use the following wildcard characters within the first argument for the SEARCH function:
n Question mark (?) matches any single character.
n Asterisk (*) matches any sequence of characters.
TIP If you want to find an actual question mark or asterisk character, type a tilde (~) before the
TIP
question mark or asterisk.
216