How to use TEXTBEFORE, TEXTAFTER in Excel
TEXTBEFORE returns the text before a separator and TEXTAFTER the text after it. They replace the old LEFT/MID + FIND combinations with one readable function.
Syntax
=TEXTBEFORE(text, delimiter, [instance]) · TEXTAFTER(text, delimiter, [instance])
text | The text to cut, e.g. an email in A2. |
delimiter | The separator, e.g. "@", "-" or " ". |
[instance] | Optional. Which separator to use: 2 for the second, -1 for the last. |
Examples
=TEXTBEFORE(A2, "@")The user name of an email address.=TEXTAFTER(A2, "@")The domain.=TEXTAFTER(A2, " ", -1)The last word, like a surname.=TEXTBEFORE(TEXTAFTER(A2, "-"), "-")The middle part of BG-1024-SOF: 1024.Emails, names and codes
Most splitting jobs are "everything before X" or "everything after X". These functions say exactly that.
=TEXTBEFORE(A2, " ")The first name from "Anna Petrova".=TEXTAFTER(A2, ", ")The first name from "Petrova, Anna".=TEXTBEFORE(A2, ".", -1)A file name without its extension.Old way vs new way
The same result with LEFT and FIND, for files that must work in older Excel.
=LEFT(A2, FIND("@", A2)-1)Before the @, in every version.=MID(A2, FIND("@", A2)+1, 99)After the @, in every version.Common mistakes
- #N/A error
- The separator isn't in the text. Give a 6th argument for that case, or wrap the formula in IFNA.
- #NAME? error
- Excel 2021 and older don't have these functions. Use LEFT/MID with FIND there.
- The result is text
- Even digits come back as text. Add
--in front to get a number.
Frequently asked questions
Which Excel versions have TEXTBEFORE and TEXTAFTER?
Microsoft 365 and Excel for the web. Excel 2021 and older don't; use LEFT and MID with FIND there.
How do I get the text after the last space?
Use -1 as the instance: =TEXTAFTER(A2, " ", -1).
Why do I get #N/A?
The separator isn't in the text. Add a value for that case as the 6th argument, or wrap the formula in IFNA.
How do I split text into several columns at once?
TEXTSPLIT does it in Microsoft 365: =TEXTSPLIT(A2, "-"). In older versions use Data → Text to Columns.
Available in: Microsoft 365, Excel for the web, Google Sheets