Ever needed to extract just a portion of text from a larger string in Excel? This can be particularly helpful when working with lists of names, product IDs, or even URLs. Enter Excel's TEXTBEFORE
and TEXTAFTER
functions, two powerful tools designed to make extracting specific parts of text as easy as pie. The TEXTBEFORE
function grabs everything before a specified character, and TEXTAFTER
does the opposite—it grabs everything after. Whether you're separating names, trimming URLs, or cleaning up data, these functions have got your back!
Syntax
Let’s break down how these functions work with their syntax.
Function | Syntax |
---|---|
TEXTBEFORE | =TEXTBEFORE(text, delimiter, [instance_num]) |
TEXTAFTER | =TEXTAFTER(text, delimiter, [instance_num]) |
text
: The text string from which you want to extract the information.delimiter
: The character (or characters) that marks where you want to cut the text.[instance_num]
: (Optional) Which instance of the delimiter you want to use if there are multiple. By default, it will look for the first instance.
Example
Let’s say you have a list of product codes where each code contains a department and a unique identifier separated by a dash. You want to extract the department and the unique identifier separately.
Product Code | Department (TEXTBEFORE) | Unique ID (TEXTAFTER) |
---|---|---|
HR-12345 | =TEXTBEFORE(A2, "-") | =TEXTAFTER(A2, "-") |
FIN-98765 | =TEXTBEFORE(A3, "-") | =TEXTAFTER(A3, "-") |
IT-54321 | =TEXTBEFORE(A4, "-") | =TEXTAFTER(A4, "-") |
In this example, the TEXTBEFORE
function extracts the department (HR
, FIN
, IT
) and the TEXTAFTER
function extracts the unique identifier (12345
, 98765
, 54321
).
Practice Exercise 1
Task: You have a list of full names in the format "First Last", and you need to separate them into first and last names using TEXTBEFORE
and TEXTAFTER
.
Hint: Use the space (" ") as the delimiter to separate the first and last names.
Practice Exercise 2
Task: You have a list of file paths in the format "C:\Folder\Subfolder\File.ext", and you need to extract the folder name and the file extension separately. The folder name should be everything before the second backslash (\
), and the file extension should be everything after the last period (.
).
Hint:
- Use the second backslash (
\
) as the delimiter for the folder name extraction. - Use the period (
.
) as the delimiter to extract the file extension.
Solution to Exercise 1
Extracting first and last names from full names:
=TEXTBEFORE(A2, " ")
=TEXTAFTER(A2, " ")
=TEXTBEFORE(A3, " ")
=TEXTAFTER(A3, " ")
=TEXTBEFORE(A4, " ")
=TEXTAFTER(A4, " ")
Solution to Exercise 2
Extracting the folder name and file extension from file paths:
=TEXTBEFORE(A2, "\", 2)
=TEXTAFTER(A2, ".")
=TEXTBEFORE(A3, "\", 2)
=TEXTAFTER(A3, ".")
=TEXTBEFORE(A4, "\", 2)
=TEXTAFTER(A4, ".")
These exercises cover different scenarios where the TEXTBEFORE
and TEXTAFTER
functions are useful—whether you're separating names, cleaning file paths, or working with any other text-based data. Happy extracting!