Highlight found text string in cell?

E

Ed

Having gotten my "find this" macro going (big thanks to Chip Pearson!), I'm
now wondering if it is possible to highlight the text string when it's
found. I use an InputBox to get a text string, then use the string in
Set rngFound = Selection.Find(What:=MyTarget, _
MatchCase:=False)

I'm searching through a spreadsheet of test report information, such as
title, grouping, condition, subject, failures and modes, and so forth.
rngFound is set to columns A-U of one row at a time. The text string can be
any where in any cell in that range; if not, the entire row is hidden. If I
want to search for all reports on the "start" of various test procedures,
I'll not only get "Start of Cold Test", but "Starter Failed", and so forth.
If there was some way I could highlight my text string - maybe turn the
letters a different color or bold them or something - I could at a glance
tell if the string was found in a parameter meaningful to my search
(something I can't necessarily code in without a lot of extra stuff and
crossed-finger hoping the reports were written correctly!).

Is this easily possible?

Ed
 
V

Vasant Nanavati

Untested, but try:

If Not rngFound Is Nothing Then
rngFound.Characters(WorksheetFunction.Search(MyTarget, Range("A1")),
Len(MyTarget)).Font.Color = vbRed
 
T

Tom Ogilvy

Sub AATester10()
Dim rngFound As Range
Dim MyTarget As String
MyTarget = "brown"
Set rngFound = Selection.Find(What:=MyTarget, _
MatchCase:=False)
If Not rngFound Is Nothing Then
iloc = InStr(1, rngFound, MyTarget, vbTextCompare)
rngFound.Characters(iloc, Len(MyTarget)).Font.Bold = True
End If
End Sub
 
E

Ed

Thank you, Tom!
Ed

Tom Ogilvy said:
Sub AATester10()
Dim rngFound As Range
Dim MyTarget As String
MyTarget = "brown"
Set rngFound = Selection.Find(What:=MyTarget, _
MatchCase:=False)
If Not rngFound Is Nothing Then
iloc = InStr(1, rngFound, MyTarget, vbTextCompare)
rngFound.Characters(iloc, Len(MyTarget)).Font.Bold = True
End If
End Sub


--
Regards,
Tom Ogilvy




can If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top