Example 1
Cut a fixed segment from a code
C2 =MID(A2,4,2)
Result: 24
Starts at position 4, the "2" of 24, and takes two characters. The result is text.
C2 =MID(A2,4,2) returns 24
Returns a stated number of characters from the middle of text, starting at a position you choose.
Syntax
=MID(, , )
All versions of Excel.
Try it
Edit the formula in C2 or change the inputs. The sheet recalculates as you type.
C2 =MID(A2,4,2) returns 24
MID returns characters from the middle of text. Use it to cut a segment out of a code, read the year from an ID such as PW-24-0113, or pull out the text between two markers.
=MID("PW-24-0113",4,2) starts at the fourth character and returns "24".If you ask for more characters than remain, MID returns what is left, without padding. If start_num is beyond the end of the text, the result is an empty string. A start_num below 1 or a negative num_chars returns #VALUE!.
Like its relatives, MID returns text, even when the characters are digits. Wrap it in VALUE when you need a number: =VALUE(MID(A2,4,2)).
Use MID directly when the layout never changes. If every product code looks like FX-1010-BLK, with the number in positions 4 to 7, then =MID(A2,4,4) returns 1010. Count positions carefully, because the dashes count too.
When the pieces vary in length, find the markers with FIND or SEARCH and let the formula do the counting. To pull the text between square brackets in Pallet wrap [WH-2]:
=MID(A2,FIND("[",A2)+1,FIND("]",A2)-FIND("[",A2)-1)
The start is one character after the opening bracket. The length is the distance between the brackets, minus one so the closing bracket is left out. This pattern, start after one marker and measure to the next, is the one to remember.
In Excel for Microsoft 365 and Excel 2024, TEXTAFTER and TEXTBEFORE do the same job in a shorter form: =TEXTBEFORE(TEXTAFTER(A2,"["),"]"). TEXTSPLIT can break a whole code into pieces in one go.
MID and REPLACE use the same position idea, one to read and one to overwrite. =REPLACE(A2,4,4,"2000") swaps the four characters that MID would have returned.
MID counts from the start, so to take a segment relative to the end, combine it with LEN. Three characters that sit just before a 4-character suffix start at LEN(A2)-6, so the formula is =MID(A2,LEN(A2)-6,3). Often RIGHT inside LEFT is easier to read than one clever MID.
A2.#VALUE!, and a value beyond the end of the text returns an empty string.#VALUE!.Each example is a live sheet. Change the inputs or the formula and watch the result.
Example 1
C2 =MID(A2,4,2)
Result: 24
Starts at position 4, the "2" of 24, and takes two characters. The result is text.
C2 =MID(A2,4,2) returns 24
Example 2
C2 =2000+VALUE(MID(A2,4,2))
Result: 2024
VALUE converts the text "24" to the number 24, so you can add 2000 to it.
C2 =2000+VALUE(MID(A2,4,2)) returns 2024
Example 3
C3 =MID(A3,FIND("[",A3)+1,FIND("]",A3)-FIND("[",A3)-1)
Result: WH-11
In Gloves [WH-11], the opening bracket is at position 8, so MID starts at 9. The closing bracket is at 14, so it takes 14 - 8 - 1 = 5 characters.
C3 =MID(A3,FIND("[",A3)+1,FIND("]",A3)-FIND("[",A3)-1) returns WH-11
Example 4
C2 =MID(A2,5,100)
Result: 0457
A num_chars larger than the remaining text is fine: MID returns everything from position 5 to the end.
C2 =MID(A2,5,100) returns 0457
Solve it in the sheet. Every answer is checked, with hints if you need them.
Task 1 of 2
In B2, return the three-letter colour code from A2, the characters after the second dash.
Answer in B2.
=MID(A2,1,2) is the same as =LEFT(A2,2). A start_num of 0 returns #VALUE!.PW-24-0113 by eye and forgetting the dashes is the usual reason a fixed MID returns the wrong slice.=MID(A2,4) is rejected, unlike LEFT and RIGHT. To take the rest of the text, use a generous number or LEN(A2).=MID(A2,4,2) gives "24", which won't match the number 24 in a lookup. Wrap it in VALUE when you need a number.Use `=MID(A2,start,count)`. For example, `=MID(A2,4,2)` returns two characters starting at the fourth. When the position varies, find it with FIND.
Use `=MID(A2,FIND("[",A2)+1,FIND("]",A2)-FIND("[",A2)-1)`, replacing the brackets with your markers. In Microsoft 365, `=TEXTBEFORE(TEXTAFTER(A2,"["),"]")` is shorter.
start_num is less than 1 or num_chars is negative. In formulas that use FIND to calculate these numbers, FIND may also be failing because a marker is missing from the text.
Yes. Make num_chars larger than what is left, such as `=MID(A2,5,100)`, or use `LEN(A2)`. MID stops at the end of the text.
Almost. MID takes a start position and a length, and positions start at 1. Many languages count from 0 and may take an end position instead of a length.
MID is taught in Logic and text, lesson 11: MID and FIND. Each lesson is a short, graded exercise like the one above.