Skip to content

MID

Returns a stated number of characters from the middle of text, starting at a position you choose.

Syntax

=MID(, , )

Text

All versions of Excel.

Try it

Edit the formula in C2 or change the inputs. The sheet recalculates as you type.

C2
A
B
C
1
Batch code
Qty
Year
2
PW-24-0113
400
24
3
GL-25-0207
250
4
PW-25-0301
610
5

C2 =MID(A2,4,2) returns 24

How MID works

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.

The arguments

  • text is what to read, normally a cell reference.
  • start_num is where to start. Excel counts the first character as position 1, not 0. =MID("PW-24-0113",4,2) starts at the fourth character and returns "24".
  • num_chars is how many characters to take. It is required, which is the difference from LEFT and RIGHT.

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)).

Fixed positions

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.

Variable positions

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

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.

Counting from the end

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.

Arguments

text
The text to take characters from. Usually a cell such as A2.
start_num
The position of the first character to return. The first character of the text is position 1. A value below 1 returns #VALUE!, and a value beyond the end of the text returns an empty string.
num_chars
How many characters to return. Unlike LEFT and RIGHT, this argument is required. If it runs past the end of the text, MID returns everything up to the end. A negative value returns #VALUE!.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

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
A
B
C
1
Batch code
Qty
Year
2
PW-24-0113
400
24
3
GL-25-0207
250
4
PW-25-0301
610
5

C2 =MID(A2,4,2) returns 24

Example 2

Turn the segment into a number

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
A
B
C
1
Batch code
Qty
Full year
2
PW-24-0113
400
2024
3
4

C2 =2000+VALUE(MID(A2,4,2)) returns 2024

Example 3

Text between two markers

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
A
B
C
1
Item
Qty
Warehouse
2
Pallet wrap [WH-2]
38
3
Gloves [WH-11]
120
WH-11
4

C3 =MID(A3,FIND("[",A3)+1,FIND("]",A3)-FIND("[",A3)-1) returns WH-11

Example 4

Take the rest of the text

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
A
B
C
1
Ref
Owner
Number part
2
INV-0457
Finance
0457
3
4

C2 =MID(A2,5,100) returns 0457

Your turn.

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.

B2
A
B
C
D
1
Product
Colour code
Bin
Qty
2
FX-1010-BLK [A12]
38
3
FX-1085-RED [B3]
120
4
FX-2040-GRN [C104]
96
5
6
7

Common mistakes

  • Positions start at 1, not 0. =MID(A2,1,2) is the same as =LEFT(A2,2). A start_num of 0 returns #VALUE!.
  • Separators count as characters. Counting PW-24-0113 by eye and forgetting the dashes is the usual reason a fixed MID returns the wrong slice.
  • num_chars is required. =MID(A2,4) is rejected, unlike LEFT and RIGHT. To take the rest of the text, use a generous number or LEN(A2).
  • MID returns text, even for digits. =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.
  • When the marker positions vary, a fixed start_num breaks on the next row. Use FIND to locate the markers.

Questions people ask

How do I extract text from the middle of a cell?

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.

How do I get text between two characters in Excel?

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.

Why does MID return #VALUE!?

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.

Can MID take the rest of the text from a position?

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.

Is MID the same as SUBSTRING in other languages?

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.

Learn MID in a real sheet.

MID is taught in Logic and text, lesson 11: MID and FIND. Each lesson is a short, graded exercise like the one above.