I have written code to highlight any word found in a cell from a list. eg. Look for the word "BUBBLE" in a search range and if found then change the color of "BUBBLE" to vbRed and put the number 1 in the next cell over. I then go through another list to check for false positives, eg. Look for "NO BUBBLE" in the same search range. If "NO BUBBLE" is found then change the color of "NO BUBBLE" to vbBlack.
After going through the false positives list I then run a sub routine call FalsePositives to check if the cell color in the search range is vbBlack because I don't want them displayed. Since in the above example "BUBBLE" which was vbRed has now been changed to vbBlack when "NO BUBBLE" was found I just go back through the cells and check to see if they are vbBlack now and then remove the 1's from the offset cells. When I physically look at the cell it is indeed vbBlack - RGB(0,0,0) but according to VB is it not so the offset values are not changed to "". I have tried to check the color of the cell by using font.color but shows blank rather than 0, which is what shows up for a cell that has not been changed. It seems to me that this is a bug in VB, but maybe my logic is just wrong. I am hoping someone can recommend a better way.
After going through the false positives list I then run a sub routine call FalsePositives to check if the cell color in the search range is vbBlack because I don't want them displayed. Since in the above example "BUBBLE" which was vbRed has now been changed to vbBlack when "NO BUBBLE" was found I just go back through the cells and check to see if they are vbBlack now and then remove the 1's from the offset cells. When I physically look at the cell it is indeed vbBlack - RGB(0,0,0) but according to VB is it not so the offset values are not changed to "". I have tried to check the color of the cell by using font.color but shows blank rather than 0, which is what shows up for a cell that has not been changed. It seems to me that this is a bug in VB, but maybe my logic is just wrong. I am hoping someone can recommend a better way.
Code:
'HIGH search valuesFndRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
Range("A2").Select
For x = 1 To FndRows 'Go through the list of values to search
oStrg = ActiveCell.Value
On Error Resume Next
For Each cl In SrchRng
If InStr(1, cl.Value, oStrg, vbTextCompare) > 0 Then
cl.Offset(0, 1).Value = 1 'Mark it as found
cl.Offset(0, 2).Value = 1 'Mark it as HIGH
End If
fndTxt = InStr(1, cl, oStrg, vbTextCompare)
Do Until fndTxt = 0
With cl.Characters(fndTxt, Len(oStrg))
.Font.Color = vbRed 'RGB(255, 0, 0)
.Font.Bold = True
End With
fndTxt = InStr(fndTxt + 1, cl, oStrg, vbTextCompare)
Loop
Next cl
ActiveCell.Offset(1, 0).Select
Next
'Turn off highlighted search values that are false positives
FndRows = Range("E2", Range("E2").End(xlDown)).Rows.Count
Range("E2").Select
For x = 1 To FndRows 'Go through the list of values to search
oStrg = ActiveCell.Value
On Error Resume Next
For Each cl In SrchRng
If InStr(1, cl.Value, oStrg, vbTextCompare) > 0 Then 'Found a false positive
cl.Offset(0, 6).Value = 1 'Mark false positive found
fndTxt = InStr(1, cl, oStrg, vbTextCompare)
Do Until fndTxt = 0
With cl.Characters(fndTxt, Len(oStrg))
.Font.Color = vbBlack
.Font.Bold = False
End With
fndTxt = InStr(fndTxt + 1, cl, oStrg, vbTextCompare)
Loop
End If
Next cl
ActiveCell.Offset(1, 0).Select
Next
Call FalsePositives
Sub FalsePositives()
ws.Target.Activate
' Turn off false positive cells - they only have black font color.
FndRows = Range("F5", Range("F5").End(xlDown)).Rows.Count
Range("F5").Select
For x = 1 To FndRows 'Go through the list of values to search
If ActiveCell.Offset(0, 6).Value = 1 Then 'Highlighted as having a false positive
If ActiveCell.Font.Color = vbBlack Then
'ActiveCell.Offset(0, 1).Value = ""
'ActiveCell.Offset(0, 2).Value = ""
End If
End If
ActiveCell.Offset(1, 0).Select
Next
End Sub
Last edited by a moderator: