Skip to content

TEXTBEFORE

Returns the text that comes before a delimiter, counting from the start or the end of the text.

Syntax

=TEXTBEFORE(, , , , , )

Text

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
A
B
C
1
Email
Team
User name
2
dana.whitfield@example.com
Finance
dana.whitfield
3
li@example.com
Sales
4

C2 =TEXTBEFORE(A2,"@") returns dana.whitfield

How TEXTBEFORE works

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.

The arguments

  • text is where to look.
  • delimiter is the marker that ends the part you want.
  • instance_num says which occurrence of the delimiter to use. The default is the first. A negative number counts from the end.
  • match_mode is case-sensitive by default. Set it to 1 to ignore case.
  • match_end set to 1 treats the end of the text as a delimiter, so you get all the text when the delimiter is missing.
  • if_not_found is what to return when the delimiter isn't there. Without it you get #N/A.

=TEXTBEFORE("dana.whitfield@example.com","@") returns dana.whitfield.

Choosing the occurrence

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.

Handling a missing delimiter

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.

Compared with LEFT and FIND

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.

Results are text

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.

Combining with TEXTAFTER

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.

Multiple delimiters

The delimiter can be an array of alternatives: =TEXTBEFORE(A2,{"-","/"}) stops at whichever of the two comes first.

Arguments

text
The text to search. Usually a cell such as A2.
delimiter
The text that marks the end of the part you want, such as "@" or "-". It can be several characters long, or an array of alternatives such as {"-","/"}.
instance_numoptional
Which occurrence of the delimiter to use. The default is 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!.
match_modeoptional
0, the default, is case-sensitive. 1 ignores case.
match_endoptional
1 treats the end of the text as a delimiter. The default, 0, doesn't.
if_not_foundoptional
What to return when the delimiter isn't found, such as "No dash". By default TEXTBEFORE returns #N/A.

Examples

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

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
A
B
C
1
Email
Team
User name
2
dana.whitfield@example.com
Finance
dana.whitfield
3
li@example.com
Sales
4

C2 =TEXTBEFORE(A2,"@") returns dana.whitfield

Example 2

Stop at the second delimiter

C2 =TEXTBEFORE(A2,"-",2)

Result: PW-24

instance_num 2 stops at the second dash, so the first dash stays in the result.

C2
A
B
C
1
Batch code
Qty
Family
2
PW-24-0113-A7
400
PW-24
3
4

C2 =TEXTBEFORE(A2,"-",2) returns PW-24

Example 3

Count from the end with a negative instance

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
A
B
C
1
Batch code
Qty
Without lot
2
PW-24-0113-A7
400
3
GL-25-0207-B12
250
GL-25-0207
4

C3 =TEXTBEFORE(A3,"-",-1) returns GL-25-0207

Example 4

Show your own message when the delimiter is missing

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
A
B
C
1
Code
Owner
Prefix
2
FX-1010
Finance
3
FX1085
Planning
No dash
4

C3 =TEXTBEFORE(A3,"-",1,0,0,"No dash") returns No dash

Example 5

Ignore case when matching the delimiter

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
A
B
C
1
Footer
Source
Page
2
Page 3 OF 10
Report
Page 3
3
4

C2 =TEXTBEFORE(A2," of ",1,1) returns Page 3

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 part of the email address in A2 before the @. Fill it down to B5.

Answer in B2, then it fills down to B5.

B2
A
B
C
D
1
Email
User name
Batch code
Without lot
2
dana.whitfield@example.com
PW-24-0113-A7
3
li@example.com
GL-25-0207-B12
4
priya.nair@example.com
FX-26-0001-C3
5
sam.chen@example.com
PW-25-0450-A10
6
7

Common mistakes

  • By default, a missing delimiter returns #N/A. Give if_not_found a value, or set match_end to 1 to get the whole text back.
  • An instance_num of 0, or one larger than the length of the text, returns #VALUE!. An instance number higher than the number of delimiters returns #N/A.
  • Matching is case-sensitive by default. If the delimiter is a word such as "of", set match_mode to 1.
  • The result is text. A number cut out of the text won't add up until you wrap it in VALUE.
  • Spaces next to the delimiter stay in the result. Wrap the formula in TRIM, or include the space in the delimiter, such as ", ".
  • TEXTBEFORE isn't available in Excel 2021 or earlier, where it shows #NAME?. Use LEFT with FIND there.

Questions people ask

How do I get the text before a character in Excel?

Use `=TEXTBEFORE(A2,"-")`, with your character in quotes. In older versions, use `=LEFT(A2,FIND("-",A2)-1)`.

How do I get the text before the last delimiter?

Set instance_num to -1: `=TEXTBEFORE(A2,"-",-1)`. A negative number counts delimiters from the end of the text.

Why does TEXTBEFORE return #N/A?

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

Does TEXTBEFORE ignore case?

Not by default. Set match_mode to 1, for example `=TEXTBEFORE(A2,"of",1,1)`.

What is the difference between TEXTBEFORE and TEXTSPLIT?

TEXTBEFORE returns one piece, the text before a chosen delimiter. TEXTSPLIT returns all the pieces and spills them across cells or down rows.

Learn TEXTBEFORE in a real sheet.

TEXTBEFORE is taught in Logic and text, lesson 13: TEXTBEFORE and TEXTAFTER. Each lesson is a short, graded exercise like the one above.