Example 1
The part of an email address before the @
C2 =TEXTBEFORE(A2,"@")
Result: dana.whitfield
Everything before the first @. The same formula works for addresses of any length.
C2 =TEXTBEFORE(A2,"@") returns dana.whitfield
Returns the text that comes before a delimiter, counting from the start or the end of the text.
Syntax
=TEXTBEFORE(, , , , , )
Excel for Microsoft 365, Excel 2024 and later, and Excel for the web. Not available in Excel 2021 or earlier.
Try it
Edit the formula in C2 or change the inputs. The sheet recalculates as you type.
C2 =TEXTBEFORE(A2,"@") returns dana.whitfield
TEXTBEFORE returns the text that comes before a delimiter. It does in one step what LEFT with FIND needed before: take the part of a code up to a dash, the user name in front of the @, or everything before the last slash.
#N/A.=TEXTBEFORE("dana.whitfield@example.com","@") returns dana.whitfield.
With an instance number you can stop at any delimiter. For PW-24-0113-A7:
=TEXTBEFORE(A2,"-") returns PW, everything before the first dash.=TEXTBEFORE(A2,"-",2) returns PW-24, everything before the second.=TEXTBEFORE(A2,"-",-1) returns PW-24-0113, everything before the last.The last one is the real gain over older formulas. Counting from the end used to need a long trick with SUBSTITUTE and FIND.
By default the formula returns #N/A when the delimiter isn't found. That can break a column, so give the sixth argument. =TEXTBEFORE(A2,"-",1,0,0,"No dash") returns No dash. If you would rather have the whole text back, use =TEXTBEFORE(A2,"-",1,0,1), where match_end treats the end of the text as the delimiter. Wrapping the formula in IFERROR also works.
The older equivalent is =LEFT(A2,FIND("-",A2)-1). Both give the same result, and TEXTBEFORE is easier to read and has the instance, case and not-found options built in. LEFT and FIND work in every version of Excel. If the file must open in Excel 2021 or earlier, use them.
TEXTBEFORE returns text, even if it looks like a number. To get a number, wrap it in VALUE. It also keeps any spaces next to the delimiter, so "Smith, Ana" split at "," returns Smith, but split at " " returns Smith,. Wrap the result in TRIM when spaces may creep in.
TEXTAFTER is the mirror image. Put one inside the other to extract the text between two markers: =TEXTBEFORE(TEXTAFTER(A2,"["),"]") returns what is inside the first pair of square brackets. To get every piece rather than one, use TEXTSPLIT.
The delimiter can be an array of alternatives: =TEXTBEFORE(A2,{"-","/"}) stops at whichever of the two comes first.
A2."@" or "-". It can be several characters long, or an array of alternatives such as {"-","/"}.1, the first. A negative number counts from the end, so -1 is the last one. 0, or a number larger than the text length, returns #VALUE!.0, the default, is case-sensitive. 1 ignores case.1 treats the end of the text as a delimiter. The default, 0, doesn't."No dash". By default TEXTBEFORE returns #N/A.Each example is a live sheet. Change the inputs or the formula and watch the result.
Example 1
C2 =TEXTBEFORE(A2,"@")
Result: dana.whitfield
Everything before the first @. The same formula works for addresses of any length.
C2 =TEXTBEFORE(A2,"@") returns dana.whitfield
Example 2
C2 =TEXTBEFORE(A2,"-",2)
Result: PW-24
instance_num 2 stops at the second dash, so the first dash stays in the result.
C2 =TEXTBEFORE(A2,"-",2) returns PW-24
Example 3
C3 =TEXTBEFORE(A3,"-",-1)
Result: GL-25-0207
-1 means the last dash. This removes the lot code however many dashes come before it.
C3 =TEXTBEFORE(A3,"-",-1) returns GL-25-0207
Example 4
C3 =TEXTBEFORE(A3,"-",1,0,0,"No dash")
Result: No dash
FX1085 has no dash. The sixth argument replaces the #N/A that TEXTBEFORE would otherwise return.
C3 =TEXTBEFORE(A3,"-",1,0,0,"No dash") returns No dash
Example 5
C2 =TEXTBEFORE(A2," of ",1,1)
Result: Page 3
The delimiter is written in lower case, but the text has OF in capitals. match_mode 1 ignores the case difference.
C2 =TEXTBEFORE(A2," of ",1,1) returns Page 3
Solve it in the sheet. Every answer is checked, with hints if you need them.
Task 1 of 2
In B2, return the part of the email address in A2 before the @. Fill it down to B5.
Answer in B2, then it fills down to B5.
#N/A. Give if_not_found a value, or set match_end to 1 to get the whole text back.#VALUE!. An instance number higher than the number of delimiters returns #N/A.", ".#NAME?. Use LEFT with FIND there.Use `=TEXTBEFORE(A2,"-")`, with your character in quotes. In older versions, use `=LEFT(A2,FIND("-",A2)-1)`.
Set instance_num to -1: `=TEXTBEFORE(A2,"-",-1)`. A negative number counts delimiters from the end of the text.
The delimiter isn't in the text, or there are fewer occurrences than instance_num asks for. Check the spelling and the case, or supply if_not_found, as in `=TEXTBEFORE(A2,"-",1,0,0,"None")`.
Not by default. Set match_mode to 1, for example `=TEXTBEFORE(A2,"of",1,1)`.
TEXTBEFORE returns one piece, the text before a chosen delimiter. TEXTSPLIT returns all the pieces and spills them across cells or down rows.
TEXTBEFORE is taught in Logic and text, lesson 13: TEXTBEFORE and TEXTAFTER. Each lesson is a short, graded exercise like the one above.