Take part of a text with LEFT, RIGHT and MID
Text functions7 min
Builds on Decide with IF.
Order numbers and product codes often pack several facts into one cell. When finance asks for the country of every order, or the warehouse wants stock counted by color, the answer is already inside the code.
How it works
Excel treats text as a row of characters, numbered from 1 on the left. Every character counts, spaces and dashes included. In the asset tag LT-2024-0587, L is character 1, the first dash is character 3, and the year runs from character 4 to character 7.
Three functions copy characters out of that row. Each one takes the text, usually a cell, and how many characters you want:
| Row | A | B | C |
|---|---|---|---|
| 1 | Function | Takes characters from | Arguments |
| 2 | LEFT | the start | the text, how many |
| 3 | RIGHT | the end | the text, how many |
| 4 | MID | any position | the text, where to start, how many |
They suit codes with a fixed layout, where the part you want sits at the same position on every row. RIGHT also copes with codes of different lengths when the part you want always comes last, because it counts back from the end.
The result is always text, even when it looks like a number. That's why 0587 keeps its leading zero.
Example
IT keeps a register of laptops, one asset tag each. The tag in F2 is LT-2024-0587: the type, the year bought and a serial number.
=MID(F2,4,4)It returns 2024. =LEFT(F2,2) returns LT, and =RIGHT(F2,4) returns 0587.
Common mistakes
=MID(F2,3,4)returns-202. The start is the position of the first character you want, counting from 1 and counting the dashes.- A fixed count breaks when the first part changes length: for a desk tagged
DSK-2023-0112,=LEFT(F5,2)returns DS. The next lesson finds the dash with FIND, so the count can change from row to row. 0587from RIGHT is text, so it sits on the left of the cell and SUM skips it. Wrap the result in VALUE when you need a number.
In short
- Characters are numbered from 1, and spaces and dashes count.
- LEFT takes from the start, RIGHT from the end, and MID from any position you choose.
- The result is text, so leading zeros stay.
Practice
4 exercises · free with an account