简体   繁体   中英

MS Excel Formula: Find & Remove Multiple Matches from Text using Range List

Using a MS Excel formula, I would like a formula that will search within a Cell (B2) to find any/all specific matching keywords found in range (Column A2:A63) and remove multiple keywords from string (B2) and only leave the remaining text.

I've attempted to use and tweak the formula provided below without success. It does remove one but not multiple (all) keywords from the text. Not certain if this helps or can be modified to perform the task.

=TRIM(SUBSTITUTE(B2,INDEX($A$2:$A$63,IF(ISNA(MATCH(FALSE,ISERR(FIND($A$2:$A$63,B2)),FALSE)),0,MATCH(FALSE,ISERR(FIND($A$2:$A$63,B2)),FALSE)),1)," "))

Keyword List (Column A2:A63)
Knowing
this is
aren't
didn't

Text Data (Cell B2):
We didn't know where Sally had went, but this isn't the first time she has left us.
Notes: This is the actual text string.

Extracted Keywords (Cell C2):
Results: didn't, know, this, but, had, has, she, We
Notes: This is the formula used: =TEXTJOIN(", ",TRUE,TRIM(IF(ISERR(FIND(""&$A$2:$A$63&"",B2)),"",$A$2:$A$63)))

Desired Output (Cell D2):
Results: where Sally went, isn't the first time left us.
Notes: Output only provides the remaining text string from B2 that are not found in the Keyword List (A2:A63).

Sample MS Excel Remove Multiple Keywords from String

在此处输入图片说明

I would utilize a User Defined Function (UDF) making use of Regular Expressions.

We construct a regular expression out of the Keyword List, then apply that using the Regex.Replace function.

eg:

Option Explicit
Function remKeyWords(rkeyWordList As Range, sText As String) As String
    Dim RE As Object
    Dim sPat As String
    Dim v, w

v = rkeyWordList
For Each w In v
    If w <> "" Then _
       sPat = sPat & "|" & w
Next w

sPat = "\b(?:" & Mid(sPat, 2) & ")\b"

Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .Pattern = sPat
    .ignorecase = True
    remKeyWords = WorksheetFunction.Trim(.Replace(sText, ""))
End With

End Function

In the below, you might use this as:

D2: =remKeyWords($A$1:$A$1000,B2)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM