Exceljet

Quick, clean, and to the point

LAMBDA strip trailing characters recursive

Excel formula: LAMBDA strip trailing characters recursive
Generic formula 
=LAMBDA(str,char,
  IF(RIGHT(str)<>char,str,
    StripTrailingChars(
      MID(str,1,LEN(str)-1),
      char
    )
  )
)
Summary 

The LAMBDA function can be used to create custom, reusable functions. In the example shown, cell D5 contains the custom LAMBDA function "StripTrailingChars":

=StripTrailingChars(B5,C5)

The StripTrailingChars function is designed to accept two input parameters:  str is the text from which to remove trailing characters, and char is a the trailing character to remove. In the example shown, StripTrailingChars is configured to remove the characters seen in column C from the text seen in column B. These characters will only be removed when the are "trailing", i.e. when the appear at the end of the string. This custom LAMBDA formula illustrates a feature called recursion, in which a function calls itself.

Note: the LAMBDA function is available through the beta channel of Excel 365 only.

Explanation 

LAMBDA function can be used to create custom, reusable functions in Excel. This example illustrates a feature called recursion, in which a function calls itself. Recursion can be used to create elegant, compact, non-redundant code. This example is primarily proof of concept, to show a very simple recursive LAMBDA function.

When creating a recursive LAMBDA formula a key consideration is how the formula will "exit" the loop it performs by calling itself. In this example, before the recursive call happens, the IF function is used to check the last character of of the input text string (str). If the last character is not equal to the target character (char) the function exits and returns the current value of str. Otherwise, the formula calls itself:

=LAMBDA(str,char,
  IF(RIGHT(str)<>char,str, // test and exit if needed
    StripTrailingChars( // recurse
      MID(str,1,LEN(str)-1),
      char
    )
  )
)

The actual removal of the trailing character is handled by the MID function and LEN function:

MID(str,1,LEN(str)-1)

The MID function returns the value of str without the last character. MID strips one character at a time, which is why this formula is recursive. The result is given to the StripTrailingChars function, along with the original value for char. When all the target trailing characters have been removed, the function returns the current value for str.

You can find more general information about the LAMBDA function here.

LAMBDA is a beta function available in Excel 365 only.
Author 
Dave Bruns

Excel Formula Training

Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.

Download 100+ Important Excel Functions

Get over 100 Excel Functions you should know in one handy PDF.