Need to create a project schedule? Try our Gantt Chart for Excel!

Text Manipulation Formulas in Excel – The Ultimate Guide

Text Formulas in Excel

When you are cleaning up data in Excel, the Text-to-Columns and Flash Fill features are awesome, but sometimes you need to use formulas to manipulate text. In this article I'll demonstrate examples of the text formulas I commonly use, including LEN, TRIM, UPPER, LOWER, PROPER, CONCATENATE, INDIRECT, CHAR, FIND, SEARCH, SUBSTITUTE, LEFT, RIGHT, MID and REPLACE (and some others).

The examples start very simple and then get progressively more advanced as you scroll through the page, building upon earlier examples.

Do you have a text manipulation challenge that you need a formula to solve? Feel free to ask your question by commenting below.

Links to Text Formulas on this Page

1. Get the LENgth of a text string

=LEN("onetwothree")
Result: 11

This comes in handy when you need to write a title for a web page or complete a form with a limited number of characters. Just open a blank spreadsheet and type your title in cell A1. In B1, enter =LEN(A1).

2. Change case to UPPER, lower, or Proper

=UPPER("this text")   Result: THIS TEXT
=LOWER("THIS TEXT")   Result: this text
=PROPER("this text")   Result: This Text

3. Concatenate a text string

You can use CONCATENATE, the & operator, or the newer CONCAT and TEXTJOIN functions to concatenate strings. The following formulas combine a first name in cell A1 and a last name in cell B1 with a space in the middle. The result is "John Smith" for all four formulas.

A1="John"
B1="Smith"

=A1 & " " & B1
=CONCATENATE(A1," ",B1)
=CONCAT(A1:B1)
=TEXTJOIN(" ",TRUE,A1:B1)

Result: "John Smith"

NOTE The spaces before and after the & operator are not required - I've included the spaces only to help make the formula more readable.

The CONCAT and TEXTJOIN functions are new functions that requires an Office 365 subscription (they work in Excel Online). The CONCAT function is like CONCATENATE except that it lets you use a range of cells as an argument. The TEXTJOIN function lets you specify a delimiter and ignore blank values.

4. Use INDIRECT to create a reference from a text string

The INDIRECT function allows you to create a reference from a text string. The example below shows a reference to cell A5 in worksheet 'Sheet 2'. The single quotes around the worksheet name are only necessary if the worksheet name includes a space.

=INDIRECT("'Sheet 2'!A5")

Use INDIRECT if you want the worksheet name to be a text string chosen by the user. For example, you may want to do this if you have many identical worksheets and you want to create a summary table that uses the names of those worksheets as references in your lookup formulas.

The following example creates a reference to cell X5 in a worksheet that is named in cell A1.

A1="Sheet 2"
A2=INDIRECT("'" & A1 & "'!X5")

The INDIRECT function can be very useful in array formulas. For example, to create an array of numbers 1 through N, where N is a number contained in cell A1, you can use:

=ROW(INDIRECT("1:" & A1))

5. Use CHAR to return special characters

The CHAR function lets you return a character for a given numeric code. The UNICHAR function returns a character for a decimal Unicode value. Although most of the numeric codes for the CHAR function correspond to the ASCII codes, some may not be the same (such as codes 128-160).

The functions CODE and UNICODE are the opposites of CHAR and UNICHAR, returning the numeric value for the first character in a text string.

Use CHAR(34) to return the double quote " character

When you concatenate text and need to include double quotes in the displayed text, you can use the CHAR(34) or UNICHAR(34) function. Both the ASCII and Unicode value for double quotes is 34.

=CHAR(34) & "Hi World" & CHAR(34)
Result: "Hi World" (quotes included)

Use CHAR(10) to include a line break in a string

When using a formula to return a string, use CHAR(10) or UNICHAR(10) for a line break. See Custom Number Formats to learn how to add a line break within a custom number format (for chart labels and stuff like that).

="abc" & CHAR(10) & "def"
Result:
abc
def

! To display wrapped text with line breaks, the cell must also have the Word Wrap property toggled on.

TIP To quickly generate a list of characters based on their numeric code, enter =CHAR(ROW()) or =UNICHAR(ROW()) into cell A1 of a blank worksheet and copy the formula down.

See my article "Using UNICODE Characters in Excel" for more information.

6. SUBSTITUTE text within a string

The SUBSTITUTE function is very powerful. It can be used to replace ether ALL occurrences or just the Nth occurrence of a string with another character or text string. In the example below, we're replacing the # character with a space.

text = "one#two#three"
=SUBSTITUTE(text,"#"," ")   Result: "one two three"
=SUBSTITUTE(text,"#"," ",2)   Result: "one#two three"

7. Use TRIM to get rid of extra spaces

The TRIM function removes all regular spaces (ASCII character 32) except for a single space between words.

=TRIM("   Hi  World  ")
Result: "Hi World"  (quotes not included)

TRIM does not remove tabs, line breaks, or other nonprinting characters from the text. To remove the non-printing ASCII characters 0-31 (including the tab character), you can use the CLEAN function.

text="Hi		World" (contains two tabs)
=CLEAN(text)
Result: "HiWorld"

The problem with the CLEAN function is that it completely removes the characters, so words separated by tabs or newline characters will be combined, so you may end up with "HiWorld" when you would prefer "Hi World".

To change special characters to regular spaces, you can use the SUBSTITUTE function and then wrap the function with TRIM to remove extra spaces like this:

text="Hi		World" (contains two tabs)
=TRIM( SUBSTITUTE(text,CHAR(9)," ") )
Result: "Hi World"

NOTE Here is a short list of CHAR codes for commonly replaced characters: Tab (9), Newline (10), Carriage Return (13), Space (32), Non-Breaking Space (160), Special Quote Symbols: ‘(145), ’(146), “(147), ”(148)

8. Use FIND and SEARCH to get the position of text in a string

The FIND function for case-sensitive searching and the SEARCH function for case-insensitive searching will return the starting character position of a text string within another string.

=FIND("a","ooAooaoo",1)   Result: 6
=SEARCH("a","ooAooaoo",1)   Result: 3

The 3rd argument of the FIND and SEARCH functions is the starting character position to begin the search, with the default being 1 (the first character). You can use a nested FIND or nested SEARCH to find the position of the 2nd occurrence of a text string like this:

text="ooAooAoo"
=FIND("A",text,FIND("A",text,1)+1)

Evaluation Steps
Step 1: FIND("A","ooAooAoo",3+1)
Step 2: FIND("A","ooAooAoo",4)
Step 3: 6

You can do some really tricky things with SUBSTITUTE when combined with FIND or SEARCH. The following function allows you to find the location of the Nth occurrence of a string within another text string. In this example, we want to know the position of the 3rd space in the name.

text="Tim A. J. Crane"
=FIND("#",SUBSTITUTE(text," ","#",3),1)

Evaluation Steps
Step 1: =FIND("#","Tim A. J.#Crane",1)
Step 2: =10

9. Use MID, LEFT and RIGHT to extract text from a string

The MID function is like the substr() function in other coding languages. It extracts a string from within another string by specifying the starting character position and the number of characters to extract. The REPLACE function is similar except that it returns the original text string with the text replaced. The LEFT and RIGHT functions are like shorthand versions of MID for extracting text from the left or right end of a string.

SYNTAX: =MID(text,start_num,num_chars)
SYNTAX: =REPLACE(text,start_num,num_chars,replace_text)
SYNTAX: =LEFT(text,num_chars)
SYNTAX: =RIGHT(text,num_chars)

Below is an example showing how these functions work.

text = "one#two#three"
=MID(text,5,3)   Result: "two"
=REPLACE(text,5,3,"BLAHBLAH")   Result: "one#BLAHBLAH#three"
=LEFT(text,5)   Result: "one#t"
=RIGHT(text,7)   Result: "o#three"

I haven't had much use for the REPLACE function, because I typically use SUBSTITUTE instead of REPLACE.

The MID, LEFT, and RIGHT functions become much more powerful when you use the FIND or SEARCH functions within them. Some of the following tips show examples of that.

10. Count the number of spaces in a text string

You can use this technique to count other characters besides spaces. For example, just substitute " " with "," or ";" to count the number of commas or semi-colons.

text = "Todd Allen Smith"
=LEN(text)-LEN(SUBSTITUTE(text," ",""))

Evaluation Steps
Step 1: LEN("Todd Allen Smith")-LEN("ToddAllenSmith")
Step 2: 16-14
Step 3: 2

The SUBSTITUTE function in this example returns a new text string with the spaces removed (replacing all " " with ""). We are subtracting the length of that modified text string from the original length to calculate the number of spaces in the original text.

11. Count occurrences of a string within text

If you want to count the number of occurrences of a string within text (instead of just a single character), then you can use a slightly modified version of the above formula. In this case, we'll just divide the result by the length of string.

text = "A##B##C"
string = "##"
=(LEN(text)-LEN(SUBSTITUTE(text,string,""))) / LEN(string)
Result: 2

12. Split text into columns using formulas

The Text-to-Columns Wizard and Flash Fill (Ctrl+e) features in Excel are fast and simple to use, but there may be times when you want to use formulas instead (to make a more dynamic or automated worksheet). Splitting up text using formulas typically involves a combination of LEFT, RIGHT, MID, LEN, and FIND (or SEARCH). We'll start with a couple simple formulas.

Extract the First Name

To extract the first word (or name) from a text string, you can use the following formula, where text is either a cell reference or a string surrounded by double quotes like "this".

text = "Tom Sawyer"
=LEFT(text,FIND(" ",text)-1)

Evaluation Steps
Step 1: =LEFT("Tom Swayer",4-1)
Step 2: =LEFT("Tom Swayer",3)
Step 3: ="Tom"

In the above formula, FIND(" ",text) returns the numeric position of the first space " " within the text. We subtract one from that value so the space is not included in the result.

Extract the Text After the First Space

To return the rest of the string after the first space, we use the RIGHT function, which extracts a specified number of characters from the end of the string. We calculate the number of characters to extract by subtracting the position of space from the total length of the string:

text = "Jay Allen Reems"
=RIGHT(text,LEN(text)-FIND(" ",text))

Evaluation Steps
Step 1: =RIGHT("Jay Allen Reems",LEN("Jay Allen Reems")-4)
Step 2: =RIGHT("Jay Allen Reems",15-4)
Step 3: =RIGHT("Jay Allen Reems",11)
Step 4: ="Allen Reems"

We could repeat these formulas in other columns to extract Allen and then Reems.

The article "Split text into different columns with functions" on support.office.com provides various examples of formulas for separating names into different parts based on different ways that a name may be written.

The SPLIT function in Google Sheets

I hope that Excel eventually includes a SPLIT function like the one available in Google Sheets. For example, to split a name like "Allen James Reems" into separate cells only requires the following simple formula:

=SPLIT(text," ")

If you'd like to see a SPLIT function in Excel, vote on this suggestion via excel.uservoice.com.

13. Get the last word in a string

For this example, we'll use the name "Allen Jay Reems" to show how to get the last word in a string, where a space character is the delimiter. This example shows how I sometimes build a more complicated formula using intermediate steps.

delimiter = " "
last_name
   =RIGHT(text,LEN(text)-position_of_last_delimiter)
position_of_last_delimiter
   =FIND("^", SUBSTITUTE(text,delimiter,"^",number_of_delimiters))
number_of_delimiters
   =LEN(text)-LEN(SUBSTITUTE(text,delimiter,""))

The final formula looks like this with A1="Allen Jay Reems" and will return the last name "Reems":

=RIGHT(A1,LEN(A1)-FIND("^", SUBSTITUTE(A1," ","^",LEN(A1)-LEN( SUBSTITUTE(A1," ","") ))))

If you have a string delimited by commas like "one, two, three, four" you can extract the last element by replacing " " with "," in the above formula and wrapping the entire thing with TRIM to remove the leading space.

If your string might not contain any spaces, then you can wrap the entire formula with IFERROR to return an empty string or the original text.

If your string contains the "^" character, you'll need to choose a different temporary delimiter to use in the formula such as "~" or another uncommonly used character.

14. Get the Nth word in a string

This function is really crazy, but useful. I learned it from a post on mrexcel.com. Basically what is going on is that you replace the delimiter text with a bunch of blank spaces so that you create a new text string that can be divided into chunks, where each chunk contains a different word. There will be a lot of space surrounding each word, so you use TRIM to remove it.

text = "One#Two#Three" (the original text)
delimiter = "#" (the delimiter text)
word_num = 2 (the word to extract)

=TRIM(MID(SUBSTITUTE(text,delimeter,REPT(" ",LEN(text))),(word_num-1)*LEN(text)+1,LEN(text)))

Evaluation Steps
1: =TRIM(MID(SUBSTITUTE(text,"#",REPT(" ",13)),(2-1)*13+1,13))
2: =TRIM(MID(SUBSTITUTE(text,"#","             "),14,13))
3: =TRIM(MID("One             Two             Three",14,13))
4: =TRIM("  Two        ")
5: ="Two"

In Google Sheets, this is a piece of cake. The SPLIT function returns an array, so you can return the 3rd word in a string using:

=INDEX(SPLIT(text,delimiter),3)

15. Convert a text string to an array of words

Want to convert "One#Two#Three" into an array like {"One";"Two";"Three"} that can be used within other formulas? That is what the SPLIT function in Google Sheets does, but to do this in Excel is still possible - it's just complicated. First, start with the formula in the previous section and replace word_num with the following:

=ROW(INDIRECT("1:"&((LEN(text)-LEN(SUBSTITUTE(text,delimiter,"")))/LEN(delimiter)+1)))

To display the results within an array of cells, remember to use Ctrl+Shift+Enter. Use TRANSPOSE if you want to display the results of this formula in a row instead of a column.

To create the array as an inline text string, you can use the following formula:

text = "One#Two#Three" (the original text)
str = "#" (the delimiter text)
="{"&CHAR(34)&SUBSTITUTE(text,str,CHAR(34)&";"&CHAR(34))&CHAR(34)&"}"

Resulting text string: {"One";"Two";"Three"}

16. Convert a text string to an array of characters

If you want to split a text string into an array of individual characters, such as converting "abcd" to {"a";"b";"c";"d"}, the formula is fairly simple. This formula is entered as an Array Formula (Ctrl+Shift+Enter).

=MID(text_string,ROW( INDIRECT("1:"&LEN(text_string)) ),1)

To convert each of the characters in a string to their numeric codes, wrap the above function with CODE or UNICODE. The formula would be entered as a multi-cell array to display each of the numeric values in a different cell.

The following formula in Google Sheets will convert a text string to a comma-delimited list of numeric code values.

text="Hello"
=ARRAYFORMULA( TEXTJOIN(",",TRUE, CODE(MID(text,ROW( INDIRECT("1:"&LEN(text)) ),1))) )
Resulting text string: "72,101,108,108,111"

17. Use EXACT for case-sensitive text comparisons

If you ever need to determine if the text in a cell is UPPERCASE, lowercase, or Proper Case, you can use the EXACT formula to compare the original text to the converted text.

The following formulas return TRUE if the text in cell A1 is uppercase, lowercase or proper case, respectively:

=EXACT(A1,UPPER(A1))
=EXACT(A1,LOWER(A1))
=EXACT(A1,PROPER(A1))

Note: The SUMIF and COUNTIF article provides a lot of different examples of text-based comparisons.

Have a Text Formula Challenge?

If this article hasn't answered your question, feel free to comment below if you have a problem that you want solved using a text formula. Make sure to provide sufficient detail for your question to be answered. Thank you!

Related Content (may include ads)

Comments

9 comments… add one
  • Hi- I am familiar with excel, but I only at a basic level. I have started collecting data on a spreadsheet. It is formatted as a table and I have filters and a few pivot tables, but now my boss wants to see this data in a different summary format. For example, I have listed all our shipments details such as: vendor, customer, destination, shipping cost, , etc.; he wants to be able to just filter by customer name, or port, or vendor, and be able to get the high and low cost of shipping. I can do this by filtering the table, but I think he wants a format that he can use without all that other data. I am limited in my excel knowledge and don’t know how to create what he needs. I’ve researched, but while I see formulas that could help, I just don’t understand how to use them. Would you please help? I would really appreciate it. Thank you!

    Reply
  • Good Morning,

    I was wondering if there was a formula to convert numbers as follows…
    #####.#### with no decimal
    4 twould need to read 000040000
    1.20 would need to be 000012000
    12.3666 to 000123666

    Any advice would be greatly appreciated.

    Thank you, Krista

    Reply
    • @Krista, I would probably use the formula =value*10000 and then use a Custom Number Format of 000000000. Or, you could use the formula =TEXT(A1*10000,”000000000″) where your list of numbers starts with cell A1. The result of the TEXT formula will be text (not a number), but maybe that’s what you want.

      Reply
  • I have a list of names where the parts of the names are all jumbled up! I need to arrange the names in a particular order so that the number of unique names can be identified and then we can apply the VLOOKUP formula for further calculations.

    The name Juãn Carlos Da Silva would appear either as Juãn Carlos DA SILVA or DA SILVA Juãn Carlos etc.
    The name J. Morrison would appear either as J. MORRISON or MORRISON J.

    There are hundreds of thousands of rows with about 10000 unique names (may be more!), each form of the name appearing multiple times! The following are the properties I could locate myself:

    1. Each name would have at least 1 first name and 1 last name.
    2. Each name has 0 to 4 middle names.
    3. Each part of the name, the first, last as well as the middle name(s), has at least 2 characters.
    4. The last name is always in Upper Case.
    5. The first name is always in Proper Case or only the first letter in upper case followed by a “.” (dot).
    6. The middle name(s) may be either in Proper Case or in Upper Case.
    7. Each part of the name may contain the Latin-1 supplement characters like ã, õ, ù, Á, Ç, Ü etc, as is there in the example given above.
    8. Each part of the name may contain “-” (hyphen).
    9. The last name does not have the “.” (dot).
    10. The middle name(s) may have the “.” (dot) or also the “-” (hyphen).
    11. The “.” (dot), if it is there, is at the end of the first or a middle name.
    12. No name has more than one “.” (dot).
    13. The first letter of each name is an upper case letter.
    14. The proper case words in the name appear next to each other, either at the beginning or at the end. So are the upper case words in the name.

    Need a formula to convert the names to make them appear in one particular form. Using code is not an option. Macro is disabled as of now! Formula compatible with Excel 2007 would be much appreciated, however formula compatible with Excel 2010 would still work!

    Reply
    • @Michael … The answer to your question might require a full article to explain in detail. Without Googling to see if there is a more elegant solution, I’d probably tackle the problem by first splitting the names into columns (using the Text to Columns feature).

      Then, sort the names in each row in alphabetical order so that you could more easily compare duplicates. In other words, you want “J. C. MORRISON” and “MORRISON J. C.” to both become “C. J. MORRISON” so that you can later concatenate the names into a single column to use for identifying duplicates.

      To make this a piece of cake, I would copy or upload the list of names into Google Sheets because of the awesome SORT function. If your split names are located in columns A-D, then the following formula copied down in column E would create a new set of columns with each row sorted alphabetically: =TRANSPOSE(SORT(TRANSPOSE(A2:D2),1,TRUE))

      This is just a method for getting the data into a form that can be used to easily identify duplicates. If you need additional help, I would recommend contacting an expert Excel consultant to help you.

      Reply
      • Hi Jon,

        Thanks for the response. Also, let me apologize for not stating the problem more precisely!

        I already have a set of data with about 36 columns (Column A through AJ). The names appear in the 1st or the leftmost column A. I need to get the “formatted name” in an inserted column in each of the rows. There are and will be multiple occurrence of the same name in multiple rows. But if the names are in the same format, it would be easy and accurate to prepare the reports using VLOOKUP and Pivot Table and other standard Excel formula. Presently, because of the ways the names appear, VLOOKUP and Pivot Table are considering the same person as two or more different people!

        Also, (not sure if this changes the significance of the problem in any manner) let me point out that the kind of name you suggested in your response, C. J. Morrison isn’t there in the list, as it has 2 “.” (dot) characters. As I mentioned earlier, in point number 12, there’s at most 1 “.” (dot) in a name and that isn’t in the last name (mentioned in point 9).

        Reply
        • That’s fine. Just insert a bunch of columns as needed so that you can work with the names. Or temporarily copy the names into a separate spreadsheet so that you can do the manipulation (making sure to preserve the row order).

  • thank you for sharing !!!

    Reply

Leave a Comment (comments are manually approved)

Your Name will be displayed along with your comment. Your Email and IP address are stored with the comment and used to identify/prevent spam (via a service provided by Automattic.com), but are not shared publicly. See our privacy policy to learn more.