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".
6. Function not working
The TEXTBEFORE function returns
- #N/A! error if you use a non-existing delimiter. Note that the TEXTBEFORE function performs a case sensitive action.
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!), the function will return the same error.
6.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
6.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference C3 converted to hard-coded value using the F9 key. The TEXTBEFORE function requires a valid delimtier which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
6.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
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