TEXTBEFORE function

TEXTBEFORE returns the substring before a given delimiter, letting you pick which occurrence and supply your own value when the delimiter is absent.

=TEXTBEFORE(text, delimiter, [instance_num], [match_mode], [match_end], [if_not_found])

Generate a TEXTBEFORE formula

Describe what you need. The generator will reach for TEXTBEFORE where TEXTBEFORE is the right tool, and tell you when it is not.

How to get a better answer
  • Name your columns by letter and by header: "column F (Net Value)" beats "the amount column".
  • State every condition, including the negatives — "not cancelled" changes the formula's shape.
  • Say where the data starts if it is not row 1, and whether it will grow.
  • Check the settings above match your spreadsheet: the wrong argument separator is a syntax error on your machine.

Arguments

How TEXTBEFORE reads its arguments
textrequireddelimiterrequiredinstance_numoptionalmatch_modeoptionalmatch_endoptionalif_not_foundoptionalTEXTBEFORE
ArgumentRequiredDescription
textRequiredString or reference containing the text to search; non-text values are coerced to text, errors propagate.
delimiterRequiredString that marks where to cut; if empty, TEXTBEFORE returns an empty string, and if not found it triggers if_not_found or an error.
instance_numOptionalOptional positive integer indicating which occurrence of delimiter to use; defaults to 1, zero or non-numeric values cause #VALUE!.
match_modeOptionalOptional 0 (exact case-sensitive) or 1 (case-insensitive); defaults to 0, any other value returns #VALUE!.
match_endOptionalOptional 0 (search from start) or 1 (search from end); defaults to 0, other values raise #VALUE!.
if_not_foundOptionalOptional value to return when the delimiter is absent; if omitted, TEXTBEFORE returns #VALUE!.

Returns

Returns a text string; if the delimiter is not found it can return a custom value or an error.

Availability

Excel: 365 · Google Sheets: Not available

Worked examples

1. Extract SKU from a combined SKU-Warehouse string

SKUWarehouseOn HandReorder PointCost
A100East1201005.5
B200West455012.75
C300North2001503.2
=TEXTBEFORE(A2 & "-" & B2, "-")

Result: A100

The formula concatenates the SKU in A2 with a hyphen and the warehouse in B2, producing "A100-East". TEXTBEFORE then returns everything before the first hyphen, which is the SKU "A100".

2. Get the warehouse name from a "Warehouse:On Hand" label

SKUWarehouseOn HandReorder PointCost
A100East1201005.5
B200West455012.75
C300North2001503.2
=TEXTBEFORE(B3 & ":" & C3, ":")

Result: West

B3 holds "West" and C3 holds 45. The concatenation yields "West:45". TEXTBEFORE looks for the colon and returns the text before it, which is the warehouse name "West".

3. Capture SKU and warehouse up to the second hyphen

SKUWarehouseOn HandReorder PointCost
A100East1201005.5
B200West455012.75
C300North2001503.2
=TEXTBEFORE(A4 & "-" & B4 & "-" & A4, "-", 2)

Result: C300-North

A4, B4 and A4 are concatenated into "C300-North-C300". By specifying instance_num = 2, TEXTBEFORE returns everything before the second hyphen, giving the combined SKU and warehouse "C300-North".

Common errors

Which TEXTBEFORE error are you seeing?
TEXTBEFORE returned an error#VALUE!
Add a fourth argument, e.g. "Not found", to return a friendly message instead of an error.
#VALUE!
Use a positive integer such as 1, 2, … to indicate which delimiter occurrence to use.
#REF!
Correct the cell reference so it points to a valid cell containing the source string.
ErrorWhy it happensHow to fix it
#VALUE!The delimiter does not exist in the text and no if_not_found argument was supplied.Add a fourth argument, e.g. "Not found", to return a friendly message instead of an error.
#VALUE!instance_num is set to 0 or a non-numeric value, which TEXTBEFORE cannot interpret.Use a positive integer such as 1, 2, … to indicate which delimiter occurrence to use.
#REF!The text argument refers to a cell that has been deleted or contains a reference error.Correct the cell reference so it points to a valid cell containing the source string.

Tips and when to use something else

  • Use match_mode = 1 to ignore case when the delimiter might appear in different capitalisations.
  • When you need the part after the delimiter, switch to TEXTAFTER – it works the same way but returns the trailing substring.
  • Combine TEXTBEFORE with FIND or SEARCH if you need the position of the delimiter for further calculations.
  • If you only need a fixed number of left-most characters, LEFT is often simpler and faster.

Frequently asked questions

What does TEXTBEFORE return when the delimiter is not in the text?
It returns #N/A. Supply the sixth argument, if_not_found, to get something else instead — an empty string is the usual choice, written as =TEXTBEFORE(A2, "-", 1, 0, 0, ""). Wrapping the whole call in IFNA works too, but the built-in argument is clearer and does not hide other errors.
Can TEXTBEFORE handle multi-character delimiters?
Yes. The delimiter argument can be any string, such as "--" or "|". TEXTBEFORE will look for the exact sequence of characters you specify.
What is the difference between instance_num and match_end?
instance_num chooses which occurrence of the delimiter to cut at, while match_end (0 or 1) decides whether the search starts from the beginning (0) or the end (1) of the text.
Why does TEXTBEFORE sometimes return a numeric value instead of text?
If the text argument is a number, Excel coerces it to text before processing, so the result is still a text string. However, if you concatenate a number with text, the number is first converted to its string representation.

Need a different formula?

The full generator is not scoped to one function — describe any spreadsheet problem and it will pick.

Open the formula generator