The tutorial shows how to use the Substring functions in Excel to extract text from a cell, get a substring before or after a specified character, find cells containing part of a string, and more.
Before we start discussing different techniques to manipulate substrings in Excel, let's just take a moment to define the term so that we can begin on the same page. So, what is a substring? Simply, it's part of a text entry. For example, if you type something like "AA-111" in a cell, you'd call it an alphanumeric string, and any part of the string, say "AA", would be a substring.
Although there is no such thing as Substring function in Excel, there exist three Text functions (LEFT, RIGHT, and MID) to extract a substring of a given length. Also, there are FIND and SEARCH functions to get a substring before or after a specific character. And, there are a handful of other functions to perform more complex operations such as extracting numbers from a string, replacing one substring with another, looking up partial text match, etc. Below you will find formula examples to do all this and a lot more.
How to extract substring of a certain length
Microsoft Excel provides three different functions to extract text of a specified length from a cell. Depending on where you want to start extraction, use one of these formulas:
- LEFT function - to extract a substring from the left.
- RIGHT function - to extract text from the right.
- MID function - to extract a substring from the middle of a text string, starting at the point you specify.
As is the case with other formulas, Excel substring functions are best to learn from an example, so let's look at a few ones.
Extract substring from start of string (LEFT)
To extract text from the left of a string, you use the Excel LEFT function:
Where text is the address of the cell containing the source string, and num_chars is the number of characters you want to extract.
For example, to get the first 4 characters from the beginning of a text string, use this formula:
=LEFT(A2,4)
Get substring from end of string (RIGHT)
To get a substring from the right part of a text string, go with the Excel RIGHT function:
For instance, to get the last 4 characters from the end of a string, use this formula:
=RIGHT(A2,4)
Extract text from middle of string (MID)
If you are looking to extract a substring starting in the middle of a string, at the position you specify, then MID is the function you can rely on.
Compared to the other two Text functions, MID has a slightly different syntax:
Aside from text (the original text string) and num_chars (the number of characters to extract), you also indicate start_num (the starting point).
In our sample data set, to get three characters from the middle of a string beginning with the 6th character, you use the following formula:
=MID(A2,6,3)
Tip. The output of the Right, Left and Mid formulas is always text, even when you are extracting a number from a text string. If you want to operate on the result as a number, then wrap your formula in the VALUE function like this:
=VALUE(MID(A2,6,3))
Extract substring before or after a given character
As shown in the above examples, the Left, Right and Mid functions cope nicely with uniform strings. When you are dealing with text strings of variable length, more complex manipulations shall be needed.
Note. In all of the below examples, we will be using the case-insensitive SEARCH function to get the position of a character. If you want a case-sensitive formula, use the FIND function instead.
How to extract text before a specific character
To get a substring preceding a given character, two things are to be done: first, you determine the position of the character of interest, and then you pull all characters before it. More precisely, you use the SEARCH function to find the position of the character, and subtract 1 from the result, because you don't want to include the character itself in the output. And then, you send the returned number directly to the num_chars argument of the LEFT function:
For example, to extract a substring before the hyphen character (-) from cell A2, use this formula:
=LEFT(A2, SEARCH("-",A2)-1)
No matter how many characters your Excel string contains, the formula only extracts text before the first hyphen:
How to extract text after character
To get text following a specific character, you use a slightly different approach: get the position of the character with either SEARCH or FIND, subtract that number from the total string length returned by the LEN function, and extract that many characters from the end of the string.
In our example, we'd use the following formula to extract a substring after the first hyphen:
=RIGHT(A2,LEN(A2)-SEARCH("-",A2))
How to extract text between two instances of a character
To get a substring between two occurrences of a certain character, use the following generic formula:
The first two arguments of this MID formula are crystal clear:
Text is the cell containing the original text string.
Start_num (starting point) - a simple SEARCH formula returns the position of the desired character, to which you add 1 because you want to start extraction with the next character.
Num_chars (number of chars to extract) is the trickiest part:
- First, you work out the position of the second occurrence of the character by nesting one Search function within another.
- After that, you subtract the position of the 1st occurrence from the position of the 2nd occurrence, and subtract 1 from the result since you don't want to include the delimiter character in the resulting substring.
For example, to extract text surrounded by two hyphens, you'd use this formula:
=MID(A2, SEARCH("-",A2) + 1, SEARCH("-",A2,SEARCH("-",A2)+1) - SEARCH("-",A2) - 1)
The screenshot below shows the result:
If you are looking to extract text between 2nd and 3rd or 3nd and 4th occurrences of the same character, you can use a more compact SEARCH SUBSTITUTE combination to get the character's position, as explained in How to find Nth occurrence of a character in a string:
In our case, we could extract a substring between the 2nd and 3rd hyphens with the following formula:
=MID(A2, FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),2))+1, FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),3)) - FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),2))-1)
How to find substring in Excel
In situations when you don't want to extract a substring and only want to find cells containing it, you use the SEARCH or FIND function as shown in the above examples, but perform the search within the ISNUMBER function. If a cell contains the substring, the Search function returns the position of the first character, and as long as ISNUMBER gets any number, it returns TRUE. If the substring is not found, the search results in an error, forcing ISNUMBER to return FALSE.
Supposing, you have a list of British postcodes in column A and you want to find those that contain the substring "1ZZ". To have it done, use this formula:
=ISNUMBER(SEARCH("1zz", A2))
The results will look something similar to this:
If you'd like to return your own message instead of the logical values of TRUE and FALSE, nest the above formula into the IF function:
=IF(ISNUMBER(SEARCH("1zz", A2)), "Yes", "")
If a cell contains the substring, the formula returns "Yes", an empty string ("") otherwise:
As you may remember, the Excel SEARCH function is case-insensitive, so you use it when the character case does not matter. To get your formula to distinguish the uppercase and lowercase characters, opt for the case-sensitive FIND function.
For more information on how to find text and numbers in Excel, please see If cell contains formula examples.
How to extract text from cell with Ultimate Suite for Excel
As you have just seen, Microsoft Excel provides an array of different functions to work with text strings. In case you are unsure which function is best suited for your needs, commit the job to our Ultimate Suite for Excel. With these tools in your Excel's arsenal, you just go to Ablebits Data tab > Text group, and click Extract:
Now, you select the source cells, and whatever complex strings they contain, a substring extraction boils down to these two simple actions:
- Specify how many characters you want to get from the start, end or middle of the string; or choose to extract all text before or after a given character.
- Click Insert Results. Done!
For example, to pull the domain names from the list of email addresses, you select the All after text radio button and type @ in the box next to it. To extract the user names, you select the All before text radio button, as shown in the screenshot below.
And you will get the following results in a moment:
Apart from speed and simplicity, the Extract Text tool has extra value - it will help you learn Excel formulas in general and substring functions in particular. How? By selecting the Insert as formula checkbox at the bottom of the pane, you ensure that the results are output as formulas, not values.
In this example, if you select cells B2 and C2, you will see the following formulas, respectively:
- To extract username:
=IFERROR(LEFT(A2,SEARCH("@",A2)-1),"")
- To extract domain:
=IFERROR(RIGHT(A2, LEN(A2)- SEARCH("@",A2) - LEN("@") + 1),"")
How much time would it take you to figure out these formulas on your own? ;)
Since the results are formulas, the extracted substrings will update automatically as soon as any changes are made to the original strings. When new entries are added to your data set, you can copy the formulas to other cells as usual, without having to run the Extract Text tool anew.
If you are curious to try this as well as many other useful features included with Ultimate Suite for Excel, you are welcome to download evaluation version.
More formulas for substrings in Excel
In this tutorial, we have demonstrated some classic Excel formulas to extract text from string. As you understand, there can be almost infinite variations of these basic scenarios. Below you will find a few more formula examples where the Text functions come in handy.
Available downloads
Excel substring functions - practice workbook (.xlsx file)
Ultimate Suite - trial version (.exe file)
441 comments
I want to extract the flat and Tower in this example is denoted by TL which comes right after the cheques from the left. Is there anyways to get it right?
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007747~902620102 CHQ. NO:007747 23062026972504242645 - AE0190095
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007746~902620102 CHQ. NO:007746 23062026972504242643 - AE0189708
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007724~902620102 CHQ. NO:007724 23062026972504242638 - AE0189619
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007723~902620102 CHQ. NO:007723 23062026972504242626 - AE0189194
CLEARING CHEQUESF1203 TL19 DEFAULT - CHQ. NO: 000131~302620177 23062026023062001250 - AE0173816
CLEARING CHEQUESF408 TL7 DEFAULT - CHQ. NO: 000097~102620128 23062026023062001249 - AE0173462
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007722~902620102 CHQ. NO:007722 23062026972504242630 - AE0141381
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007720~902620102 CHQ. NO:007720 23062026972504242633 - AE0141371
CLEARING CHEQUESIN-HOUSE CHEQUE TRANSFER CHQ. NO: 007726~902620102 CHQ. NO:007726 23062026972504242605 - AE0141335
OUTWARD CLEARINGF603 TL5 DEFAULT - ~2549518001 CHQ. NO: 000017~804020101 23061026023061001594 - AE0060948
CLEARING CHEQUEINWARD CLEARING CHQ. NO: 007717~902620102 CHQ. NO:007717 23061100000012570428 - AE0012593
I am not sure I fully understand what you mean.
I have a list of TEXT, over 90,000 lines . I copied from a webpage, approx 25,000 names and added it to my list. Now when I sort the list alphabetically, it does not work properly. For example the following is an extract
ABLAZING GRACE
ABLE BEAUTY
ABLE HIT
ABLE IVY
ABLE LASS
ABLE LOTTY
ABLE MAGPIE
ABLE MILLIE
ABLE QUEST
ABLE RAMON
ABLE SABLE
ABLE TO RUN
ABLE VIVA
ABLE BONNIE
ABLE CUSTOMER
ABLE FAME
ABLE LANE
ABLEBE
You will see that ABLE BONNIE, ABLE CUSTOMER, ABLE FAME and ABLE LANE are not in the correct order. These are what I added from a webpage. Interestingly, if I retype the name and resort my list, the retyped value is sorted correctly.
I cannot possibly spend the hours to retype all these names - does anyone have a solution - I have tried everything I know ! Thank you
Hello!
You may have non-printing characters in your text. Try removing them using these recommendations: Delete spaces, line breaks and non-printing characters. We also have a tool that can remove extra spaces, line breaks, or non-printing characters in one click: Remove Characters tool.
It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Hi,
I am trying to extract the Last Name - "Twain" from this data:
Shania Twain\ShanTw00
Is there an easy way to do this with a formula?
Hi!
Please read the above article carefully. Try to use information in this article: How to extract text before a specific character and How to extract text after character.
=RIGHT(LEFT(A2, SEARCH("\",A2)-1), LEN(LEFT(A2, SEARCH("\",A2)-1))-SEARCH(" ", LEFT(A2, SEARCH("\",A2)-1)))
Hi! I have the following data set and I need to extract weather the model is "RHS" or "LHS" using only =SEARCH
RHS FRONT SEAT ASY 2WAY COMP
FRONT SEAT ASY COMP RHS 2WAY
FRONT SEAT ASY COMP LHS 4WAY
RHS FRONT SEAT ASY COMP 6WAY
Many thanks!
Hi!
I don't quite understand what you want to extract. Give me an example of the result you want to get. But in any case, you need to use other Excel functions besides the SEARCH function. Please read the above article carefully.
23.13 1/17/2023 P64634 01 DOE, JOHN
123.14 1/27/2023 6463401 DOE, JASON ALLEN
3.14- 12/15/2023 F 6463401 DOE, JASON ALLEN
Could use some help if anyone can. Needing a macro or formula that will put this one cell of data usually in Column A into five different columns. Each line of data is a dollar (sometimes a negative after the value), date, account number (sometimes the is a letter that is separated in front or there is a digit or two separated at the end), and then a name. Always Last, First (sometimes the middle name or initial is there). Text to column won't work since there are not always the same width in between the different data types and sometimes the account number has the space in the beginning and/or the end. Any help would be appreciated. Thanks! :)
Hi!
Your text strings do not have a single pattern or a single unique delimiter with which to separate text by columns. I'm really sorry, we cannot help you with this.
Hi,
I wondering if you could help me out with this.
I have a column with alphabets "A","B","U" and blanks . "A" means Main part ; "B" means Sub Part; "U" means Miscellaneous.
I want the descriptions of the alphabets in the next column. Is there a way?
Many thanks!
Hi!
Put the descriptions in a separate table and search there. You can also find useful information in this article: Excel VLOOKUP function tutorial with formula examples.
Thanks very much!
Hi ! Thank you for all the information.
My case is a bit tricky.
I want to extract the address of a string of many different characters.
Eg - From:
“ [ { code: “ABC” } ; { value : “123” } ; { name : “ABC” } ; { code: “XYZ” } ; { value : “&$)
” } ; { Address : “5 Conrad Street 2710” } ; { code dit : “JDI<-” } ; { value : “123” } ; { name : “$;&;7:?” } ;] “
I want to only have the address - 5 Conrad Street 2710.
Note that the number of characters of the address will vary. But the characters right before and after will be the same
Before : Address : “
After : ” } ; { code dit :
Is there a way to do that ? Thank you !
Hello!
Use the advice from the article above. Extract text after certain characters. Then, in the resulting text string, extract the text before certain characters.
Try this formula:
=LEFT(RIGHT(A2,LEN(A2)-SEARCH("Address : ",A2)-10), SEARCH("} ; { code dit :",RIGHT(A2,LEN(A2)-SEARCH("Address : ",A2)-10))-3)
I have a cell A2 that has ABC COMPANY #332; DEF COMPANY #254. I need to extract the 4 character numbers #332 and #254. I would like multiple columns to pull in each instance. I can successfully get the 1st instance using the formula
=TRIM(LEFT(SUBSTITUTE(MID(A2,FIND("#",A2),LEN(A2))," ",REPT(" ",4)),4)) will result in #332
How can i pull in the 2nd instance #254 in another column?
Hello!
If I understand your task correctly, the following formula should work for you:
=MID(A2,SEARCH("$", SUBSTITUTE(A2,"#","$",2)),4)
Thanks, this is working. is there a way to add a qualifier to only return values that have a # followed by numbers and NOT include any instances where a # is followed by letters?
Example
#332; #FGH; #254
Return #254 instead of #FGH
Hello!
To extract multiple text strings from the text, use regular expressions. Read more about it in the article How to extract substrings in Excel using regular expressions (Regex). Use this formula -
=RegExpExtract(A1,"#\d+")
You can use regular expressions with Regex Tools. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Please help on how to extract or split if the start of the characters are the same?
Example I want to extract only the words after the second "20L" which is ERASE on below.
9D21461T5580739.11PHMC1020LP4ETR20LERASE
Hi!
If I got you right, the formula below will help you with your task:
=MID(SUBSTITUTE(A2,"20L","#",2), SEARCH("#",SUBSTITUTE(A2,"20L","#",2))+1,50)
What about if the two characters are different? Which character do you put where?
For example I am looking to to pull out just "240" from the string "200- COGS : 240 - Prof Services Mgmt" in a different cell. In this example, I'm looking for the section of the string between the "-" character and ":" character.
I'm using the formula =MID(F2, SEARCH(":",F2) + 1, SEARCH("-",F2,SEARCH("-",F2)+1) - SEARCH(":",F2) - 1), but I think I need to change around the "-" and ":", but not sure where. I am always needing to break out these 4 pieces of information from a string like this and it takes forever. So if someone could tell me the best formula to use to get each of the pieces of information (200, COGS, 240, Prof Services Mgmt), that would be really helpful!
Thanks!
Hi!
You can extract from text with delimiters 4 values into 4 cells with a single formula using new function TEXTSPLIT
=TEXTSPLIT(F2,{":","-"})
If this function is not available to you, I recommend using these instructions: How to split cells in Excel: Text to Columns, Flash Fill and formulas.
Hi,
Can you please help in excel?
I want to extract the date from a cell and is it possible the word "Date" would be appearing every time just before the date.
I extracted the last 8 characters that contains date (Example of that text --> JSB-EPP-005253-23052022) but the requirement of the format is not achieved. Last 8 characters should be displayed in the assigned cell as "Date: 23.05.2022)".
If this is possible, so please guide me how it would be implemented.
Appreciate your great work,
Thanks & Warm Regards,
Ali
Hi!
To insert characters into text, use the REPLACE function
Try this formula:
="Date: "&REPLACE(REPLACE(RIGHT(A1,8),3,0,"."),6,0,".")
Thank you very very much...this Formula worked great and fulfilled my all requirement :)
I once again appreciate and bundle of thanks Dear Sir..
if I have sentence in one cell, for example :
1. I want to got to "Market"
2. Yesterday, "Old lady" passed away
3. "Car" is expensive
How do you take only the word in between " " sign and copy to other cell (in this case word : Market, Old lady, Car)
Thank you
Hi!
Pay attention to the following paragraph of the article above – How to extract text between two instances of a character. It covers your case completely.
I'm needing to use this for a set of data that sometimes has multiple words in a cell, but not always. I've tried nesting it in an IF formula, but I've gone wrong somewhere. What I have works if there are spaces, but not if there aren't.
Sample data set:
Cat
Brown dog
Mixed-breed dog
I would like to get the following results:
Cat
Brown
Mixed-breed
This formula (where A16 is the original cell) works for the second two, but not the first.
=IF(SEARCH(" ",A16),LEFT(A16,SEARCH(" ",A16)-1),A16)
Hi!
Use the IFERROR function to handle an error when a space is not found.
=IFERROR(IF(SEARCH(" ",A16),LEFT(A16,SEARCH(" ",A16)-1),A16),A16)
That's worked! Thank you!
Hi...!!
I want to check a cell by formula "if a cell contains FORMULA in itself or not".
I am using Excel 2010 so "ISFORMULA" function is not workable for me.
Please advise.
Please advise
Hi
I hope you can help me with this kind sir
In column one, I have a list of tracking numbers that looks like this:
123456
123456
123456
123456
12345678
12345678
12345678
1234567
1234567
12345
12345
12345
Suppose all FedEx trackings are 7 digit numbers, USPS are 8 digits length, DHL are 6 and XPO are the ones with 5 digits.
What I'm trying to do is finding all USPS trackings and extract em from colum 1 to column 2, then, find all FedEx trackings and extract em to column 3, then find all the DHL tracking numbers to column 4 and so on.
Is there a formula for that? Like, one that find all values of a given amount of digits (or characters) in a column and list em in a different column?
Thanks in advance!
(Sorry for the spam, I wasn't sure if I have replied to someone's comment lol)
Hello!
Determine the number of characters using the LEN function. Use the FILTER function to get values of a specific length.
For example:
=FILTER(A1:A20,LEN(A1:A20)=6)
I am looking to extract the text after MA_
RURAL_BUILD_FTTP_28B_FTTP_MA_PCPV9135_ON69848_AGN_SPN_CBT_RURAL_VA1
RURAL_BUILD_28B_FTTP_MA_PCPV9152_POLING_CIVILS_VA1
RURAL BUILD FTTP 30A_FTTP_MA_PCPV9058_MICS_CIVILS_VA1
RURAL BUILD_29A_FTTP_MAIDSTONE_MA_PCPV9027_TEST ROD AND ROPE
Ideally I would like a return of only the V*** after PCP
Any help would be greatly appreciated
Hello!
You can find the examples and detailed instructions here: Extract substring before or after a given character. This should solve your task.
Please How do i extract group of numbers appearing in in different positions of a different cells.
Example;
A
ABSTV234 K:50s
sg789nvhn092h
satcads15qw20
B(outcome)
234
789
15
Hello!
To extract the first number from text string use the user-defined function RegExpExtract. You can find the examples and detailed instructions here: How to extract substrings in Excel using regular expressions (Regex).
"1970000.
00"
how to remove 1st and last 4th char in excel. i excel sheet it is hot showing same.
in excel is display as 1970000.00
i have tried right, left, char, find & replace, int, roundup & etc.
pls help me it is taking lot of time.
Hi!
I am not sure I fully understand what you mean. Explain what result you want to get. Maybe this article will be helpful: How to delete text before or after a certain character in Excel.
Hi Dear,
Can you please help me to extract the text using excel logic
791541213823202211Towage10120
80292721355020228Charts/Publications10.11
80292721355020228Class Certificates / Survey fee (DNV, Lloyd's, GL) / ISM250
80292721355020228Port Costs1897
80292721355020228Port Costs2116.8
80292721355020228Port Costs4.7
80292721355020228Port Costs66.69
80292721355020228Waste Disposal1639.28
90301491169020229Mooring Unmooring1003.89
903643010118Federal Goods and Services Taxes (GST)1633.41
903643010118Federal Goods and Services Taxes (GST)1834.4
903643010118Federal Goods and Services Taxes (GST)2035.4
903643010118Federal Goods and Services Taxes (GST)218.28
903643010118Federal Goods and Services Taxes (GST)230.02
Hello!
To extract everything except numbers from text, you can use the user-defined RegExpExtract function.
=RegExpExtract(A1, "[^\d]+")
You can also find useful information in this article: How to remove numbers from text string in Excel.
Also you can try the formula:
=SUBSTITUTE((CONCAT(IF(NOT(ISNUMBER(--MID(A1,ROW($1:$94),1))), MID(A1,ROW($1:$94),1),"")))," ","")
I hope it’ll be helpful.
Hi All,
not even sure if this is possible. But i need to return the first 5 digit number from the below alphanumeric text in a cell. the answers should be
Example 1: 93423
Example 2: 87952
Example 1: "**02.06 return updated in SPA** ordered 93423 BR 4PNS PCFC A RK 495L 1X1 x 2 delivered 2 x 4Pns Pl Al 94253 1300353110"
Example 2: noted with d Short delivered multiple invoices - 2 x BR Vct Br NGB 750 4x3 87952 12 x BR PI N A 5.1% NGB 330ML 4X6IMP 94152
Hello!
You can solve your problem with a user-defined REGEX function. The following tutorial should help: How to extract substrings in Excel using regular expressions (Regex). To extract a five digit number, try this formula:
=RegExpExtract(A1, "\d{5}", 1)