How to Extract Text from a Cell in Microsoft Excel
Useful Excel Functions for Text Extraction
There are 5 main functions I find particularly useful when manipulating text in Microsoft Excel. You may already be familiar with these functions, but when used creatively, they can accomplish pretty much anything you need to do regarding text manipulation in Excel.
1. =len(text) - This length function calculates the number of characters in a text string. Useful in combination with other functions to extract the text you want.
2. =left(text,[num_chars]) - This function returns a text string of a specified length (input as [num_chars]), starting with the leftmost character in the selected text string.
3. =right(text,[num_chars]) - This function returns a text string of a specified length (input as [num_chars]), starting with the rightmost character in the selected text string.
4. =mid(text, start_num, num_chars) - This function returns a text string of a specified length (input as num_chars), starting with a specific character number (input as start_num) in the selected text string.
5. =find(find_text, within_text, [start_num]) - This function searches a text string (within_text) for a character or string (find_text) and returns the character number at which the searched character is found.
Hopefully you can start to see how powerful these 5 functions can be when used in combination.
Example 1: Extracting First 3 (or Last 3) Characters from a String
Let's start out with a basic example. Say you want to take just the first three characters from a text field. The LEFT function can do that. See the screenshots to the right. The formula has two inputs, first the text string you wish to extract from (A2 in this case), and second the number of characters you wish to extract (3).
The same methodology can be applied with the RIGHT function to extract the last three characters (or digits) from a string.
You can do this to return any number of characters from the string. To return only the first (or last) two characters, replace the number 3 with the number 2 in the [num_chars] argument of the LEFT or RIGHT function.
Example 2: Extracting Text from the Middle of a String
Pulling text from the middle of a string can be a little bit trickier than starting from the left or the right, but as long as you know the starting point (character #) within the string and the length of the segment you want to extract, the MID function will do the trick.
NOTE: If the starting point is not always the same character number, see Example 5 below
Here you will need to input two different numbers as arguments to the MID function. The first will be the starting point of the string you wish to extract, 4 in the example to the right. The second will be the length of the segment you wish to extract. In the example to the right, the formula is written as =MID(A2,4,3). This will return the 4th, 5th, and 6th characters from the string. These numbers can be changed to achieve the result you are looking for.
Example 3: Extracting a Variable Number of Characters from the Beginning of a String
This is where things start to get exciting. Let's say you have a list of names in the format Last Name, First Name. You want to extract the Last Name from the string. You know you want to use the LEFT function, but not all the last names are the same length. This is where the FIND function comes in handy. Because there is a comma separating the Last Name from the First Name in our data set, we can use the FIND function to return the character number of the comma and only return the characters that come before. See the example to the right.
Note how these two formulas are used in combination to achieve the desired result. Instead of specifying a fixed number of characters to return, we call another function to tell us where the comma is. The FIND function requires two inputs. First, the text you are searching for in quotation marks ("," in this scenario). Second, the text string you want to search in (A2). This returns the character number the first comma is found at. If you have more than one comma in the searched text this formula will return only the location of the first. We then have to subtract one from the result, as we don't want the comma to be included in the text string returned.
This can be used in any situation when you have a dividing character such as the comma in our example between the text you want to extract and the rest of the string. Ideally, the dividing character would only occur once in the string to eliminate the possibility of the FIND formula turning up the wrong result.
Example 4: Extracting a Variable Number of Characters from the End of a String
This is the same situation as above, but now we want to extract what comes after the dividing character. Continuing our example, now we want to extract the First Name from the string. We can do this using the RIGHT function, but now we have to determine the length of the first name in order to cut the string off at the right point.
In the example to the right, we now are using three functions together. We are using the LEN function to return the total number of characters in the string. If we again use the FIND function to return the location of the comma, we can subtract that from the total number of characters to return the number of characters after the comma. Because there is a space between the comma and the first letter of the first name, we again subtract one from the number returned.
Example 5: Extracting Text from a Variable Place within the Middle of a String
The last example I will give in this article is to be used when there is a string of defined length within the larger string that needs to be extracted, but the starting point is not always the same. In order for this to work, there has to be a characteristic that all the strings being extracted share. In the example to the right, that characteristic is the letters YR at the end.
Here, we use the MID function to extract the string we are looking for, but the starting point is defined using the FIND function. We search for "YR" and take the starting character number, then subtract one to get to the character before. The string we are looking for is three letters long, so the last input to the function is the number 3. This is just one example, and the formula can be manipulated a number of ways to fit the specific application.
As you can see, these five functions can become very powerful tools when used creatively and in combination. There are many other applications I have not outlined above, so feel free to post your favorites in the comments section below. Please also post any challenges regarding text extraction and manipulation in Excel that you haven't been able to solve and I will do my best to help out.