Reputation: 233
Let's say I have a list of strings and I want to remove specific words from them. I can easily use multiple SUBSTITUTE functions, for example, this will remove the strings in B2, B3 and B4 from the string in A2:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,$B$2,""),$B$3,""),$B$4,"")
How can I make this dynamic so that when I add more terms to remove in the B column they'll be removed automatically from A2. I tried the following methods but they didn't work:
1 - add the B cells as an array
=SUBSTITUTE(A2,{$B$2:$B$4},"")
or =SUBSTITUTE(A2,{$B$2,$B$3,$B$4},"")
2 - Make a single condition
cat|donkey|mouse
3 - Using Indirect and concatenate - I built the correct function as a string (using REPT and CONCATENATE) and tried to activate it with INDIRECT) but this also failed.
Here's the spreadsheet (Col A are the strings to clea, B are the words to remove, D is the manual method that works, F, H and K are the failed 3 attempts). https://docs.google.com/spreadsheets/d/15u8qZ0xQkjvTRrJca6AInoQ4aPkijccouAETE4Gyr9I/edit#gid=0
Upvotes: 0
Views: 3108
Reputation: 1
By using some newly introduced Google Sheet Formula, I have made myself a more generic formula
=MAP(
targetRange,
LAMBDA(
input,
REDUCE(
input,
SPLIT("from1>to1%from2>to2%from3>to3","%"),
LAMBDA(input, regex,
REGEXREPLACE(
input,
REGEXEXTRACT(regex,"(.*)>"),
REGEXEXTRACT(regex, ">(.*)")
) ) ) ) )
The part SPLIT("from1>to1%from2>to2%from3>to3","%")
could also be replaced dynamically as
BYROW(
subsTableRange,
LAMBDA(r, TEXTJOIN(">", 1, r)
)
Upvotes: 0
Reputation: 36890
You can also try-
=TEXTJOIN(" ",TRUE,FILTER(SPLIT(A2," "),ISERROR(MATCH(SPLIT(A2," "),$B$2:$B$7,0))))
Upvotes: 0
Reputation: 27282
In the 'Copy' of the tab I entered
=ArrayFormula(IF(LEN(A2:A), REGEXREPLACE(A2:A, TEXTJOIN("|", 1, B2:B),),))
See if that works for you?
Upvotes: 3