Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default 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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default 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.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
line input problem-need to return 5 lines after a string is found festdaddy Excel Programming 3 December 8th 05 04:38 PM
Search for a test string and if found insert 'x' in clumn 'A' TCL Excel Discussion (Misc queries) 2 September 21st 05 05:18 PM
Search column - return row found in long text string JayL Excel Programming 1 March 5th 05 03:23 AM
get linenumber when string found in VBE codemodule RB Smissaert Excel Programming 11 October 10th 04 09:56 AM
Highlight found text string in cell? Ed[_18_] Excel Programming 4 June 4th 04 03:07 PM


All times are GMT +1. The time now is 05:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"