Forum Discussion
alex alex
Jun 18, 2018Copper Contributor
Find a word in a cell from a list of words
Hi, Looking for an advice. I have a list of words in a column and sentence in a cell. What would be the formula, to search for these words in a sentence and give a result of matching word in a sep...
- Jun 19, 2018
Alex-
Maybe try using this macro. Since I cannot post a macro enabled workbook due to security risks. You'll need to insert this code in a standard code module for it to work:
VBA:
Sub GetWords() Dim wrdLRow As Integer Dim wrdLp As Integer Dim CommentLrow As Integer Dim CommentLp As Integer Dim fndWord As Integer Dim Sht As Worksheet On Error Resume Next 'Suppress Errors... for when we don't find a match 'Define worksheet that has data on it.... Set Sht = Sheets("Sheet1") 'Get last row for words based on column A wrdLRow = Sht.Cells(Rows.Count, "A").End(xlUp).Row 'Get last row for comments based on column C CommentLrow = Sht.Cells(Rows.Count, "C").End(xlUp).Row 'Loop through lists and find matches.... For CommentLp = 2 To CommentLrow For wrdLp = 2 To wrdLRow 'Look for word... fndWord = Application.WorksheetFunction.Search(Sht.Cells(wrdLp, "A"), Sht.Cells(CommentLp, "C")) 'If we found the word....then If fndWord > 0 Then Sht.Cells(CommentLp, "D") = Sht.Cells(CommentLp, "D") & "; " & Sht.Cells(wrdLp, "A") fndWord = 0 'Reset Variable for next loop End If Next wrdLp Sht.Cells(CommentLp, "D") = Mid(Sht.Cells(CommentLp, "D"), 3, Len(Sht.Cells(CommentLp, "D")) - 2) Next CommentLp End Sub
BEFORE:
AFTER:
Matt Mickle
Bronze Contributor
Alex-
Maybe try using this macro. Since I cannot post a macro enabled workbook due to security risks. You'll need to insert this code in a standard code module for it to work:
VBA:
Sub GetWords() Dim wrdLRow As Integer Dim wrdLp As Integer Dim CommentLrow As Integer Dim CommentLp As Integer Dim fndWord As Integer Dim Sht As Worksheet On Error Resume Next 'Suppress Errors... for when we don't find a match 'Define worksheet that has data on it.... Set Sht = Sheets("Sheet1") 'Get last row for words based on column A wrdLRow = Sht.Cells(Rows.Count, "A").End(xlUp).Row 'Get last row for comments based on column C CommentLrow = Sht.Cells(Rows.Count, "C").End(xlUp).Row 'Loop through lists and find matches.... For CommentLp = 2 To CommentLrow For wrdLp = 2 To wrdLRow 'Look for word... fndWord = Application.WorksheetFunction.Search(Sht.Cells(wrdLp, "A"), Sht.Cells(CommentLp, "C")) 'If we found the word....then If fndWord > 0 Then Sht.Cells(CommentLp, "D") = Sht.Cells(CommentLp, "D") & "; " & Sht.Cells(wrdLp, "A") fndWord = 0 'Reset Variable for next loop End If Next wrdLp Sht.Cells(CommentLp, "D") = Mid(Sht.Cells(CommentLp, "D"), 3, Len(Sht.Cells(CommentLp, "D")) - 2) Next CommentLp End Sub
BEFORE:
AFTER:
kp1234
Dec 19, 2019Copper Contributor
hi, its working great, but there's one prob with it...
suppose the string in col C is "How can i make excel to search word against a list of words?"
and the words to look in col A is "how, ke, cel, abcd, xyz"
then this will return the "how; ke; cel;",
but i want it to only return "how" not the char "ke;cel;" inside the words "make;excel"
how to modify this script, so, it returns only words if it contains space either before of after the word, so it can be guessed as the whole word insted of char.
suppose the string in col C is "How can i make excel to search word against a list of words?"
and the words to look in col A is "how, ke, cel, abcd, xyz"
then this will return the "how; ke; cel;",
but i want it to only return "how" not the char "ke;cel;" inside the words "make;excel"
how to modify this script, so, it returns only words if it contains space either before of after the word, so it can be guessed as the whole word insted of char.