Search and pull Keywords from text string

rajtak

Board Regular
Joined
Feb 23, 2009
Messages
74
Hello Everyone,

Would it be possible to pull specific keywords from a text string.

As an example, you will find list of keywords in Column D and the result that I am expecting is in column B. For example: 'wowhead' is listed as a keyword in column D and it is found in the first title so the result should be 'wowhead' in column B, similarly, 'property' and 'certificates' are also listed in column D and both keywords are mentioned in title 2 hence the result should be 'property, certificates' in column B and so on.

Please let me know if this can be achieved by using excel formula or VB script.
2015-08-07_10h19_36.jpg
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I would investigate the InStr( ) function in VBA
 
Upvote 0
Thank you, diddi. I do not know how to code a VB script, all i know how to execute it in excel Alt+F11.

If you could provide a full code or an excel formula would really be appreciated.
 
Upvote 0
Paste this UDF in a standard module

Code:
Function aconcat(A As Variant, Optional sep As String = "") As String
' Harlan Grove, Mar 2002
    Dim y As Variant


    If TypeOf A Is Range Then
        For Each y In A.Cells
            aconcat = aconcat & y.Value & sep
        Next y
    ElseIf IsArray(A) Then
        For Each y In A
            aconcat = aconcat & y & sep
        Next y
    Else
        aconcat = aconcat & A & sep
    End If


    aconcat = Left(aconcat, Len(aconcat) - Len(sep))
End Function

Array formula in B2 copied down
=SUBSTITUTE(Aconcat(IF(ISNUMBER(SEARCH($D$2:$D$11,A2)),", "&$D$2:$D$11,"")),", ","",1)

confirmed with Ctrl+Shift+Enter

Hope this helps

M.
 
Upvote 0
insert a new module and paste this in. make sure the sheet with the data is your active sheet

Code:
Sub SearchText()
    Dim Keys
    
    ReDim Keys(Cells(Rows.Count, "D").End(xlUp).Row - 1)
    For Row = 2 To Cells(Rows.Count, "D").End(xlUp).Row
        Keys(Row - 1) = Cells(Row, 4)
    Next Row
    
    For Row = 1 To Cells(Rows.Count, "A").End(xlUp).Row
        For Key = 1 To UBound(Keys)
            If InStr(1, Cells(Row, 1), Keys(Key)) Then
                temp = temp & Keys(Key) & ", "
            End If
        Next Key
        Cells(Row, 2) = Left(temp, Len(temp) - 1)
        temp = ""
    Next Row
End Sub
 
Upvote 0
Hi Marcelo,

Thank you for the UDF, it works exactly what i wanted but when i changed the cell range, it adds commas at the end. Please find a screenshot below:

2015-08-07_13h18_06.jpg
 
Upvote 0
Hi Diddi,

Thank you for your help. When I hit Run button an error pops up. Please find attached screenshot.
2015-08-07_13h34_42.jpg
 
Upvote 0
Hi Marcelo,

Thank you for the UDF, it works exactly what i wanted but when i changed the cell range, it adds commas at the end.

Try

B2 copied down
=SUBSTITUTE(Aconcat(IF($D$2:$D$1000<>"",IF(ISNUMBER(SEARCH($D$2:$D$1000,A2)),", "&$D$2:$D$1000,""),"")),", ","",1)

Ctrl+Shift+Enter

M.
 
Upvote 0
Here is another UDF for you to consider (it can also be called from other VBA code if need be) which has a much simpler calling mechanism from within a worksheet formula.

Code:
Function GetWords(Text As String, WordList As Variant) As String
  Dim W As Variant, Words As Variant
  For Each W In WordList
    If " " & UCase(Text) & " " Like "*[!A-Z]" & UCase(W) & "[!A-Z]*" Then
      If Len(W) Then GetWords = GetWords & ", " & W
    End If
  Next
  GetWords = Mid(GetWords, 3)
End Function

This function differs from what has been submitted earlier in the following ways...

1) My function finds words from the Keywords List as exact words. Given that, if you had the word "train" in your Keyword List, then "train" would be found only where it was not embedded within another word, like strainer for example (try that with the other code you have received).

2) Because of the way the function works, as described in #1 above, you cannot assume the function will automatically handle plurals (or any other alternate forms of the word for that matter), so whereas your Keyword List had "certificate" in it and wanted "certificates" returned (cells A3/B3 from you example data), my code won't do that... if you want both "certificate" and "certificates" returned, both words must be in your Keyword List. I feel this is good thing given no reasonably constructed code would handle irregular plurals (such as radius..radii).

3) My function is case insensitive which I assumed you wanted given you had "wowhead" in the Keyword List and it matched "Wowhead" in cell A2 for your example data. I can change this if you want.

4) Calling my function from a worksheet cell (using your posted data as an example) is as simple as putting this in cell B2 and copying it down...

=GetWords(A2,D$2:D$11)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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