First, I discourage the global use of
This will hide all errors, not just the one you expect.
Second, if you use InStr instead of Search, you are using a built-in VBA function so it may be faster, and it will not raise an error if the string is not found. So you can remove the line shown above.
In the following line of code you are referring to the wrong sheet and the wrong column, corrected in red here:
Sht2.Cells(CommentLp, "S") = Sht2.Cells(CommentLp, "S") & "; " & Sht3.Cells(wrdLp, "A")
Last, we want to remove the first semi-colon, only if there is one.
Here is the corrected and tested code:
Sub Tags()
Dim wrdLRow As Integer
Dim wrdLp As Integer
Dim CommentLrow As Integer
Dim CommentLp As Integer
Dim fndWord As Integer
' Declare sheets that we will be referencing
Dim Sht2 As Worksheet
Dim Sht3 As Worksheet
'On Error Resume Next 'Suppress Errors... for when we don't find a match
' Define worksheets that contain data
' ignore Set Sht = Sheets("Sheet1")
Set Sht2 = Sheets("Sheet2")
Set Sht3 = Sheets("Sheet3")
'Get last row for hashtags based on column A in sheet3 (aka Hastag Lookup)
wrdLRow = Sht3.Cells(Sht3.Rows.Count, "A").End(xlUp).Row
'Get last row for posts based on column J in sheet2 (aka All Merged Data)
CommentLrow = Sht2.Cells(Sht2.Rows.Count, "J").End(xlUp).Row
' Initialize
Sht2.Range("S:S").ClearContents
Sht2.Range("S1") = "Tags"
'Loop through lists and find matches....
For CommentLp = 2 To CommentLrow
For wrdLp = 2 To wrdLRow
'Look for hashtags...
fndWord = InStr(1, Sht2.Cells(CommentLp, "J"), Sht3.Cells(wrdLp, "A"))
'If we found the hashtag....then
If fndWord > 0 Then
Sht2.Cells(CommentLp, "S") = Sht2.Cells(CommentLp, "S") & "; " & Sht3.Cells(wrdLp, "A")
fndWord = 0 'Reset Variable for next loop
End If
Next wrdLp
If Left(Sht2.Cells(CommentLp, "S"), 2) = "; " Then
Sht2.Cells(CommentLp, "S") = Mid(Sht2.Cells(CommentLp, "S"), 3, Len(Sht2.Cells(CommentLp, "S")) - 2)
End If
Next CommentLp
End Sub
Bookmarks