11
Most of the functions you’ve learned so far focus on crunching raw numbers.
But Excel also provides functions that work with other types of data, including dates, times, and ordinary text. For text, for example, you may want Excel to pull first and last names from two different columns and join them in a single column.
Or you may want to find and remove a word that appears in a bunch of column titles.
Similarly, you may want to replace a character in a word, capitalize a name, or count the number of letters in a cell. Excel provides specialized text functions for all these tasks, and you’ll learn about them in this chapter.
Excel also gives you specialized functions for dates and times. These functions perform some indispensable tasks, like retrieving the current time and determin- ing what day of the week a given date falls on. In addition, Excel lets you perform calculations with dates and times just as you would with ordinary numbers. This chapter introduces these techniques, too, and explains how Excel stores dates and times behind the scenes.
Manipulating Text
You can’t use arithmetic operators like + and – with text. If you try to, Excel displays a #VALUE error message. However, there’s one operator you can use: the concat- enation operator (&), which joins together text. For example, imagine you have an individual’s first name in cell A1, and the last name in cell B1. You could join the values from these two cells to create a full name with this formula:
=A1 & B1
Manipulating Dates, Times, and Text
ManipulaTing
TExT This approach has one drawback: In all likelihood, the first- and last-name cells don’t include any leading or trailing spaces. That means that when you join the two names, Excel will fuse them into a single word, like JohnSmith. One solution is to explicitly add a space (between quotation marks) into your formula, like so:
=A1 & " " & B1
The important concept in this example is that you can enter string literals—fixed pieces of text (including spaces)—as easily as you can enter literal numbers. The only difference between entering literal text and literal numbers is that you have to place text between quotation marks. You can stitch together as many pieces of text as you want; there’s no limit. The next group of functions showcases the many ways that Excel lets you manipulate text.
Concatenation also works with cells that contain numbers. In these cases, the “text”
is simply the cell content formatted with the General number format, no matter what number format the cell uses. For example, if you format the number 43.2 so it appears as the currency value $43.20 in a cell, that number automatically reverts to the ordinary 43.2 when you join it to a piece of text using concatenation. This is often a different result from the one you want, particularly if the cell contains date information, which Excel displays as a serial number in the General number format.
(For more on how number formats affect the appearance of dates, see page 137.) To avoid these problems, use the TEXT() function described on page 325.
CONCATENATE(): Joining Strings of Text Together
The CONCATENATE() function lets you join text in exactly the same way as the concatenation operator (&) does. CONCATENATE() joins all the parameters you supply into one long piece of text, in the order you specify them.
Here’s how you rewrite the name-joining formula shown earlier using CONCATE- NATE() with two pieces of text:
=CONCATENATE(A1, " ", B1)
LEFT(), MID(), and RIGHT(): Copying Portions of a Text String
Just as you can join pieces of text, so you can split up a string of text. The LEFT(), MID(), and RIGHT() functions let you extract a portion of text from a larger text string. For example, the LEFT() function takes two arguments: the text you want to examine, and the number of characters that Excel should extract, starting from the string’s left side:
LEFT(text, num_characters)
To take the first four letters from the text in cell A1, you’d use the formula:
=LEFT(A1, 4)
Assuming the cell contains the text tofurkey, this formula would give you tofu.
ManipulaTing The RIGHT() function performs the same operation, but extracts letters starting from TExT
the right side of the string. For example, consider the following formula:
=RIGHT(A1, 5)
If you use this function with the same text string, you end up with the text urkey. The MID() function is more powerful than the LEFT() and RIGHT() functions, as it has the ability to extract a consecutive series of characters from anywhere inside a string. When using the MID() function, you need to supply three arguments: the text you’re evaluating, the starting position of the extraction, and the number of charac- ters you want to retrieve. Excel numbers each letter and space in a string, starting with 1 for the first character, 2 for the second character, and so on. That means that if you specify a starting position of 3 and a length of 2, Excel extracts the third and fourth characters from a string. The basic formula looks like this:
MID(text, start_position, number_of_characters)
Here’s an example that copies characters from the middle of a string. If the cell A1 contains the text Swanky Franks, the following formula returns the value Frank.
=MID(A1, 8, 5)
NOTE LEFT(), MID(), and RIGHT() all pluck out the string you specify, but leave the cell’s original content unchanged.
LEN(), FIND(), and SEARCH(): Counting Characters in a String
The LEFT(), RIGHT(), and MID() functions let you copy specified segments of a string.
But what happens if you don’t know the exact length of the string you’re searching?
For example, if you’re interested in retrieving last names from a column that contains full names, none of these functions help you since last names vary in length.
Fortunately, Excel provides some other tools that can help you out. Three of these, LEN(), FIND(), and SEARCH(), give you numeric information about text. This section explains how to use each of these functions, and then shows how to combine them with the LEFT(), RIGHT(), and MID() functions to perform some really powerful operations.
To begin with, LEN()—short for LENgth—counts the number of characters in a string of text. For example, the result of the following formula is 5:
=LEN("Hello")
The FIND() function is more sophisticated. It gives you a number representing the position of a given character or series of characters. For example, it can tell you where a space is located in a phrase. If there’s more than one match, the FIND() function returns the position of only the first match. The FIND() function can take an optional third parameter that tells the function where to begin the search—if
ManipulaTing
TExT you leave it out, Excel starts searching at the beginning of the text. Here’s what the basic formula looks like:
FIND(find_text, within_text, [start_position])
Now consider the following example. It gives you a result of 5, indicating that the first space in the phrase “Mind the Gap” is in position 5.
=FIND(" ", "Mind the Gap")
SEARCH() works in almost exactly the same way, and it takes the same two or three arguments. The only difference is that FIND() performs a case-sensitive search, which means that it looks for upper- and lowercase letters that match those in your search term, whereas SEARCH() doesn’t care about matching cases.
The LEN(), FIND(), and SEARCH() functions all become even more useful when you combine them with the LEFT(), RIGHT(), and MID() functions. For example, say you have a column of full names, as shown in Figure 11-1, and you want to copy the first and last names into their own new columns.
The first step is to use the FIND() function to find the space that separates the first name from the last name. You can then take that number and subtract 1 from it to get the length of the first name. Finally, you can use the newly calculated length with the LEFT() function to snip out the first name. Here’s the formula that puts all these steps together:
=LEFT(A2,FIND(" ", A2)-1)
You can see the result in cell B2 of Figure 11-1.
You use a similar trick to get the last name, but with the RIGHT() function:
=RIGHT(A2,LEN(A2)-FIND(" ", A2))
Figure 11-1 uses this formula in cell C2. At first glance, the formula might look a bit overwhelming, so it helps to remember the two arguments that the RIGHT() func- tion uses. The first argument is the cell to evaluate (A2). The second argument is the number of characters that RIGHT() should take from the right side of the text, using the calculation LEN(A2)–FIND(" ", A2). Here’s how the second argument breaks down: First, the FIND() function locates the space between the first and last names. Next, the LEN() function determines how many characters (including spaces) make up the full name. Finally, Excel subtracts the FIND() result from the LEN() result to determine the length of the last name, which is what the RIGHT() function needs to do its job.
TIP The previous example uses a custom formula to split text, but Excel has a feature designed for just this purpose, called the Text to Columns feature. Simply select the cells you want to change, and then choose DataData ToolsText to Columns. Excel launches the Convert Text to Columns Wizard, which lets you choose how to carve up text into separate columns (either by fixed position, or by using a recognized delimiter character, like a space or a comma). The Convert Text to Columns Wizard is based on the Text Import Wizard, which you’ll explore in detail on page 754.
ManipulaTing TExT
FiGuRE 11-1 This worksheet shows the LEFT(), RIGHT(), FIND(), and LEN() functions at work. Excel uses these functions to extract the first and last names from the full names in column A.
UPPER(), LOWER(), and PROPER(): Changing Capitalization
Another way to manipulate text is by changing its case. Excel provides three func- tions for this purpose (see Figure 11-2):
• UPPER() converts text to all capitals.
• LOWER() converts text to all lowercase.
• PROPER() converts text to initial-case. That means every letter is lowercase, except for the first letter in each word. (Excel identifies words by looking for the spaces between them.)
FiGuRE 11-2 This worksheet shows how the UPPER(), LOWER(), and PROPER() functions change the capitalization of the names in column A.
ManipulaTing
TExT All three of these functions need just one argument, which is a string of text (a short bit of text, anywhere from a few letters to a lengthy phrase). For the argument, you can reference a cell that contains text, or type in a piece of literal text—as long as you remember the quotation marks. For example, the following formula displays the text contained in cell A1, but changes all the characters to lowercase:
=LOWER(A1)
GEM IN THE ROUGH
Using Text Functions to Clean Up Your Data
Usually, functions like UPPER(), LOWER(), and PROPER() trans- form the appearance of text that’s already in your spreadsheet.
But you could have a tricky time figuring out exactly how to do this transformation.
Say you’ve got a list of first and last names in column A (be- ginning in cell A1) whose letters are all uppercase. You want to change these names so that only the first letter of each name is capitalized. Here’s what to do: First, insert a new column B.
Next, enter the formula =PROPER(A1) in cell B1, and then copy this formula to the rest of the cells in column B. Because this formula uses a relative cell reference (as explained on page 251), Excel automatically adjusts the formula for each cell that you paste it into.
Once you take this step, Excel displays the properly capitalized names in column B. But you can’t just copy and paste these reformatted names into column A because the formulas in column B reference the cells in column A. Instead, you need to select the corrected names from column B, copy them (Home→Clipboard→Copy), move to cell A1, and then choose Home→Clipboard→Paste→Values (not the plain-vanilla Home→Clipboard→Paste command).
This technique provides a quick way to clean up a number of problems. For example, you can use this approach with many other text functions, like TRIM(), CLEAN(), SUBSTITUTE(), TEXT(), FIXED(), DOLLAR(), all of which are explained in this chapter.
TRIM() and CLEAN(): Removing Unwanted Spaces and Non-Printing Characters
The TRIM() and CLEAN() clean up any strings of text you run through them. TRIM() removes any leading and trailing spaces; it also changes any series of more than one space to a single space. Thus, if you use TRIM() on the text string “ Hello There
“ the altered text becomes “Hello There.” TRIM() can be quite handy for fixing erratic spacing.
CLEAN() simply removes non-printable characters from a text string. Non-printable characters, which usually appear as empty-box icons in your text, tend to appear only if you import text that uses a format that Excel has difficulty understanding.
SUBSTITUTE(): Replacing One Sequence of Characters with Another
The SUBSTITUTE() function replaces a sequence of characters in a string with another set of characters. The function has three parts: the text you want to modify, the characters you’re looking to replace, and the replacement text you want to insert.
In addition, you can supply an optional occurrence number parameter, which Excel uses if it finds more than one match. For example, if Excel matches your search text three times and you supply 2 for the occurrence number, Excel changes only the
ManipulaTing second occurrence of the matched text. If you don’t supply the occurrence number, TExT
Excel changes all occurrences. Here’s what the function looks like:
SUBSTITUTE(text, old_text, new_text, [occurrence_number])
Consider the case where cell A1 contains the text It was the best of times; it was the worst of times. You could use the following formula to change the word “times”:
=SUBSTITUTE(A1, "times", "nanoseconds")
The result is the string It was the best of nanoseconds; it was the worst of nano- seconds.
On the other hand, the following formula explicitly replaces just the second occur- rence of “times.” The resulting string is It was the best of times; it was the worst of crimes.
=SUBSTITUTE(A1, "times", "crimes", 2)
NOTE The SUBSTITUTE() function always performs a case-sensitive search. That means that if you try using SUBSTITUTE() to replace the word it, in the previous example, Excel won’t match It.
TEXT(), VALUE(), FIXED(), and DOLLAR(): Converting Text to Numbers and Vice Versa
Sometimes, you may need to convert text into a number, or vice versa. For exam- ple, imagine you have a cell that contains the sentence “A good sandwich costs
$5.95.” Using the MID() function, you could copy just the part of this text that has the price—“5.95.” However, even though this text contains numeric information, to Excel it’s still a piece of text, so you can’t perform mathematical operations with it (like adding tax). On the other hand, you may have the reverse problem: You might want to show a string of text that includes a number from another cell. In these cases, the data conversion functions TEXT() and VALUE() are useful.
TEXT() converts an ordinary number into formatted text using the format you specify.
It always requires two arguments:
TEXT(number, format)
The first argument is the number you’re converting; the second is the format you want to use. You can use any of the date, time, or numeric formatting codes described in Chapter 5 (in particular, Table 5-2 on page 146). For example, the following formula converts the number 434.2 to the formatted text $434.20.
=TEXT(434.2,"$#,##0.00")
On its own, this method may not seem very practical. After all, you already learned how to control cell formatting using the Format Cells window. What’s the point of doing the same job with a formula? The answer is that you may find it handy when you want to perform some fancy text processing with your number.
ManipulaTing
TExT For example, imagine you have the price of a product in cell A1 (which happens to be 300). You could use the following formula to change the number to formatted text, and then put it into a complete sentence, like $300.00 is way too expensive.
=TEXT(A1,"$#,##0.00") & " is way too expensive"
Now compare what happens if you try to do the same thing without using the TEXT() function:
=A1 & " is way too expensive."
You end up with an unformatted result (300 is way too expensive), regardless of what formatting you used in cell A1.
In other words, you can use text-based functions with numbers, but unless you ex- plicitly perform the conversion using the TEXT() function, Excel always uses General formatting—which may not be what you want.
TIP You can also use the FIXED() or DOLLAR() functions (explained below) to convert content to specific formats without needing to find the right format string.
The VALUE() function does the reverse—it converts a piece of text into a number you can manipulate in a formula. Here’s an example:
=VALUE(A1)
This trick becomes useful if you need to extract a price from a string of text, and then perform a calculation with it. Here’s an example that gets the number 300 out of a sentence using the MID() function, and then converts it to a number using the VALUE() function:
=VALUE(MID("I suggest a price of $300.00 for the chair.",23,6))
NOTE In many cases, you can get by without the VALUE() function, because you can coax Excel into converting text into numbers. However, with some formulas, you need it—and it never hurts to make your formulas clearer by using it.
The VALUE() function is fairly simple to use, but it isn’t terribly bright. If you use it with content that contains both numeric characters and letters, like the string of text 42 bananas, it fails. You’ll see the error message #VALUE! instead of the desired content.
The FIXED() and DOLLAR() functions also convert numbers to text. The difference is that they use a set format (described shortly), so you don’t need to specify a format for the result, as you would with the TEXT() function.
The FIXED() function actually performs several steps. First, it rounds a number to a specified number of decimal places. Next, it formats the number with a decimal point and, optionally, with commas to separate the thousands. Finally, it converts the number to text. Here’s what the function looks like:
FIXED(number, [number_of_decimals], [no_commas])
ManipulaTing You need only the first argument. If you don’t specify the other arguments, the TExT
FIXED() function automatically uses two decimal places and includes commas if your number’s large enough to warrant them.
For example, the following formula gives you the text 5,450.59:
=FIXED(5450.586, 2)
If you don’t want commas, set the third argument to TRUE.
The DOLLAR() function automatically applies the currency format before it converts a value, so your text appears with a currency symbol before it. If you want, you can specify a number of decimal places, or just accept the default of two decimal places:
DOLLAR(number, [number_decimals])
NOTE Because the FIXED() and DOLLAR() functions give you text, not numbers, you’ll notice that cells bearing these functions are left-aligned (Excel’s default alignment for all text). If this bothers you, you can explicitly change the alignment. You’ll also notice that if you change the number format for any of these cells, it won’t have any effect. That’s because the cell contains text, not a number.
Other Text Functions
So far you’ve toured Excel’s most useful text-manipulation functions. The program provides a few lesser-used functions, too, which are outlined in Table 11-1.
TABlE 11-1 Miscellaneous text functions
FUNCTION SYNTAX DESCRIPTION
CHAR() CHAR(number) Returns the character for a spe- cific character code. For example, CHAR(100) corresponds to the lower- case letter d.
CODE() CODE(text) Returns the numeric code for the first character in a text string. For exam- ple, CODE(“d”) returns 100.
EXACT() EXACT(text1, text2) Compares two pieces of text, and re- turns TRUE if they match, and FALSE if they don’t. Usually, it’s just easier to use the IF() function with the equal sign for comparison, as described on page 239.
REPT() REPT(text, number_of_times)
Creates a text string by repeating the text you specify the number of times you specify. This function’s a quick way to repeat one or more characters in a cell.