This is part three of the Excel Tutorial Series. Read all tutorial posts by browsing the Tutorial category.
Text functions are used to manipulate data found inside cells in your Excel worksheet. We'll be exploring five different text functions in this post:
Descriptions of each function were taken from: Excel Function Reference
LEFT Function
- Usage:
- LEFT(text,num_chars)
- LEFT returns the first character or characters in a text string, based on the number of characters you specify.
Arguments:
- Text is the text string that contains the characters you want to extract.
- Num_chars specifies the number of characters you want LEFT to extract.

RIGHT Function
- Usage:
- RIGHT(text,num_chars)
- RIGHT returns the last character or characters in a text string, based on the number of characters you specify.
Arguments:
- Text is the text string that contains the characters you want to extract.
- Num_chars specifies the number of characters you want RIGHT to extract.

MID Function
- Usage:
- MID(text,start_num,num_chars)
- MID returns a specific number of characters from a text string, starting at the position you specify, based on the number of characters you specify.
Arguments:
- Text is the text string that contains the characters you want to extract.
- Start_num is the position of the first character you want to extract in text. The first character in text has start_num 1, and so on.
- Num_chars specifies the number of characters you want MID to return from text.

FIND Function
- Usage:
- FIND(find_text,within_text,start_num)
- FIND locates one text string within a second text string, and return the number of the starting position of the first text string from the first character of the second text string.
Arguments:
- find_text is the text you want to find.
- within_text is the text containing the text you want to find.
- start_num specifies the character at which to start the search. The first character in within_text is character number 1. If you omit start_num, it is assumed to be 1.

LEN Function
- Usage:
- LEN(text)
- LEN returns the number of characters in a text string.
Arguments:
- Text is the text whose length you want to find. Spaces count as characters.

After reading the above, I hope you can understand how simple, yet how powerful these functions are. It helps if you think of them as I/O functions: you put in a certain type of value, and get a certain type of value back. If I could peek inside Excel and see how the functions are compiled, I imagine it would look something like this (or whatever the equivalent is in C++):
Function Left(Text As String, Num_Chars As Long) As String Function Right(Text As String, Num_Chars As Long) As String Function Mid(Text As String, Start_Num As Long, Num_Chars As Long) As String Function Find(Text_To_Find As String, Text As String, Start_Num As Long) As Long Function Len(Text As String) As Long
So the first three return strings as their value, while Find and Len return numbers. Let's review some samples so we can see how this works.
Extract information from cells
In a cell with two words and exactly one space, the LEFT function can extract the first word with the following formula:
=LEFT(A1,FIND(" ",A1)-1)
When I'm constructing a formula such as this, it helps sometimes to work backwards. So we'll start with the answer for this particular problem, and generalize the solution to work with any two-word cell.
=LEFT(A1,4)
Recall that the second argument of the Left function requires a number representing the number of characters you want to extract. The Find function returns a number representing the position of the found text inside the specified string. What we need is a count of characters to extract, to use as the second argument of the Left function.
I see that there's a space right after the 'n' in 'John', and it's the fifth character. I need a way to tell Excel that I want to extract everything up to that character. I remember the Find function will return the position of the space within the string (fifth position):
=LEFT(A1,FIND(" ",A1))
But we don't want to extract the space, so we subtract one from the total to get four. The function then becomes
=LEFT(A1,FIND(" ",A1)-1)
When you evaluate it, you can watch the formula work itself out.
- =LEFT(A1,FIND(" ",A1)-1)
- =LEFT("John Smith",5-1)
- =LEFT("John Smith",4)
Extracting the last name is a similar process. Here are three formulas that can do it.
- =MID(A1,FIND(" ",A1)+1,LEN(A1))
- =MID(A1,FIND(" ",A1)+1,LEN(A1)-FIND(" ",A1))
- =RIGHT(A1,LEN(A1)-FIND(" ",A1))
The only difference between the first and second formulas are that the first one just blindly extracts using the length of the entire string, whereas the second one actually calculates the exact number of characters to extract.

Extracting parts of phone numbers is done in a similar fashion. If cell A1 has the phone number "(800) GET-HELP":
- To extract area code: =MID(A1,FIND("(",A1)+1,3)
- To extract exchange: =MID(A1,FIND(")",A1)+2,3)
- To extract last four: =RIGHT(A1,4)

The last one is obvious, so I'll walk through the first two so you can see how they evaluate.
=MID(A1,FIND("(",A1)+1,3)
=MID("(800) GET-HELP",1+1,3)
=MID("(800) GET-HELP",2,3) (starting from the second character, extract the first three characters from the string "(800) GET-HELP")
=MID(A1,FIND(")",A1)+2,3)
=MID("(800) GET-HELP",5+2,3)
=MID("(800) GET-HELP",7,3) (starting from the seventh character, extract the first three characters from the string "(800) GET-HELP")
We can also format social security numbers the same way. Given a nine digit number in cell A1:
- To add hyphens: =LEFT(A1,3)&"-"&MID(A1,4,2)&"-"&RIGHT(A1,4)
- To remove hyphens: =LEFT(A1,3)&MID(A1,5,2)&RIGHT(A1,4)
Use the first one to add hyphens to a nine digit number, the second to remove hyphens from an existing SSN. See Format SSN for more samples.

The beautiful thing about all of these formulas is that you can fill them across a row and/or down a column, to extract from multiple cells at the same time. So if you had a list of phone numbers in column A (assuming they all have the same format as above), you would put "=MID(A1,FIND("(",A1)+1,3)" in B1, "=MID(A1,FIND(")",A1)+2,3)" in C1, and "=RIGHT(A1,4)" and fill down as needed to separate each number into its components.
Further reading:
Excel Text Functions
Built-In Excel Functions
Excel Text Formulas
Awesome! Thanks to this tutorial you saved me hours of manual work!
Cell B6 = Device HH1A-3550-24-1
This cell always will have "Device xxnx-…" The first two Letters are what is extracted.
Cell B7 = Interface Fa0/10 (100M-FD) j5r7744
I am trying to extract the information after the "j" = "one to four characters possible"
Also trying to extract information after the second "r" = "one to four characters possible"
Cell B19
Value: HH-44 jack 5
Formula:
=IF(ISERR(CONCATENATE(MID(B6,8,2),"-",RIGHT(D18,(SEARCH("r",D18,1)))," jack ",LEFT(D18,SEARCH("r",D18,1)-1))),"",CONCATENATE(MID(B6,8,2),"-",RIGHT(D18,(SEARCH("r",D18,1)))," jack ",LEFT(D18,SEARCH("r",D18,1)-1)))
I did this because nesting can be tricky.
Cell B18
Value: 5r7744
Formula:
=RIGHT(B7,LEN(B7)-FIND("j",B7)*1)
As you can see the results are not always consistent when the jack and room numbers change number of place holders.
Not sure if you care to help but it would be nice and much appreciated.
For cell B7, here's a formula that will extract everything past the "j":
=MID(B7,FIND("~",SUBSTITUTE(B7," ","~",4))+1,255)
You can adapt this formula to extract the second r as well:
=MID(B7,FIND("~",SUBSTITUTE(B7," ","~",4))+3,255)
or change it to explicitly look for the "r":
=MID(B7,FIND("~",SUBSTITUTE(B7,"r","~",2)),255)
I was not really clear on my first question. But I looked over your example and tweaked it somewhat for the results we need. I appreciate your inspiration. One question, How does the tilde ~ know it is looking for the second "r"? I gathered it is a wild card if it is followed by special characters.
—————
=IF(ISERR(CONCATENATE(MID(B6,8,2),"-",SUBSTITUTE(MID(B7,FIND("~",SUBSTITUTE(B7,"r","~",2)),255),"r","",1)," ",SUBSTITUTE(MID(B7,FIND("j",B7,1),((FIND("r",B7,11))-(FIND("j",B7,1)))),"j","jack "))),"Missing Jack or Room #",CONCATENATE(MID(B6,8,2),"-",SUBSTITUTE(MID(B7,FIND("~",SUBSTITUTE(B7,"r","~",2)),255),"r","",1)," ",SUBSTITUTE(MID(B7,FIND("j",B7,1),((FIND("r",B7,11))-(FIND("j",B7,1)))),"j","jack ")))
——————–
Paste into cells the below data:
B6 = Device HH1A-3550-24-1
B7 = Interface Fa0/10 (100M-FD) j1234r5678
Result of above formula pasted as one line of text:
HH-5678 jack 1234
Never really used function "Substitute" prior to this, so thanks.
The fourth argument for SUBSTITUTE tells the function which instance to replace. So you can choose which instance of "r" you want to look for. In this case, it replaces the second "r" with the "~" character. Then the FIND function looks for the "~" in the new string, but MID extracts everything to the right of the original (unaltered) string.
Hi JP. Thanks for this great tutorial. I know how to convert a text to columns, but this one I just can't figure out how:
I need to move the fields Address, City, PCode, Province, Country etc and convert them into separate column headings and underneath the data. Thanks in advance for your help.
The list I would like to convert is as follows:
Name, Address: the address
City: the city
Postal Code: the postal code
Province: the province
Country: the country
and again, (the list contains more than 200 names):
Name, Address: the address
City: the city
Postal Code: the postal code
Province: the province
Country: the country
If your data is vertical, but you want it to be tabular, you're going to have a tough time.
Are there empty rows between each data set? If not, I would invest the time to add some manually. That will make the data easier to work with in VBA. For example, if each data set had an empty row separating it from the next data set, you could loop through the Areas collection to work with each set. Then you could either transpose it onto another worksheet, or paste it row by row.
Thank you JP. As I couldn't find any helpful documentation, I realized this could be a tough one
Yes, there is an empty row between each set of data. Could you please elaborate a bit around the concept of Areas collection? Maybe with an example?
Any script suggestion to help convert my vertical sets of data to a tabular one would be vrey appreciated. Thank you again!