LEFT, MID and RIGHT: taking part of a code or a postcode
LEFT takes characters from the start of some text, RIGHT from the end, and MID from the middle: with the code KT-2291-BLU in A2, =LEFT(A2,2) gives KT, =RIGHT(A2,3) gives BLU and =MID(A2,4,4) gives 2291. All three work in every version of Excel, on Windows and Mac and in Excel for the web. When the parts change length, FIND tells LEFT where to stop: =LEFT(A2,FIND(" ",A2)-1) gives everything before the space.
The formulas at a glance
| You want | Formula | Gives |
|---|---|---|
| The first few characters | =LEFT(A2,2) | KT from KT-2291-BLU |
| The last few characters | =RIGHT(A2,3) | BLU |
| Characters from the middle | =MID(A2,4,4) | 2291 (start at the 4th, take 4) |
| Everything before a space | =LEFT(A2,FIND(" ",A2)-1) | LS6 from LS6 2QT |
| Everything after a character | =RIGHT(A2,LEN(A2)-FIND("@",A2)) | acme.co.uk from an email address |
| How many characters | =LEN(A2) | 11 for KT-2291-BLU |
| The part as a number | =VALUE(MID(A2,4,4)) | 2291, which adds up |
| The same, in Excel 365 and 2024 | =TEXTBEFORE(A2," "), =TEXTAFTER(A2,"@") | No counting needed |
Each one is worked through below, with a sheet.
A worked example: product codes
A shop's codes are made of three parts: a 2-letter department, a 4-digit product number and a 3-letter colour. To read KT-2291-BLU by hand:
- The department is the first 2 characters: KT.
- The colour is the last 3 characters: BLU.
- The product number starts at the 4th character (after K, T and the dash) and is 4 characters long: 2291.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Code | Dept | Product | Colour |
| 2 | KT-2291-BLU | KT | 2291 | BLU |
| 3 | GD-0418-GRN | GD | 0418 | GRN |
| 4 | BT-7730-RED | BT | 7730 | RED |
LEFT(A2,2): from A2, the first 2 characters.RIGHT(A2,3): from A2, the last 3 characters.MID(A2,4,4): from A2, starting at character 4, take 4 characters.
The zero in GD-0418-GRN stays, because the result is text. That matters below, in "The result won't add up".
When the parts change length: FIND and LEN
UK postcodes have two parts with a space between. The second part is always 3 characters, so =RIGHT(A2,3) gets it. But the first part can be 2, 3 or 4 characters: M1, B33, LS6, SW1A. LEFT needs to know how many to take, so find the space first:
=LEFT(A2,FIND(" ",A2)-1)
- In LS6 2QT, the space is the 4th character, so
FIND(" ",A2)gives 4. - The part before it is 4 − 1 = 3 characters long.
LEFT(A2,3)gives LS6.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Postcode | Area part | Last part | Length |
| 2 | LS6 2QT | LS6 | 2QT | 7 |
| 3 | M1 4BT | M1 | 4BT | 6 |
| 4 | SW1A 9ZZ | SW1A | 9ZZ | 8 |
LEN counts the characters, spaces included. Because the last part is always 3 characters after a space, =LEFT(A2,LEN(A2)-4) gives the first part too: LS6 2QT is 7 characters, and 7 − 4 = 3.
Everything after a character
An email address splits at the @. Before it is the person's name, after it is the company's domain. Take grace.reid@acme.co.uk:
- The @ is the 11th character, so
FIND("@",A2)gives 11. - The name is the 10 characters before it:
=LEFT(A2,FIND("@",A2)-1)gives grace.reid. - The whole address is 21 characters long. 21 − 11 = 10 characters come after the @, so
=RIGHT(A2,LEN(A2)-FIND("@",A2))gives acme.co.uk.
| A | B | C | |
|---|---|---|---|
| 1 | Name part | Domain | |
| 2 | grace.reid@acme.co.uk | grace.reid | acme.co.uk |
| 3 | t.fraser@northcol.ac.uk | t.fraser | northcol.ac.uk |
| 4 | priya@shahdesign.com | priya | shahdesign.com |
MID does the same job with less arithmetic: start one after the @ and take plenty, =MID(A2,FIND("@",A2)+1,100). MID stops at the end of the text, so 100 is just "more than enough".
The same idea takes characters off the start or the end. =RIGHT(A2,LEN(A2)-3) takes the first 3 characters off, and =LEFT(A2,LEN(A2)-4) takes the last 4 off, so KT-2291-BLU becomes KT-2291.
A middle part of any length
Some product numbers have 5 digits, as in KT-22910-BLU. MID still starts at the 4th character, but now it needs to take 5. Every code has the same 2 letters, 2 dashes and 3-letter colour around the number, which is 7 characters, so the number is the length minus 7:
=MID(A2,4,LEN(A2)-7)
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Code | Length | Product | Product (365) |
| 2 | KT-2291-BLU | 11 | 2291 | 2291 |
| 3 | KT-22910-BLU | 12 | 22910 | 22910 |
| 4 | GD-418-GRN | 10 | 418 | 418 |
Column D gets the same answers in Excel 365 by splitting at the dashes, as in the next section.
The easier way in Excel 365: TEXTBEFORE and TEXTAFTER
In Microsoft 365 and Excel 2024, you say what to split at, rather than counting characters:
=TEXTBEFORE(A2," ")gives the postcode's first part.=TEXTAFTER(A2," ")gives its last part.=TEXTAFTER(A2,"@")gives an email address's domain.=TEXTBEFORE(TEXTAFTER(A2,"-"),"-")gives whatever is between the two dashes, however long it is.
They're not in Excel 2021 or older, which show #NAME? (see the #NAME? error). To split into several columns at once, see TEXTSPLIT. For names, splitting first and last names uses the same functions for first names, surnames, middle names and initials.
The result won't add up
LEFT, MID and RIGHT always give text, even when it looks like a number. So 2291 taken out of a code can't be added up, and won't match a real 2291 in a lookup. Wrap it in VALUE to turn it into a number:
| A | B | C | |
|---|---|---|---|
| 1 | Code | MID | VALUE(MID) |
| 2 | KT-2291-BLU | 2291 | 2291 |
| 3 | GD-0418-GRN | 0418 | 418 |
| 4 | BT-7730-RED | 7730 | 7730 |
| 5 | Total | 0 | 10439 |
VALUE drops the leading zero, so 0418 becomes 418. If the zeros matter, as in a code you look up on another list, keep it as text and make sure the other list is text too. The TEXT function puts leading zeros back on a number.
Common mistakes
The space comes along too
=LEFT(A2,FIND(" ",A2)) includes the space, because FIND gives the space's own position. Take 1 off.
#VALUE! when there's nothing to find
If a postcode was typed with no space, such as LS62QT, FIND has nothing to find and gives #VALUE!. Wrap it to show a note instead: =IFERROR(LEFT(A2,FIND(" ",A2)-1),"No space"). FIND also cares about capitals, so FIND("x",A2) won't find an X; SEARCH does the same job and ignores capitals.
Spaces you can't see
A space at the start or end throws the counts out. Tidy the data with TRIM first.
Counting from the wrong place
MID's second part is where to start, counting the first character as 1. To start after the dash in KT-2291-BLU, that's character 4: K is 1, T is 2 and the dash is 3.
Try it now
In B2, take the first part of the postcode in A2: everything before the space. The first parts are different lengths. Your formula will be copied down to B7.
| A | B | |
|---|---|---|
| 1 | Postcode | Area part |
| 2 | LS6 2QT | |
| 3 | M1 4BT | |
| 4 | SW1A 9ZZ | |
| 5 | B33 8TH | |
| 6 | NE12 7XP | |
| 7 | G2 3DL |
Open this in Try a formula: the same sheet with the answer filled in, to change or share by link.
Part of Excel data cleaning practice: every lesson on the topic, with a project in Excel.
Where it works
LEFT, MID, RIGHT, LEN and FIND work in every version of Excel on Windows and Mac, in Excel for the web and in Google Sheets. TEXTBEFORE and TEXTAFTER need Microsoft 365, Excel 2024 or Excel for the web.
Questions
How do I extract text after a character in Excel?
Find the character with FIND, then take everything after it. For the part after an @, use =MID(A2,FIND("@",A2)+1,100), which starts one character after the @ and takes up to 100. In Microsoft 365 or Excel 2024, =TEXTAFTER(A2,"@") does the same in one step. For the part before it, use =LEFT(A2,FIND("@",A2)-1) or TEXTBEFORE.
How do I remove the first or last characters from a cell?
Use LEN to count the characters, then take fewer. =RIGHT(A2,LEN(A2)-3) leaves off the first 3 characters, and =LEFT(A2,LEN(A2)-1) leaves off the last one. For KT-2291-BLU, =LEFT(A2,LEN(A2)-4) gives KT-2291. Copy the formula down, then paste the results back as values if you want to replace the original column.
Why won't the numbers from LEFT or MID add up?
LEFT, MID and RIGHT always give text, even when the result is all digits, so SUM skips them and lookups against real numbers fail. Wrap the formula in VALUE, as in =VALUE(MID(A2,4,4)), to get a number. VALUE drops any zeros at the front, so keep the text version for codes where 0418 and 418 must stay different.
What is the difference between FIND and SEARCH?
Both give the position of some text inside a cell, counting from 1. FIND cares about capitals, so FIND("a","ABC") gives #VALUE!. SEARCH ignores capitals and also lets you use wildcards such as * and ?. Either one works inside LEFT or MID to find a space, a dash or an @, where capitals make no difference.