How to use the TEXTBEFORE function
What is the TEXTBEFORE function?
The TEXTBEFORE function extracts a string before a specific substring from a given value. The TEXTBEFORE function is available to Excel 365 users.
Table of Contents
1. Introduction
The TEXTBEFORE function is one of many string manipulation functions in Excel. Here is a list of the other functions:
- The LEFT function returns a specified number of characters from the left side of a text string.
- The RIGHT function returns a specified number of characters from the right side of a text string.
- The MID function returns a specified number of characters from a text string, starting at a specified position.
- The SUBSTITUTE function replaces a specified text string with another text string within a given text.
- The REPLACE function replaces a specified number of characters in a text string with another text string, starting at a specified position.
- The TEXTAFTER function returns all characters in a text string after a specified delimiter or text.
Many of these functions use a given position in a string counting from left to right. For example, 2 represents B in string ABC because it is in the second position counting from left to right. The following functions are very useful for determining positions of characters in a given string.
- The LEN function returns the number of characters in a text string.
- The SEARCH function returns the position of a specified text string within a given text, and is not case-sensitive.
- The FIND function returns the position of a specified text string within a given text, and is case-sensitive.
Cell formatting allows you to manipulate a string based on a formatting code without changing the source value. Select the cell you want to change and then press short cut keys CTRL + 0 (zero) to open the "Cell formatting" dialog box. There you can select a formatting code that changes the output. For example, value 1000 is formatted to show $1,000.
2. Syntax
TEXTBEFORE(input_text,text_before, [n], [ignore_case])
input_text | Required. The original string. |
text_before | Required. The string to search for, text before this string is extracted. |
[n] | Optional. The instance of text_before string, default is 1. |
[ignore_case] | Optional. FALSE represents case sensitive search, default is TRUE. |
3. Example
This example demonstrates how the TEXTBEFORE function can be used to parse and extract specific portions of text based on a given delimiter.
The TEXTBEFORE function is designed to return the portion of text that comes before a specified delimiter. In this case, it's being used to extract the subject of each sentence by finding the text before the words "breathes" and "is" respectively.
Formula in cell E4:
Column B is labeled "input_text" and contains two sentences:
- Row 3: "A blue whale breathes air."
- Row 4: "A dolphin is also breathing air."
Column C is labeled "text_before" and contains:
- Row 3: "breathes"
- Row 4: "is"
Column E is labeled "TEXTBEFORE" and contains the results of the TEXTBEFORE function:
- Row 3: "A blue whale"
- Row 4: "A dolphin"
Explaining formula
Step 1 - TEXTBEFORE function
TEXTBEFORE(input_text,text_before, [n], [ignore_case])
Step 2 - Populate arguments
input_text - B4
text_before - C4
[n] - Optional, default is 1.
[ignore_case] - Optional, default is TRUE meaning not case sensitive
Step 3 - Evaluate function
TEXTBEFORE(B4, C4)
becomes
TEXTBEFORE("A dolphin is also breathing air.", "is")
and returns "A dolphin".
4. TEXTBEFORE Function - case sensitive example
This example shows how to use the TEXTBEFORE function also considering upper and lower letters. The last argument determines if the function ignores upper and lower case letters based on boolean values TRUE or FALSE.
TEXTBEFORE(input_text,text_before, [n], [ignore_case])
FALSE makes the function case sensitive to the input values. Cell B3 contain "A blue whale breathes air.", cell C3 contains "a".
Formula in cell E4:
The formula returns "A blue wh" in cell E3. Since it has FALSE in the last argument the function performs a case sensitive search which doesnt match "a" to "A" which is located at the first position: "A blue whale breathes air." but in "a" located in the 10th position. This extracts the first 9 characters shown in cell E3: "A blue wh"
Explaining formula
Step 1 - TEXTBEFORE function
TEXTBEFORE(input_text,text_before, [n], [ignore_case])
Step 2 - Populate arguments
input_text - B4
text_before - C4
[n] - Optional, default is 1.
[ignore_case] - FALSE meaning case sensitive.
Step 3 - Evaluate function
TEXTBEFORE(B4, C4,,FALSE)
becomes
TEXTBEFORE("A blue whale breathes air.", "a", , FALSE)
and returns "A blue wh".
5. TEXTBEFORE Function alternative - older Excel versions
The TEXTBEFORE function lets you perform both a regular and a case-sensitive search based on the fourth argument [ignore_case]. The SEARCH and FIND functions are used in the alternative formulas below.
5.1 TEXTBEFORE Function alternative
This formula works in all Excel versions.
Formula in cell E4:
Explaining formula
Step 1 - Find character position of substring
The SEARCH function returns a number representing the position of character at which a specific text string is found reading left to right. It is NOT a case-sensitive search.
SEARCH(find_text,within_text, [start_num])
SEARCH(C3, B3)-1
becomes
SEARCH("breathes", "A blue whale breathes air.")-1
becomes
14-1 equals 13.
Step 2 - Extract text before based on position
The LEFT function extracts a specific number of characters always starting from the left.
LEFT(text, [num_chars])
LEFT(B3, SEARCH(C3, B3)-1)
becomes
LEFT("A blue whale breathes air.", 13)
and returns "A blue whale ".
5.2 TEXTBEFORE Function alternative - case sensitive
This formula performs a case-sensitive search in order to extract text before the given substring.
Formula in cell E3:
Explaining formula
Step 1 - Find character position of substring
The FIND function returns a number representing the position of character at which a specific text string is found reading left to right. This function performs a case-sensitive search.
FIND(find_text,within_text, [start_num])
FIND(C3, B3)-1
becomes
FIND("breathes", "A blue whale breathes air.")-1
becomes
10-1 equals 9.
Step 2 - Extract text before based on position
The LEFT function extracts a specific number of characters always starting from the left.
LEFT(text, [num_chars])
LEFT(B3, FIND(C3, B3)-1)
becomes
LEFT(B3, 9)
becomes
LEFT("A blue whale breathes air.", 9)
and returns "A blue wh".
Useful links
TEXTBEFORE function - Microsoft
Excel TEXTBEFORE function - extract text before character (delimiter)
Functions in 'Text' category
The TEXTBEFORE function function is one of 29 functions in the 'Text' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form