Search for text in cell, if it exists add to another cell, comma separated

mdsurf

New Member
Joined
Aug 1, 2017
Messages
22
Office Version
  1. 2016
Platform
  1. MacOS
Hi! I'm using a CSV file and trying to figure out how to create a specific formula or if it's even possible.

I have a CSV file with a column that contains around 1,500 cells. Each cell contains one written sentence. I have a list of keywords I would like to search for in these cells and if they exist, write them down, comma separated into another cell. Not sure if I'm explaining this clearly so hopefully the example below makes sense.


Keywords to search for, in a separate tab
Football
Soccer
Basketball court
Baseball
Tennis player


Cells with sentenceFormula that searches sentence for keywords and lists the keywords found, comma separated
Jimmy likes to play football on the weekendsfootball
Sara is an excellent swimmerNone
Carl is a tennis player and also likes to play soccer when he cantennis player, soccer
The football team often plays baseball in the summerfootball, baseball
The school has a basketball court as well as a soccer fieldbasketball court, soccer
Tomorrow the tennis player will play soccer insteadtennis player, soccer

No idea if this is possible or not, but thank you in advance for the help.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
For office 365:

Keywords in I1:I5
Excel Formula:
=TEXTJOIN(", ",TRUE,FILTER($I$1:$I$5,ISNUMBER(SEARCH($I$1:$I$5,A2)),"None"))

Earlier versions: (close with ctr+shift+return, its an array formula). Only thing here is a comma at the end of the string.
Excel Formula:
=CONCAT(IFERROR(INDEX($I$1:$I$5,IF(ISNUMBER(SEARCH($I$1:$I$5,A2)),ROW($1:$5),""))&", ",""))
 
Last edited:
Upvote 0
Earlier versions: (close with ctr+shift+return, its an array formula). Only thing here is a comma at the end of the string.
That will only work as far back as 2019 so you could use textjoin instead of concat to eliminate the additional comma at the end.
Versions older than 2019 had concatenate, not concat. The earlier version does not support arrays.
 
Upvote 0
Assuming MS365 and that the whole "actual words" in the key word list need to exist in the text (refer A8 which does not have the word football in it), you could try the modification shown in column B

For all versions and assuming that the 'case' of the final list does not have to match the key word list but can match the case in the original text, you could try this user-defined function which is used in column C. Post back if you need instructions for implementing the udf.

VBA Code:
Function WordList(s As String, ListOfWords As Range) As String
  Dim RX As Object, M As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  RX.Pattern = "\b(" & Join(Application.Transpose(ListOfWords), "|") & ")\b"
  If RX.Test(s) Then
    For Each M In RX.Execute(s)
      WordList = WordList & ", " & M
    Next M
    WordList = Mid(WordList, 3)
  Else
    WordList = "None"
  End If
End Function

mdsurf.xlsm
ABCDI
1Cells with sentenceFootball
2Jimmy likes to play football on the weekendsFootballfootballSoccer
3Sara is an excellent swimmerNoneNoneBasketball court
4Carl is a tennis player and also likes to play soccer when he canSoccer, Tennis playertennis player, soccerBaseball
5The football team often plays baseball in the summerFootball, Baseballfootball, baseballTennis player
6The school has a basketball court as well as a soccer fieldSoccer, Basketball courtbasketball court, soccer
7Tomorrow the tennis player will play soccer insteadSoccer, Tennis playertennis player, soccer
8John is from a footballing familyNoneNone
9
Sheet1
Cell Formulas
RangeFormula
B2:B8B2=TEXTJOIN(", ",TRUE,FILTER(I$1:I$5,ISNUMBER(SEARCH(" "&I$1:I$5&" "," "&A2&" ")),"None"))
C2:C8C2=WordList(A2,I$1:I$5)
 
Upvote 0

Forum statistics

Threads
1,223,959
Messages
6,175,647
Members
452,663
Latest member
MEMEH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top