ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Finding string and color the found string (https://www.excelbanter.com/excel-programming/373348-finding-string-color-found-string.html)

Agustus

Finding string and color the found string
 
Hi all,

I tried to find a string in selected cells with the following code with
no success:

Sub Findstr()
Dim myStr As String

myStr = InputBox("Enter word to be searched")
If myStr = "" Then Exit Sub

With Selection.Characters(Start:=InStr(1, ActiveCell.Value, myStr), _
Length:=Len(myStr)).Font
.FontStyle = "Bold"
.ColorIndex = 3
End With
End Sub

Any advice on improving the code is much appreciated.

TIA.


Tom Ogilvy

Finding string and color the found string
 
It worked for me if the string was in the activecell and only one cell was
selected and the substring matched exactly (including case). If not, it
worked on the first n characters in the string where n was the length of the
string being searched for.


Sub Findstr()
Dim myStr As String
Dim iloc as Long
myStr = InputBox("Enter word to be searched")
If myStr = "" Then Exit Sub

activeCell.Select
iloc = Instr(1,ActiveCell.Value,myStr,vbTextCompare)
if iloc < 0 then
With Selection.Characters(Start:=iloc, _
Length:=Len(myStr)).Font
.FontStyle = "Bold"
.ColorIndex = 3
End With
Else
msgbox mystr & " was not found"
End if
End Sub

--
Regards,
Tom Ogilvy


"Agustus" wrote:

Hi all,

I tried to find a string in selected cells with the following code with
no success:

Sub Findstr()
Dim myStr As String

myStr = InputBox("Enter word to be searched")
If myStr = "" Then Exit Sub

With Selection.Characters(Start:=InStr(1, ActiveCell.Value, myStr), _
Length:=Len(myStr)).Font
.FontStyle = "Bold"
.ColorIndex = 3
End With
End Sub

Any advice on improving the code is much appreciated.

TIA.




All times are GMT +1. The time now is 11:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com