
The `TEXTBEFORE` function in Excel is used to extract a portion of text from a string that appears before a specified delimiter. This function is especially useful when you need to separate or manipulate data where specific text elements are divided by consistent characters or strings.
Syntax:
TEXTBEFORE(text, delimiter, [instance_num], [match_mode], [match_end], [if_not_found])
Arguments:
- text: The string from which you want to extract text. This is a required argument.
- delimiter: The character or string that marks the point before which the text is extracted. This is also required.
- instance_num (optional): Determines which occurrence of the delimiter to consider. The default value is 1, meaning the first occurrence.
- If positive, it counts from the start.
- If negative, it counts from the end.
- match_mode (optional): Specifies whether the function is case-sensitive.
- 0 for case-sensitive (default),
- 1 for case-insensitive.
- match_end (optional): Defines whether to search for the delimiter at the end of the text string.
- FALSE searches from the start (default),
- TRUE searches from the end.
- if_not_found (optional): The text to return if the delimiter is not found in the text. By default, the function returns a `#N/A` error.
Example Usage:
- Basic Example:
To extract text before the first comma from cell A1:
=TEXTBEFORE(A1, ",")
If A1 contains “Hello, World”, this formula returns “Hello”.
- Specifying an instance number:
To get text before the second comma:
=TEXTBEFORE(A1, ",", 2)
If A1 contains “Hello, World, Excel”, this formula returns “Hello, World”.
- Case-insensitive extraction:
To perform a case-insensitive search for “X” as a delimiter:
=TEXTBEFORE(A1, "X", , 1)
- Handling when delimiter is not found:
Set a default return value if the delimiter is not found:
=TEXTBEFORE(A1, ",", 1, 0, FALSE, "Delimiter not found")
If there’s no comma in A1, this returns “Delimiter not found”.
Using the `TEXTBEFORE` function effectively allows you to handle text data with precision, providing flexibility in dealing with various string manipulation scenarios in spreadsheets.