Sub Run_Search()
Dim sLast, kLast As Long
Dim sRange, kRange, ktRange, finddatacolumn As Range
Dim matchedwords, temp, ktextstr As String
Dim res, intMatches, i, x, y, datacolumn As Integer
Dim tempArr, ktArr() As String
Dim scnt, areacnt As Integer
Dim colorArr() As String
Dim mycolor, thiscolor As String
mycolor = "255,16711680,16746752,16747007,35072,16771707,48383,48300,8406527,16762537"
colorArr = Split(mycolor, ",")
i = -1
x = 0
y = -1
temp = ""
tempArr = ""
areacnt = 0
Set finddatacolumn = Worksheets("Data").Range("A1:Z1").Find(What:="Client Complaint", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
datacolumn = finddatacolumn.Column
sLast = Worksheets("data").Cells(Cells.Rows.Count, datacolumn).End(xlUp).Row
kLast = Worksheets("keywords").Cells(Cells.Rows.Count, "A").End(xlUp).Row
Worksheets("data").Range("I2:I10000").ClearContents
Worksheets("data").Range("D2:D10000").ClearContents
'Worksheets("data").Range("C2:C10000").ClearContents
'Set sRange = Worksheets("data").Range("A2:A" & sLast)
Set sRange = Worksheets("data").Range(Cells(2, datacolumn).Address, Cells(sLast, datacolumn))
Set kRange = Worksheets("keywords").Range("A2:A" & kLast)
For Each Item In kRange.Offset(0, 1)
temp = Item.Value
If InStr(tempArr, temp) = 0 Then
If Len(tempArr) > 0 Then
tempArr = tempArr + "||" + temp
Else
'MsgBox temp & " " & Item.Offset(0, -1)
If Len(temp) > 0 Then
tempArr = temp
Else
tempArr = tempArr + "||" + "Empty"
End If
End If
ElseIf Len(temp) = 0 Then
tempArr = tempArr + "||" + "Empty"
End If
Next Item
'MsgBox tempArr
x = 1
If Len(tempArr) > 0 Then
ktArr = Split(tempArr, "||")
If UBound(ktArr) <= 10 Then
For Each hh In ktArr
i = i + 1
ktArr(i) = hh + "||" + colorArr(i)
Worksheets("data").Cells(x + i + 1, datacolumn + 7).Value = hh
Worksheets("data").Cells(x + i + 1, datacolumn + 7).Font.Color = colorArr(i)
Next hh
i = 0
x = 0
Else
MsgBox ("You have more than 10 different types of Loan terms on the Keywords sheet, You can only have up to 10 different term types, Change Term Types and try again")
Exit Sub
End If
End If
'MsgBox Join(ktArr)
For Each stxt In sRange
stxt.Font.Color = vbBlack
stxt.Offset(0, 2).Value = ""
stxt.Offset(0, 3).Value = ""
If Len(stxt.Value) > 0 Then
matchedwords = ""
matchedareas = ""
y = -1
For Each ktxt In kRange
res = InStr(LCase(stxt), LCase(ktxt))
If res > 0 Then
intMatches = intMatches + 1
If Len(Join(ktArr)) > 0 Then
For Each x In ktArr
If InStr(ktxt.Offset(0, 1).Value, Split(x, "||")(0)) > 0 Or Len(ktxt.Offset(0, 1).Value) = 0 Then
y = y + 1
thiscolor = Split(x, "||")(1)
Exit For
'Else
' thiscolor = Split(x, "||")(1)
' MsgBox x
' thiscolor = colorArr(9)
End If
Next x
Else
thiscolor = colorArr(9)
End If
'MsgBox thiscolor
done = highlight(stxt.Value, ktxt.Value, stxt.Cells, CLng(thiscolor))
matchedwords = matchedwords & ktxt.Value & " (" & Str(done) & "), "
If InStr(matchedareas, ktxt.Offset(0, 2).Value & " / ") = 0 Then
areacnt = areacnt + 1
matchedareas = matchedareas & ktxt.Offset(0, 2).Value & " / "
End If
stxt.Offset(0, 2).Value = matchedwords
stxt.Offset(0, 3).Value = matchedareas
stxt.Offset(0, 4).Value = intMatches
End If
Next ktxt
If areacnt > 0 Then
temptxt = stxt.Offset(0, 3).Value
stxt.Offset(0, 3).Value = Left(temptxt, (Len(temptxt) - 3))
End If
areacnt = 0
End If
intMatches = 0
Next stxt
MsgBox "Done"
Set sRange = Nothing
Set kRange = Nothing
End Sub
Function highlight(stxt As String, ftxt As String, sCell As Range, thiscolor As Long)
Dim x As Integer
Dim ptr As Integer
Dim found As Integer
found = 0
x = 0
stxt = sCell.Value
ptr = InStr(LCase(stxt), LCase(ftxt))
Do While ptr > 0
With sCell.Characters(ptr, Len(ftxt))
.Font.Color = thiscolor
End With
found = found + 1
ptr = InStr(ptr + Len(ftxt), LCase(stxt), LCase(ftxt))
Loop
highlight = found
End Function