Forum Discussion
Formula does not work for all
- May 25, 2018
Try using the below VBA Code to get the result you would like. I'm attaching a .xlsx workbook as an example since it's not possible to attach macro enabled workbooks. You will need to insert the code I have provided below into the workbook to see how it works. Once you grasp the concept you can alter the code to work for your needs. Hope this helps.
Before:
After:
Code:
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
Thank you Matt for your reply.
Unfortunately I am unable to share any data with you.
I am doing a sentimental analysis and trying to pick up positive or negative words and than score each words. I am unable to find a solution that will help me do this in excel.
Thanks
Try using the below VBA Code to get the result you would like. I'm attaching a .xlsx workbook as an example since it's not possible to attach macro enabled workbooks. You will need to insert the code I have provided below into the workbook to see how it works. Once you grasp the concept you can alter the code to work for your needs. Hope this helps.
Before:
After:
Code:
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
- Student198615May 14, 2021Copper Contributor
Matt Mickle Is it possible to extract the whole sentence containing word listed in column A rather than just a word.
- Nruti DesaiMay 25, 2018Copper Contributor
Thank you so much Matt.
It worked.
Thanks.