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)
437 comments
Hi,
I want to extract the just the website address and not the rest of the links. Can you help me with the formula?
Hello!
To extract the website address, try formula:
=LEFT(A1,SEARCH("#",SUBSTITUTE(A1,"/","#",3)))
Hello,
Are you able to extract the last bit for me, i.e. '1453510'
/CODE/ZZ/PARIS-LONDON/89853323/1453510
Many Thanks,
=RIGHT($B2,LEN($B2)-SEARCH("/",$B2,LEN($B2)-8))
Hello!
Here is the article that may be helpful to you: How to extract number from the end of text string.
I hope it’ll be helpful.
Hi,
If I want to extract just the name, what formula to use?
Farms Limited #33-1A
thanks,
Hi!
Try the following formula:
=LEFT(A1,SEARCH("#",A1)-1)
You didn't explain, but I'm guessing the name will be retrieved.
Hello Ablebits Team,
I have some data that I need to extract but can't seem to find the answer above, this is one of the cells that I need to extract a Product ID from (all cells will have varying numbers of characters) -
"adult=No,age_group=Adult,c2c_barcode=4062451243197,c2c_colour_swatch=16402,c2c_excerpt=The classic design of the Puma Road Map Golf Polo Shirt is simple enough to pair up with any golf trousers or shorts yet stylish enough to get you through your round with confidence. ,c2c_features=
dryCELL moisture-wicking technology
UPF 40 UV protection
Stretch fabric
Puma branding
80% Polyester / 15% Cotton / 5% Elastane
,c2c_mpn=595788-06,c2c_noaddtocart=0,c2c_rrp=40.00,call_for_price=No,color=Ibiza Blue/Dark Denim (PU595788-06),cross_over_id=PU59578817,default_price_of_set=0.000000,description_below_images=0,ping_popup=No,pl_size_chart=Puma Men's Clothing,price_per_unit=0,product_tag=Sale,size=S,size_system=UK,size_type=Regular"
Basically, all of the cells will include the cross_over_id= but what I need to extract is the actual cross_over_ID - in this instance it is PU59578817, is there a formula for this, please?
Hello!
The formula below will do the trick for you:
=MID(A1,SEARCH("cross_over_id=",A1)+14, SEARCH(",",A1,SEARCH("cross_over_id=",A1)+14) - (SEARCH("cross_over_id=",A1)+14))
Alexander, you are an absolute legend! Thank you.
I have a string
Maintenance Charlie 1,2590 @ 2,000 service period 4/1/2021 to 3/31/2022
I would like to extract from date in one column and end date in another column, help me how can I do that
Hello!
Divide your text into individual cells using the Extract text tool as described in the article above. You can also use the Text to Columns tool or the Split Text tool.
Hello,
I have a document coding which will be in the same format which has 3 Hyphen and 2 space and 1 dot (occasionally)
Eg for max lenght of textstring is: AAA-(BBB)-C99-DD 5555.001 (XX-YY)
Eg for min lenght of textstring is: AAA-(BB)-C8-DD 5555 (XX)
I need to print each section in the coding to a different cell. For example;
A1: AAA
A2: (BBB)
A3: C99
A4: DD
A5: 5555
A6: 001
A7: (XX-YY)
I couldn't sort it out the formula for "DD" section after last hyphen and before space and the remaining text. Thanks in advance.
Hello!
Your string is too long to be split using a formula. It also has no common pattern. Do I recommend using the Split Text tool, as described in this guide. Separate by "-" first, then by other separators. Use the Transpose tool to write the result to a column.
You can try them in trial mode.
column of data with either numbers of varying length or numbers of varying length followed by a letter. I need to split this column into 2 columns. If the cell is a number then the 1st column should simply return the number and the 2nd column will remain empty. But if the cell contains any text the 1st column will return the numbers from the cell and the second column will return any letters. Thanks so much for any help.
For instance
Col A Col B Col C
671 671
894256 894256
89G 89 G
9451w 9451 w
4589 4589
12tw 12 tw
Hello!
Use the formula from the article — How to get number from any position in a string.
To extract all letters except numbers from a cell, use the formula:
=SUBSTITUTE((CONCAT(IF(NOT(ISNUMBER(--MID(A3,ROW(INDIRECT("1:"&LEN(A3))),1))),MID(A3,ROW(INDIRECT("1:"&LEN(A3))),1),"")))," ","")
I hope my advice will help you solve your task.
1-356039-1-1-11771232_82253500
Hello,
I am trying to extract the cell right of 1-356039-1-1-, but I can't seem to get it work.
Any tips?
Hello!
If I got you right, the formula below will help you with your task:
=MID(A1,LEN("1-356039-1-1-")+1,50)
Hi ,
When I generate a report daily one coloum consist of delimited text where I need to search some keyword like (count :1) I need to get the assigned value of count .. can some one help please
Hi!
Please provide me with an example of the source data and the expected result.
Hi ,
A sample sentence has been provided below , content will be be different need to get the value of count
Eg :
This has been updated count :02 , please check
Excpeted output :02
Hello!
If I understand correctly, you need to extract the number from the text. I think this comment has an answer to your question.
I am having difficulty extracting the date listed in several items. I was only able to successfully extract the date (example 2021_02_23) when there was only one underscore proceeding it using =IF(LEN(A4)-LEN(SUBSTITUTE(A4,"_",""))=4,LEFT(RIGHT(A4,LEN(A4)-INT(FIND("_",A4)-1)-1),INT(FIND("_",RIGHT(A4,LEN(A4)-INT(FIND("_",A4))))+5)),"not in correct format")
Any feedback would be greatly appreciated.
BIO_2021_02_23_Apple
BRIEF_2021_09_28_Cat
MR_TY_2020_03_20_Apple
TAX_REC_2021_06_29_Bolton
Hello!
I recommend splitting a cell using any of the methods described in this article. Delimiter - "_". Then use the DATA function.
If you want to extract date as text use this formula:
=SUBSTITUTE(TRIM(CONCAT(IF(ISNUMBER(--MID(A1,ROW($1:$94),1)),MID(A1,ROW($1:$94),1)," ")))," ","_")
You can replace the "_" character with any other character in the formula, for example "."
Hi,
I have following texts in separate rows
TA/21-22/0001
TAS/21-22/0001
WH/TA/21-22/0001
I would like to extract the text before 0001 in each cell with a single formula. Kindly help.
Hello!
The formula below will do the trick for you:
=LEFT(A1,SEARCH("0001",A1,1)-1)
This should solve your task.
below formula in your above example is not working, tried a lot.
it should extract characters between 2nd and 3rd hypen.
output should be 3333
1111-2222-3333-4444
=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)
could you please check why the desire output not comming.
Thanks a lot
Hi!
Carefully copy the formula. Do not use — instead -
Formula works.
Hello,
I've got multiple columns each with data like so "a2b3c1"
eg A1: a2c1
B1: a3
C1: z2a4b5c1
Only one digit will ever follow a letter
What would be a formula to find and then sum the digit after "a" (eg 2+3+4=9)?
I hope this makes sense
Hello!
If I understand your task correctly, the following formula should work for you:
=SUM(--MID(A1:A4,SEARCH("a",A1:A4)+1,1))
Hi -
I am trying to extract certain words from a lookup table. For example:
Cell A1: Q1 Intelliquip invoice
Cell A2: Q2 Intelliquip
Cell A3: Intelliquip - RJE
I am trying to look at cells A1:A3 and find the word "intelliquip", based on a full list elsewhere, and just return the word "Intelliquip". I've found that i can do this for A1: =IFERROR(RIGHT($A1,LEN($A1)-SEARCH($CA$2:$CA$188,$A1)+1),0), however, this would include everything after the world Intelliquip, when i just want that word.
Thoughts on how to return just the specific word i'd like, when there is no structure in the cell i'm trying to find?
Thanks!
Logan
Hello!
I’m sorry but your description doesn’t give me a complete understanding of your task. Your formula is not working. Correct me if I’m wrong, but I believe the formula below will help:
=IF(ISNUMBER(SEARCH("Intelliquip",A1)),"Intelliquip","")
Yes, that formula would be ok. However, I'm hoping for it to be multiple lookups other than just Intelliquip (other vendors, etc.). So that is why I was hoping to do a search(look up to a list of words I want). That way, I can just drag the formula down and it will output the one-word Vendor for me. So it would capture Intelliquip below, as well as Water Systems Council (just two examples) since that lookup table will have those words in there.
Cell A1: Q1 Intelliquip invoice
Cell A2: Q2 Intelliquip
Cell A3: Intelliquip - RJE
Cell A4: Water Systems Council invoice
Cell A5: Q2 Water Systems Council
Cell A5: Water Systems Council - RJE
Hi!
You can combine these conditions in one formula if you study how to use the nested IF function in Excel to check multiple conditions.
Hope this is what you need.
Hello,
I have a plea at you I have output from database looks like this:
virtual_eth_adapters=2/0/90//0/0/SWITCH//all/none,9/0/90//0/0/SWITCH//all/none
to create a working text for further processing I need to get into this format. Basically, add two backslashes and two " to separate two parameters and add backslash \ and quotation mark " at the beginning and end of text.
\"virtual_eth_adapters=\"\"2/0/90//0/0/SWITCH//all/none\"\",\"\"9/0/90//0/0/SWITCH//all/none\"\"\",
Any idea?
Hi,
I found a solution to do it in three parts:
add characters to the end of string
change characters in the middle of string
and update the starting element.
here are my functions, however, I am not able to combine them into one big nested function
end of string: =(REPLACE(C32;LEN(C32);1;"e\""\""\"""))
middle of string: =(REPLACE(C32;FIND(",";C32);1;"\""\"",\""\"""))
beginning of string: =(REPLACE(C32;1;21;"\""virtual_fc_adapters=\""\"""))
Thank you for any help
I need to retrieve the text with two conditions
MS16-057: Security Update for Windows Server 2012 R2 (KB3156059)
condition 1 : MS16-057
condition 2 : Windows Server 2012
Need print this (KB3156059) separate cell if above condition is statisfied.
Hello!
I hope you have studied the recommendations in the tutorial above. It contains answers to your question.
Use paragraph: "How to find substring in Excel" and "How to extract text after character"
cell 1: UPENDRA C. UPRETI (6710)
cell 2: B.S.ARUNACHALAM (6503)
Dear Sir, i want to extract (only name i.e text) not the extension no.
Hello!
To extract only text without brackets from a cell, use the formula
=SUBSTITUTE(SUBSTITUTE((CONCAT(IF(NOT(ISNUMBER(--MID(A1,ROW($1:$93),1))),MID(A1,ROW($1:$93),1),"")))," ",""),"()","",1)
This should solve your task.
Hello, I have trouble finding a suitable formulas to merge a piece of text from two cells into one cell.
cell A1: RU_2017_4 DHL Automotive, IČO: 05164581
cell B1: s.r.o. (EX-04577)
the result should be: DHL Automotive, s.r.o.
Thank you!
Hello!
The formula below will do the trick for you:
=MID(A1,SEARCH(" ",A1,1)+1,SEARCH(",",A1,1)-SEARCH(" ",A1,1)+1)&LEFT(B1,6)
Hope this is what you need.
I need to exctract the string {SYS:}}{S:{MAN:CFS FILTER}{SIG:210601AMH_STP431259368}} from
{1:F01CFSMAU2SAXXX0908456442}{2:O0811400210531EAEAXXXXGXXX00363006232106010000S}{4:{202:0001}{203:0001}{305:A}{332:000000000000}}{5:{CHK:1853274C0ADA}{SYS:}}{S:{MAN:CFS FILTER}{SIG:210601AMH_STP431259368}}
would some one be able to assist?
Hi!
If I got you right, the formula below will help you with your task:
=MID(A2,SEARCH("{SYS:}",A2,1),200)
Hope this is what you need.
Hi, i have a query that i have a vehicle's data where different models are mentioned in each line of a same company and i want to extract exactly those.
1 UNIT(S) USED CAR(S) 2017 MITSU MINICAB CH/NO:
1 UNIT(S) USED VEHICLE MITSU EK WAGON CHASSIS NO :
these are data in 2 different lines and i want in next column that it should show me either Mini cab or EK Wagon
Hello!
It is possible to extract the name of the car from the text by the mask. The name of the car must be enclosed in some characters. Or there must be some pattern in the text. I don't see any of this.