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.
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:
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:
The result might look something similar to this:
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.
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.
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.
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:
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.
To extract the last name from A2, use this formula:
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:
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:
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:
In a similar manner, you can extract a substring between any other delimiters:
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:
This example demonstrates an inventive use of a complex Mid formula in Excel, which includes 5 different functions:
The generic formula is as follows:
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:
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:
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.
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:
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.
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:
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))
In a similar fashion, you can extract email addresses (based on the "@" char), web-site names (based on "www"), and so on.
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.
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:
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:
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:
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!
Excel MID function - formula examples (.xlsx file)
Table of contents