Excel MID function to extract text from the middle of a string

MID is one of the Text functions that Microsoft Excel provides for manipulating text strings. At the most basic level, it is used to extract a substring from the middle of the text string. In this tutorial, we will discuss the syntax and specificities of the Excel MID function, and then you will learn a few creative uses to accomplish challenging tasks.

Excel MID function - syntax and basic uses

Generally speaking, the MID function in Excel is designed to pull a substring from the middle of the original text string. Technically speaking, the MID function returns the specified number of characters starting at the position you specify.

The Excel MID function has the following arguments:

MID(text, start_num, num_chars)

Where:

  • Text is the original text string.
  • Start_num is the position of the first character that you want to extract.
  • Num_chars is the number of characters to extract.

All 3 arguments are required.

For example, to pull 7 characters from the text string in A2, starting with the 8th character, use this formula:

=MID(A2,8, 7)

The result might look something similar to this:
Using the MID function in Excel

5 things you should know about Excel MID function

As you have just seen, there's no rocket science in using the MID function in Excel. And remembering the following simple facts will keep you safe from most common errors.

  1. The MID function always returns a text string, even if the extracted substring contains only digits. This may be critical if you wish to use the result of your Mid formula within other calculations. To convert an output into a number, use MID in combination with the VALUE function as shown in this example.
  2. If start_num is greater than the overall length of the original text, an Excel Mid formula returns an empty string ("").
  1. If start_num is less than 1, a Mid formula returns the #VALUE! error.
  2. If num_chars is less than 0 (negative number), a Mid formula returns the #VALUE! error. If num_chars is equal to 0, it outputs an empty string (blank cell).
  3. If the sum of start_num and num_chars exceeds the total length of the original string, the Excel MID function returns a substring starting from start_num and up to the last character.

Excel MID function - formula examples

When dealing with real-life tasks in Excel, you will most often need to use MID in combination with other functions as demonstrated in the following examples.

How to extract first and last name

If you've had a chance to read our recent tutorials, you already know how to pull the first name using the LEFT function and get the last name with the RIGHT function. But as is often the case in Excel, the same thing can be done in a variety of ways.

MID formula to get the first name

Assuming the full name is in cell A2, first and last names separated with a space character, you can pull the first name using this formula:

=MID(A2,1,SEARCH(" ",A2)-1)

The SEARCH function is used to scan the original string for the space character (" ") and return its position, from which you subtract 1 to avoid trailing spaces. And then, you use the MID function to return a substring beginning with the fist character and up to the character preceding the space, thus fetching the first name.

MID formula to get the last name

To extract the last name from A2, use this formula:

=TRIM(MID(A2,SEARCH(" ",A2),LEN(A2)))

Again, you use the SEARCH function to determine the starting position (a space). There is no need for us to calculate the end position exactly (as you remember, if start_num and num_chars combined is bigger than the total string length, all remaining characters are returned). So, in the num_chars argument, you simply supply the total length of the original string returned by the LEN function. Instead of LEN, you can put a number that represents the longest surname you expect to find, for example 100. Finally, the TRIM function removes extra spaces, and you get the following result:
Excel MID formulas to extract the first and last names

Tip. To extract the first and last word from a string with a simpler formula, you can use the custom ExtractWord function.

How to get substring between 2 delimiters

Taking the previous example further, if besides first and last names cell A2 also contains a middle name, how do you extract it?

Technically, the task boils down to working out the positions of two spaces in the original string, and you can have it done in this way:

  • Like in the previous example, use the SEARCH function to determine the position of the first space (" "), to which you add 1 because you want to start with the character that follows the space. Thus, you get the start_num argument of your Mid formula: SEARCH(" ",A2)+1
  • Next, get the position of the 2nd space character by using nested Search functions that instruct Excel to start searching from the 2nd occurrence of the space character: SEARCH(" ",A2,SEARCH(" ",A2)+1)

    To find out the number of characters to return, subtract the position of the 1st space from the position of the 2nd space, and then subtract 1 from the result since you don't want any extra spaces in the resulting substring. Thus, you have the num_chars argument: SEARCH (" ", A2, SEARCH (" ",A2)+1) - SEARCH (" ",A2)

With all the arguments put together, here comes the Excel Mid formula to extract a substring between 2 space characters:

=MID(A2, SEARCH(" ",A2)+1, SEARCH (" ", A2, SEARCH (" ",A2)+1) - SEARCH (" ",A2)-1)

The following screenshot shows the result:
Mid formula to get substring between 2 spaces

In a similar manner, you can extract a substring between any other delimiters:

MID(string, SEARCH(delimiter, string)+1, SEARCH (delimiter, string, SEARCH (delimiter, string)+1) - SEARCH (delimiter, string)-1)

For example, to pull a substring that is separated by a comma and a space, use this formula:

=MID(A2,SEARCH(", ",A2)+1,SEARCH(", ",A2,SEARCH(", ",A2)+1)-SEARCH(", ",A2)-1)

In the following screenshot, this formula is used to extract the state, and it does the job perfectly:
Mid formula to extract a substring separated by a comma and a space.

How to extract Nth word from a text string

This example demonstrates an inventive use of a complex Mid formula in Excel, which includes 5 different functions:

  • LEN - to get the total string length.
  • REPT - repeat a specific character a given number of times.
  • SUBSTITUTE - replace one character with another.
  • MID - extract a substring.
  • TRIM - remove extra spaces.

The generic formula is as follows:

TRIM(MID(SUBSTITUTE(string," ",REPT(" ",LEN(string))), (N-1)*LEN(string)+1, LEN(string)))

Where:

  • String is the original text string from which you want to extract the desired word.
  • N is the number of word to be extracted.

For instance, to pull the 2nd word from the string in A2, use this formula:

=TRIM(MID(SUBSTITUTE(A2," ",REPT(" ",LEN(A2))), (2-1)*LEN(A2)+1, LEN(A2)))

Or, you can input the number of the word to extract (N) in some cell and reference that cell in your formula, like shown in the screenshot below:
Excel Mid formula to extract Nth word from a text string

How this formula works

In essence, the formula wraps each word in the original string with many spaces, finds the desired "spaces-word-spaces" block, extracts it, and then removes extra spaces. To be more specific, the formula works with the following logic:

  • The SUBSTITUTE and REPT functions replace each space in the string with multiple spaces. The number of additional spaces is equal to the total length of the original string returned by LEN: SUBSTITUTE(A2," ",REPT(" ",LEN(A2)))

    You can think of an intermediate result as of "asteroids" of words drifting in space, like this: spaces-word1-spaces-word2-spaces-word3-… This "spacious" string is supplied to the text argument of our Mid formula.

  • Next, you work out the starting position of the substring of interest (start_num argument) using the following equation: (N-1)*LEN(A1)+1. This calculation returns either the position of the first character of the desired word or, more often, the position of some space character in the preceding space separation.
  • The number of characters to extract (num_chars argument) is the easiest part - you simply take the overall length of the original string: LEN(A2). At this point, you are left with spaces-desired word-spaces substring.
  • Finally, the TRIM function gets rid of leading and trailing spaces.

The above formula works fine in most situations. However, if there happen to be 2 or more consecutive spaces between words, it yields wrong results. To fix this, nest another TRIM function inside SUBSTITUTE to remove excess in-between spaces except for a single space character between words, like this:

=TRIM(MID(SUBSTITUTE(TRIM(A2)," ",REPT(" ",LEN(A2))), (B2-1)*LEN(A2)+1, LEN(A2)))

The following screenshot demonstrates the improved formula in action:
An improved Mid formula to extract Nth word from text string

If your source strings contain multiple spaces between words as well as very big and very small words, additionally embed a TRIM function into each LEN, just to keep you on the safe side:

=TRIM(MID(SUBSTITUTE(TRIM(A2)," ",REPT(" ",LEN(TRIM(A2)))), (B2-1)*LEN(TRIM(A2))+1, LEN(TRIM(A2))))

I agree that this formula looks a bit cumbersome, but it impeccably handles all kinds of strings.

Tip. See how to extract any Nth word from text using a more compact and straightforward formula.

How to extract a word containing a specific character(s)

This example shows another non-trivial Excel Mid formula that pulls a word containing a specific character(s) from anywhere in the original text string:

TRIM(MID(SUBSTITUTE(string," ",REPT(" ",99)),MAX(1,FIND(char,SUBSTITUTE(string," ",REPT(" ",99)))-50),99))

Assuming the original text is in cell A2, and you are looking to get a substring containing the "$" character (the price), the formula takes the following shape:

=TRIM(MID(SUBSTITUTE(A2," ",REPT(" ",99)),MAX(1,FIND("$",SUBSTITUTE(A2," ",REPT(" ",99)))-50),99))
Mid formula to extract a word containing a specific character

In a similar fashion, you can extract email addresses (based on the "@" char), web-site names (based on "www"), and so on.

How this formula works

Like in the previous example, the SUBSTITUTE and REPT functions turn every single space in the original text string into multiple spaces, more precisely, 99 spaces.

The FIND function locates the position of the desired character ($ in this example), from which you subtract 50. This takes you 50 characters back and puts somewhere in the middle of the 99-spaces block that precedes the substring containing the specified character.

The MAX function is used to handle the situation when the desired substring appears in the beginning of the original text string. In this case, the result of FIND()-50 will be a negative number, and MAX(1, FIND()-50) replaces it with 1.

From that starting point, the MID function collects the next 99 characters and returns the substring of interest surrounded by lots of spaces, like this: spaces-substring-spaces. As usual, the TRIM function helps you eliminate extra spaces.

Tip. If the substring to be extracted is very big, replace 99 and 50 with bigger numbers, say 1000 and 500.

How to force an Excel Mid formula to return a number

Like other Text functions, Excel MID always returns a text string, even if it contains only digits and looks much like a number. To turn the output into a number, simply "warp" your Mid formula into the VALUE function that converts a text value representing a number to a number.

For example, to extract a 3-char substring beginning with the 7th character and convert it to a number, use this formula:

=VALUE(MID(A2,7,3))

The screenshot below demonstrates the result. Please notice the right-aligned numbers pulled into column B, as opposed to the original left-aligning text strings in column A:
Use the Excel MID function together with VALUE to return a number

The same approach works for more complex formulas as well. In the above example, assuming the error codes are of a variable length, you can extract them using the Mid formula that gets a substring between 2 delimiters, nested within the VALUE function:

=VALUE(MID(A2,SEARCH(":",A2)+1,SEARCH(":",A2,SEARCH(":",A2)+1)-SEARCH(":",A2)-1))
Nest a Mid formula in the VALUE function to turn the output into a number.

This is how you use the MID function in Excel. To better understand the formulas discussed in this tutorial, you are welcome to download a sample workbook below. I thank you for reading and hope to see you on our blog next week!

Download practice workbook

Excel MID function - formula examples (.xlsx file)

More examples of using the MID function in Excel:

206 comments

  1. I am hopeing someone can help me with this

    In 1 Cell, I have this can change but the format will be the same
    12345 - Brazil (4 Gen11 DC)
    12346 - Spain (4 Gen11 AC)
    12347 - USA (6 Gen11 DC)
    12348 - Chile( 6 Gen11 DC)
    12349 - Maxico (6 Gen11 AC)

    I need to count the number before Gen11 but only count if it is AC or DC

    Exp. output I am looking for

    Cell B3 has
    Gen11 (DC)
    Cell B4 has
    Gen11 (AC)

    C3 = 16
    C4 = 10

    Thank you in advance

    1. Hello Luis!
      You can do math operations on numbers if you extract them from text. To extract number that is written between “(” and a space, use these instructions: Extract substring before or after a given character. To convert text to a number, use VALUE function.For example:

      =VALUE(MID(A1,SEARCH("(",A1)+1, SEARCH(" ",A1,SEARCH("(",A1))-SEARCH("(",A1)))

      Then use these instructions: Excel: If cell contains then count, sum, highlight, copy or delete.

      1. Hello Alexander,

        Thank you for your reply

        That is the code I was using, but my problem is that all the information is in 1 cell, not multiple cells

        12345 - Brazil (4 Gen11 DC)
        12346 - Spain (4 Gen11 AC)
        12347 - USA (6 Gen11 DC)
        12348 - Chile( 6 Gen11 DC)
        12349 - Maxico (6 Gen11 AC)

        Thank you for your reply

        1. Hi! The formula I sent to you was created based on the description you provided in your first request. To use it, first split this text into separate cells. You can do this by using TEXTSPLIT formula.

          =TEXTSPLIT(A1,,CHAR(10))

          Then use the formula I recommended.
          If this function is not available to you, use any of methods suggested in this article: How to split text string in Excel by comma, space, character or mask.
          You can solve the problem without using formulas. Pay attention to the Split Text tool. If text is separated by commas, spaces, dashes, or other characters, you can use this tool to create multiple columns or rows from a single cell. The tool is included in the Ultimate Suite for Excel and can be used in a free trial to see how it works.

  2. AB: QUICK JUMPS: ab12 - happy: cd09 - sadness

    I want to get the code "cd09" - can you please assist? There were lots of words actually but the last code the alphabet and number at the last. please..

    1. Hi! To extract the sixth word from the text, you can use the TEXTBEFORE and TEXTAFTER functions.

      =TEXTBEFORE(TEXTAFTER(A1," ",6)," ")

      If these functions are not available to you, use the recommendations in this article: How to extract word from string in Excel: first, last, Nth, and more.
      You can also replace the sixth space with a non-standard character using the SUBSTITUTE function. Then use the SEARCH function to find where the word starts and ends. Extract the sixth word using the MID and LEFT function.
      The formula might look like this:

      =LEFT(MID(SUBSTITUTE(A1," ","#",6), SEARCH("#",SUBSTITUTE(A1," ","#",6))+1,20), SEARCH(" ",MID(SUBSTITUTE(A1," ","#",6), SEARCH("#",SUBSTITUTE(A1," ","#",6))+1,20)))

  3. I want and ID from a text.
    example :
    Line 1 : your order confirmation is USA1234 from Machine A
    Line 2 : your order confirmation is TOK4456 from Machine B
    For line 1 : return result USA1234, Line 2 return result TOK4456
    basically I needed multiple nested =MID(G56,SEARCH("USA",G56),7), MID(G56,SEARCH("TOK",G56),7)

    THANK YOU !!!

  4. How can I extract the name/s in the middle?
    CRUZ,JOHN MARK GARCIA
    CRUZ,JOHN GARCIA
    Is it possible that the formula will use the limit on the left (comma) and right (space)?
    What formula is applicable to both example above?
    Big thanks! <3

    1. Hello Jeph!
      You can find the examples and detailed instructions here: How to delete text before or after a certain character in Excel. First, remove all text after the first space, and then remove all characters before the comma from this text string.

      =RIGHT(LEFT(A1, SEARCH(" ", A1) -1), LEN(LEFT(A1, SEARCH(" ", A1) -1)) - SEARCH(",", LEFT(A1, SEARCH(" ", A1) -1)))

      You can also execute these operations using the TEXTBEFORE and TEXTAFTER functions. For example:

      =TEXTAFTER(TEXTBEFORE(A1," "),",")

  5. Hi, I would like to extract a text that starts with SRM, the issue that I am having is when the SRM has some letters without space on it.

    For example:

    conditions to govern jsndjsdSRM_011288 sjjdjsjd

    I need to extract from SRM everything else after that

    I was using =TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),MAX(1,FIND("SRM",SUBSTITUTE(A1," ",REPT(" ",99)))-50),99))

    but the result is jsndjsdSRM_011288

    What formula can I use to get only SRM and the info after that?

    Thanks!

  6. Hi,
    Kindly provide me formula to extract "257681100152" from data 8102979835#2080344###681.00#1 UNIT#03/2024#257681100152#ASSY. ORVM RH BS1/2/3/4/6

  7. I have the following formula and I would like to colon at the end of the results. How would I go about doing it?

    =MID(A2,FIND(",",A2)+1,LEN(A2))

  8. Hi Sir,

    It is possible excel can read the YEAR in different format?

    Example (Same report number but different year format)

    Report 1 - LONDON/123/2024
    Report 2 - LONDON/123/24

    1. Hi! I can assume that you want to extract the year from the text. In your examples, the year is the last word, which is separated by the "/" separator. If I understand your task correctly, the following tutorial should help: How to extract word from string in Excel: first, last, Nth, and more. Based on the information given, the formula could be as follows:

      =TRIM(RIGHT(SUBSTITUTE(A1, "/", REPT(" ", LEN(A1))), LEN(A1)))

      You can also split text using the TEXTSPLIT function. The CHOOSECOLS function can get the third word:

      =CHOOSECOLS(TEXTSPLIT(A1,"/"),3)

      You can solve the problem without using formulas. Pay attention to the Split Text tool. If text is separated by commas, spaces, dashes, or other characters, you can use this tool to create multiple columns or rows from a single cell. The tool is included in the Ultimate Suite for Excel and can be used in a free trial to see how it works.

      1. Tq Sir for your reply. I want to excel detect the duplication of the report number between 2 reports but different year format. (2024/24)

        1. Hi! I recommend paying attention to the tool Fuzzy Duplicate Finder. The add-in looks for partial duplicates and typos in Excel files that differ by 1 to 10 characters, and detects characters that are missing, have extra characters, or are misspelled. This is done without using formulas.
          The tool is included in the Ultimate Suite for Excel and can be used in a free trial to see how it works.

  9. Hi Sir,

    I want to extract ID number only from multiple row. Use this fx =MID(Sheet1!F5,6,8) but failed for the 2nd row because the wording not same. Please help

    Insp G0123211 JOHN BIN LEE
    Sarjan R0123211 JOHN BIN LEE

    1. Hello! Find the position of the first space using the SEARCH function. Then extract the next 8 characters from the text. I believe the following formula will help you solve your task:

      =MID(A1,SEARCH(" ",A1)+1,8)

  10. Hi I am trying to convert a deck of cards into a value so that Ace=14, King=13, Queen=12 and so forth. But I only want do type two letters for each card into a cell, to get the outcome into another table. So if I put into the input table "AKQ" for Ace, king and a queen I want to get into the output table the value "39". I need up to 8 cards to be able to put into the formula.

  11. Harmony Nirvana Country Flat No 1701 Unitech Harmony Sector VI

    From the above address I want to extract the value (1701). Which formula will work for it?

  12. Hello, this thread has been very helpful. I have a column where wages are listed among other text. In some cells it is a static amount, in others it is a range. Example: "Regular Full-time, 40 hours/week $24.35 - $27.55 Hourly"
    I have used a LEN formula to figure out how many $ symbols there are in Column F and am using an if formula to return the wage if only one $ is present, but I am having trouble returning only the range. I do not want "Hourly". The ideal result would be "$24.35 - $27.55".
    Here is the formula I am using, but it only returns "$24.35".
    =IF(F14=1,TEXTBEFORE(TEXTAFTER(E14,"$")," "),TRIM(MID(SUBSTITUTE(E14," ",REPT(" ",99)),MAX(1,FIND("$",SUBSTITUTE(E14," ",REPT(" ",99)))-50),99)))

    1. Use substring functions to extract the desired result from the text. Try this formula:

      =MID(E14, SEARCH("$",E14), (SEARCH(" ",E14,SEARCH("#",SUBSTITUTE(E14,"$","#",2))))-SEARCH("$",E14))

      Hope this is what you need.

  13. Hi,

    I need to find the exact position of the cell A1= DBR from:
    cell A2 = DW=4000 DH=2100 TT=1 TTT=68 M=2 MDBR=0 EH=1 EC=1 DTR=0 DBR=50

    When I use =FIND(A1,A2) it will return me position of DBR from MDBR which is the first parameter containg DBR in the name, but after that we have DBR in the end which I am interested in.

    Which formula will be the best to extract exact name from A1.

    Thanks

  14. sir i have doubt thats sa@07gar07 into sagar@0707 into dynamic whats the formula.

    1. Hi! Extract separate text strings using the MID function and combine them using the & operator. Please read the above article carefully.

      =MID(A1,1,2)&MID(A1,6,3)&MID(A1,3,3)&MID(A1,9,2)

  15. Hello, and thank you for your dedication, I have a cell with about 20 lines of data, from there I only want to extract 4 digits after the semicolon on the 10th line and nothing after. Data can look like; I get it to work with =TEXTAFTER(D4,Area Code: #") but i cannot get it to stop after those three digits and pull everything after..

    City: Miami
    State: FL
    Town: anything
    Area Code: # 305
    Province:
    Country: US
    Work Location: Local

    1. Hi! Your question does not match the formula and data you wrote below. If you want to extract the first 4 characters from the text, use the LEFT function. For example,

      =LEFT(TEXTAFTER(D4,"Area Code: #"),4)

      1. Thank you! that worked perfectly! i guess i was rushing trying to explain.

  16. Hello, I have a string like this

    SEP 28 SQUARESPACE INC. HTTPSSQUARES $46.61

    And I need to remove the date on the left and the price of transaction on the right. Is that something I can do in one formula?

  17. I have this email script in my first colum A1:

    "We are glad to hear from you again.

    Please ensure that you account is being activated before the due date.

    Here is your user ID abcdefg123@popit.com"

    My question:
    Different customers would have different user ID
    How do I trim the login ID (abcdefg123@popit.com) to be appeared in another column : B1.

    Thanks :)

  18. Please support getting the text "RET-EXPIRED", "Expired", and "DAMAGES" only from the below text in excel.

    P-02-RET-EXPIRED
    P-07-Expired-2023
    P-07-DAMAGES-2023

  19. Hi Sir,

    FLAT/ROOM 2504, FLOOR 25, TONG BO HOUSE, FU TONG ESTATE, TONG CHONG
    FLAT/RM 18 5/F TSU YONG HSE TSU PONG NORTH ESTATE
    FLAT/RM 19 13/F MAI SHAK HOUSE SHAK MEN ESTATE
    FLAT/ROOM 2103, FLOOR 21, LENG CUN HOUSE, LENG KING ESTATE
    FLAT 05 15/F IN CHING HOUSE UN CHUNG ESTATE
    FLAT/RM 3106, 31/F, MAN KING HOUSE, TSZ MING ESTATE
    ROOM 2428, MIND SHIN HSE, MING SHING ESTATE
    FLAT 1019, FLOOR 10, MEN KONG HOUSE, MODEL ESTATE
    FLAT/ROOM 1106, 11/F YAN YUE HOUSE, TENG HANG ESTATE

    I want to find the starting position of XX HOUSE by looking two " " to the left of "HOUSE" in order to extract "X X HOUSE" from the string

      1. Thank you for you reply,
        However, I cannot find textsplit in my Excel 2016.

  20. I have a very descriptive Cell (2000+ in the column). I want to extract from each cell User: John Smith and user Certifcate Serial Number in two separate columns. How do I achieve this:

    The Key Distribution Center (KDC) encountered a user certificate that was valid but could not be mapped to a user in a secure way (such as via explicit mapping, key trust mapping, or a SID). The certificate also predated the user it mapped to, so it was rejected. See http s://go.microsoft.com/fwlink/?linkid=2189925 to learn more.

    User: John Smith
    Certificate Subject: @@@OID.0.9.2242.17200300.102.1.1=12008002283899 CN=JOHN SMITH (Affiliate), OU=Support Office, O=Microsoft, C=US
    Certificate Issuer: Digicert CA
    Certificate Serial Number: 6109AE1K
    Certificate Thumbprint: 75024E979EA9006B1770
    Certificate Issuance Time: 13113180000000
    Account Creation Time: 1332200000000

Post a comment



Thanks for your comment! Please note that all comments are pre-moderated, and off-topic ones may be deleted.
For faster help, please keep your question clear and concise. While we can't guarantee a reply to every question, we'll do our best to respond :)