This post was prompted by a support query about automating the extraction of client names from cells that could contain other information after the name. Based on the use of one or more delimiter characters to separate the client name from the additional information, we are going to look at using the TEXTBEFORE() function with different delimiter alternatives.
Introduction
TEXTBEFORE()
There are a few ways to extract text strings using a delimiter, including using Power Query which would be particularly appropriate if several other processing steps were also required. However, we will concentrate on using some Excel functions.
In the days before the introduction of the advanced text functions TEXTBEFORE(), TEXTAFTER() and TEXTSPLIT(), it would have been necessary to use the FIND() or SEARCH() functions to discover the position of the delimiter character or characters, and then to use LEFT(), RIGHT() or MID() text functions as appropriate to extract the string of text characters that represent the client name. In our case, this would have meant using the LEFT() function to extract the characters before the delimiter. Now, the TEXTBEFORE() function makes the process much simpler:
If we use the simplest form of TEXTBEFORE() we just need the first two arguments, the first is the text from which we want to extract our client name, and the second the delimiter character or characters to use. It’s worth stating at the outset that this method will only work if no client names include our delimiter. We are starting by looking for the delimiter set of characters: space dash space:
=TEXTBEFORE([@[Sales details]]," - ")
Dealing with those cells that only contain the client name is easy enough. One of the optional parameters available in TEXTBEFORE() is the match_end argument. The default setting is ‘0-Don’t match to end’. This means that if our delimiter doesn’t exist in the text, the function will return #N/A as we can see above. We can set this argument to ‘1-Match to end’. This causes TEXTBEFORE() to match the delimiter to the end of the string if no actual match is found, thus including the entire text string:
=TEXTBEFORE([@[Sales details]]," - ",,,1)
Multiple delimiters
Here is our list of delimiters included in our function:
=TEXTBEFORE([@[Sales details]],{" - ","-","--"},,,1)
GROUPBY()
Following on from the recent article on using the new GROUPBY() function, we can use our TEXTBEFORE() formula within GROUPBY() to group our Sales values by client name in a single formula:
=GROUPBY(TEXTBEFORE(Sales[Sales details],Delimiters,,,1),Sales[Sales],SUM)
Additional resources
You can explore all aspects of Excel, including the range of text functions, in the ICAEW archive:
Archive and Knowledge Base
This archive of Excel Community content from the ION platform will allow you to read the content of the articles but the functionality on the pages is limited. The ION search box, tags and navigation buttons on the archived pages will not work. Pages will load more slowly than a live website. You may be able to follow links to other articles but if this does not work, please return to the archive search. You can also search our Knowledge Base for access to all articles, new and archived, organised by topic.