3

I've been following the guide on Exceljet for how to create a recursive LAMBDA function that removes all numbers from a string. For example, A1B2C3D4E5 becomes ABCDE.

The only thing I wanted differently was to have the string containing the character omissions stored inside the function rather than the cell. So, I took away the chars parameter from the opening line and used the LET function to store the string.

The problem is I keep getting a #VALUE! error and can't work out why.

=LAMBDA(str,sub,

LET(chars,"0123456789",

IF(chars="",str,

ReplaceChars(

SUBSTITUTE(str,LEFT(chars),sub),
MID(chars,2,LEN(chars)-1),
sub
)
)
))
ZygD
  • 22,092
  • 39
  • 79
  • 102
Statto
  • 410
  • 3
  • 9

1 Answers1

1

A nested LET() in a recursive LAMBDA() is going to be troublesome since every time you'd use the variable in the recursion you'd start from scratch. Replacing characters will therefor never stop (if my interpretation of this is correct). But in your case you don't even need to make a recursive LAMBDA() to replace numbers, for example:

=LAMBDA(str,LET(X,MID(str,SEQUENCE(LEN(str)),1),CONCAT(IF(ISNUMBER(X*1),"",X))))

Core of this function is now:

=LET(X,MID(A1,SEQUENCE(LEN(A1)),1),CONCAT(IF(ISNUMBER(X*1),"",X)))

which can be used outside of LAMBDA() just fine. We can even replace this with

=LET(X,MID(A1,SEQUENCE(LEN(A1)),1),CONCAT(IF(ISNUMBER(FIND(X,"0123456789")),"",X)))

to do the same thing with any character you want to replace.

Now you can call =REPLACECHARS(A1). However, IMHO the beauty of the use of variables is now lost and the original approach (for example here would have my personal preference.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JvdV
  • 70,606
  • 8
  • 39
  • 70