Chapter 11. Text Functions: Letters as data

image with no caption

Excel loves your numbers, but it can also handle your text.

It contains a suite of functions designed to enable you to manipulate text data. There are many applications to these functions, but one that all data people must deal with is what to do with messy data. A lot of times, you’ll receive data that isn’t at all in the format you need it to be in—it might come out of a strange database, for example. Text functions shine at letting you pull elements out of messy data so that you can make analytic use of it, as you’re about to find out....

Lightning smashed into your office and wiped out all your hard drives, including your customer database. No problem. Just go get the backup disks, right?

Wrong. The guy in charge of backups forgot to do them (he sends his apologies). Fortunately, you have something you might be able to use. Word is, a garbled scrap of email sent a few days ago might have salvageable information about your contacts....

image with no caption

That email has your client list, all right, but the list isn’t looking so hot. All the data is mashed together. One of your employees loaded it and saved it to an Excel file for you....

Text to Columns is a great feature that lets you split your data into columns using a delimiter, which is simply a text character that signifies the breaks between the different data points. If your delimiter is, say, a period, Text to Columns will put the data to the left of the period in one column, the data to the right in another, and then it’ll delete the period.

image with no caption

If you have more than one type of delimiter, you might have to run Text to Columns more than once. In this case, you have a period acting as a delimiter, as well as a comma, and you could even treat those weird “s” characters as delimiters, which would make Excel throw them out.

image with no caption
image with no caption
image with no caption

For starters, you need a delimiter, or at least you need the data elements to be evenly spaced. Here you have neither: the “s” characters aren’t evenly spaced, they aren’t delimiters, and there’s nothing that separates them from the next data element: the last name.

image with no caption

Better click Undo a couple times to start from scratch. You’re going to need some more firepower for this problem. Weren’t there formulas for dealing with text data?

Earlier you used the function VALUE() to convert text data to numbers, but VALUE() is just the beginning of Excel’s text functions. Excel has a whole suite of functions to deal with all sorts of situations in which you need to change or query text data.

image with no caption

You’re going to use a few of these to clean up this database, and in the future when you have text problems that Text to Columns can’t solve, you should check out the Help files for other function-based solutions.

You need to extract characters on the left side of your cells (the “s” characters) and on the right side of your cells (the phone numbers). To do this, you can use the LEFT() and RIGHT() functions. Here’s the syntax.

image with no caption

You’ll put a formula with this function in a new cell, and the formula will point to your original raw data and say how many characters to grab.

In the RIGHT() formula you used to extract phone numbers, you told Excel to extract 12 characters, which works for all the phone numbers. But the count of “s” characters varies among the cells—from one character to five.

image with no caption

So when you create a LEFT() formula to extract the “s” characters, the value of your second argument somehow needs to vary among the cells.

That data you’re working on is really important, and without it your employees are starting to have problems.

image with no caption

Better punch through those first and last names really quickly!

You know, because you already extracted values on both ends of your raw data, it’d be nice if you could use that information to get the name out. It’d be nice if you could use the data you’ve extracted to trim the ends off of your raw data.

image with no caption

Let’s use the stars and phone fields to whittle down the original. That way, breaking apart the last name and first name will be easier.

The spreadsheet is getting complex, but we’re making lots of progress. Go ahead and copy/paste the two formulas you just created for the remaining rows in your spreadsheet.

image with no caption

You need a way to extract the last name from the data you created in column D. It seems clear that you can use a LEFT() formula, but you need to create another formula-based argument to specify the number of characters to grab.

You need a formula that will state the numerical position of the comma. When you use it as your argument, your LEFT() formula will know just how many characters to grab to return the last name.

FIND() is a function that returns a number that states where a search string can be found within a piece of text. Say you were looking for the position of the text “x” in the expression “Head First Excel”.

image with no caption

Why would you need a function like this? Well, for starters, you could use it in conjunction with a LEFT() or RIGHT() formula to extract a number of characters that varies from formula to formula.

Let’s use FIND() to extract our Last Name field....

There’s a little snag when it comes to running this operation on the data you created in the Messy 2 column:

image with no caption

Text to Columns does what it says it does: take text and break it into columns. But here Excel wants to treat your formulas as text. This won’t work: you need to take the formulas and render them as values.

Paste Special is a fantastically helpful operation in Excel that lets you copy something and then—rather than paste an exact copy of the original—paste a modification of the original.

image with no caption

You can use Paste Special to paste the values that the formulas you’ve copied return, rather than the formulas themselves. And that is just what you need to do with your Messy 2 column data.

image with no caption

This chapter started off with a real mess: you received a pile of jumbled-up data, which was all you had left of your customer database. But with the help of Excel’s powerful text formulas, you fixed that messy data right up.

image with no caption
image with no caption