18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating Formulas That Manipulate Text 12<br />

The next formula examines the text in cell A1 and returns the position of the first three-character sequence<br />

that has a hyphen in the middle of it. In other words, it looks for any character followed by a hyphen and<br />

any other character. If cell A1 contains the text Part-A90, the formula returns 4.<br />

=SEARCH(“?-?”,A1,1)<br />

Searching and replacing within a string<br />

You can use the REPLACE function in conjunction with the SEARCH function to replace part of a text string<br />

with another string. In effect, you use the SEARCH function to find the starting location used by the<br />

REPLACE function.<br />

For example, assume that cell A1 contains the text Annual Profit Figures. The following formula<br />

searches for the 6-letter word Profit and replaces it with the word Loss:<br />

=REPLACE(A1,SEARCH(“Profit”,A1),6,”Loss”)<br />

This next formula uses the SUBSTITUTE function to accomplish the same effect in a more efficient manner:<br />

=SUBSTITUTE(A1,”Profit”,”Loss”)<br />

Advanced Text Formulas<br />

The examples in this section appear more complex than the examples in the preceding section. But as you<br />

can see, these examples can perform some very useful text manipulations. Space limitations prevent a<br />

detailed explanation of how these formulas work, but this section gives you a basic introduction.<br />

ON the CD-ROM<br />

You can access all of the examples in this section on the companion CD-ROM. The file is<br />

named text formula examples.xlsx.<br />

Counting specific characters in a cell<br />

This formula counts the number of Bs (uppercase only) in the string in cell A1:<br />

=LEN(A1)-LEN(SUBSTITUTE(A1,”B”,””))<br />

This formula works by using the SUBSTITUTE function to create a new string (in memory) that has all the<br />

Bs removed. Then the length of this string is subtracted from the length of the original string. The result<br />

reveals the number of Bs in the original string.<br />

The following formula is a bit more versatile; it counts the number of Bs (both uppercase and lowercase) in<br />

the string in cell A1. Using the UPPER function to convert the string makes this formula work with both<br />

uppercase and lowercase characters:<br />

=LEN(A1)-LEN(SUBSTITUTE(UPPER(A1),”B”,””))<br />

Counting the occurrences of a substring in a cell<br />

The formulas in the preceding section count the number of occurrences of a particular character in a string.<br />

The following formula works with more than one character. It returns the number of occurrences of a particular<br />

substring (contained in cell B1) within a string (contained in cell A1). The substring can consist of<br />

any number of characters.<br />

=(LEN(A1)-LEN(SUBSTITUTE(A1,B1,””)))/LEN(B1)<br />

217

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!