The Excel workbook is included with our video training.

Abstract 

In this video, we look at how to configure TEXTAFTER and TEXTBEFORE to handle situations where the delimiter is not found. One option is to use the match_end argument to treat the end of the text like a delimiter. Another option is to use the if_not_found argument to supply a custom value. The video explains both options.

Transcript 

In this video, we'll look at some advanced options for the TEXTBEFORE and TEXTAFTER functions.

In this first example, let's look at how to configure TEXTBEFORE and TEXTAFTER to handle situations when a delimiter is not found.

In column B, we have a list of projects with status. The delimiter between the project and status is a hyphen surrounded by space. The goal is to extract the project and status into separate columns.

First, let's get the project name.

This is a task for the TEXTBEFORE function

For the text, I'll go ahead and use the range B5:B16. This will cause the formula to spill all results at the same time.

For the delimiter, I need to use three characters: a space, a hyphen, and a space.

This configuration works in most cases, but notice that two projects in the list don't have a status, and fail with an N/A error because TEXTBEFORE cannot find the delimiter.

One way to handle this problem is to set the optional match_end argument to 1.

Effectively, this makes TEXTBEFORE treat the end of the text like a delimiter.

When I update the formula, we get all the project names without errors.

Another way to handle this situation is to provide a value for the last argument, called if_not_found.

I'll first undo my previous change.

Then I'll provide the range B5:B16 for if_not_found.

Now when a delimiter is not found, TEXTBEFORE returns the original value.

Next, let's get the status with the TEXTAFTER function.

The values for text and delimiter are the same.

Like TEXTBEFORE, TEXTAFTER returns an error in cases where the delimiter is not found.

However, if I set match_end to 1, TEXTAFTER returns a blank result when the status is not found.

This makes sense, since TEXTAFTER now treats the end of the text as a delimiter, and there is nothing after that.

This works petty well, since the result accurately reflects the source data in column B.

But perhaps I want to return a custom value?

In that case, I can use the if_not_found argument.

I'll first remove the match_end setting.

To show a blank result, I can set if_not_found to an empty string ("").

I can also pass in any value I like. For example, I could use three question marks, "???".

Or, the text "<no status>".

One thing to be aware of is that the match_end setting will override the if_not_found setting.

For example, if I now set match_end, to 1 my custom value disappears, since match_end takes precedence.

Dave Bruns Profile Picture

AuthorMicrosoft Most Valuable Professional Award

Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.