Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Finding text in a selection

What would be the most efficient way using programming code to find all
instances of a text phrase within a selected range of cells? I'm using an
Inputbox to capture the text to search for, and when the text is found, run
whatever code I want, then find the next occurance, run my code, etc. After
it's found the last occurance, stop.

I'm finding that my code wants to keep going back to the start and
continuously looping (I'm using a "For Each ItemFound in Selection" approach
unsuccessfully so far). Thanks for any suggestions!
--
Steve C
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Finding text in a selection

This procedure finds all instances of the text within a range you specify,
and adds them all to a collection, which then you can modify.

Sub FindAll()
Dim c As Range
Dim firstAddress As Variant
Dim SearchRange As Range
Dim FindThis As Variant
Dim Collection As New Collection
Dim Item As Variant

Item = ""

'Set the string you're looking for
FindThis = "Some String"

'Set your search range here
'Loops, finds all results, and adds them to a collection appropriately named
"Collection"
With ActiveSheet.Range("A:A")
Set c = .Find(FindThis, LookIn:=xlValues, MatchCase:=False)
If Not c Is Nothing Then
firstAddress = c.Address
Do
On Error Resume Next
Collection.Add c
On Error GoTo 0
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
MsgBox "Total number of search results: " & Collection.count
Else
MsgBox "The target ''" & FindThis & "'' could not be found.",
vbExclamation, "Search Result"
End If
End With

End Sub

--------
Cheers,
Anony


"Steve C" wrote:

What would be the most efficient way using programming code to find all
instances of a text phrase within a selected range of cells? I'm using an
Inputbox to capture the text to search for, and when the text is found, run
whatever code I want, then find the next occurance, run my code, etc. After
it's found the last occurance, stop.

I'm finding that my code wants to keep going back to the start and
continuously looping (I'm using a "For Each ItemFound in Selection" approach
unsuccessfully so far). Thanks for any suggestions!
--
Steve C

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Finding text in a selection

A belated thank you to both of you for your responses. They help a lot!
--
Steve C


"Anony" wrote:

This procedure finds all instances of the text within a range you specify,
and adds them all to a collection, which then you can modify.

Sub FindAll()
Dim c As Range
Dim firstAddress As Variant
Dim SearchRange As Range
Dim FindThis As Variant
Dim Collection As New Collection
Dim Item As Variant

Item = ""

'Set the string you're looking for
FindThis = "Some String"

'Set your search range here
'Loops, finds all results, and adds them to a collection appropriately named
"Collection"
With ActiveSheet.Range("A:A")
Set c = .Find(FindThis, LookIn:=xlValues, MatchCase:=False)
If Not c Is Nothing Then
firstAddress = c.Address
Do
On Error Resume Next
Collection.Add c
On Error GoTo 0
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
MsgBox "Total number of search results: " & Collection.count
Else
MsgBox "The target ''" & FindThis & "'' could not be found.",
vbExclamation, "Search Result"
End If
End With

End Sub

--------
Cheers,
Anony


"Steve C" wrote:

What would be the most efficient way using programming code to find all
instances of a text phrase within a selected range of cells? I'm using an
Inputbox to capture the text to search for, and when the text is found, run
whatever code I want, then find the next occurance, run my code, etc. After
it's found the last occurance, stop.

I'm finding that my code wants to keep going back to the start and
continuously looping (I'm using a "For Each ItemFound in Selection" approach
unsuccessfully so far). Thanks for any suggestions!
--
Steve C

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Finding text in a selection

Is your range multiple columns, multiple rows, both?

You can loop from range.row to range.row+range.rows.count and from range
..column to range.column+range.columns.count

Hope that helps some.

Peter Richardson

"Steve C" wrote:

What would be the most efficient way using programming code to find all
instances of a text phrase within a selected range of cells? I'm using an
Inputbox to capture the text to search for, and when the text is found, run
whatever code I want, then find the next occurance, run my code, etc. After
it's found the last occurance, stop.

I'm finding that my code wants to keep going back to the start and
continuously looping (I'm using a "For Each ItemFound in Selection" approach
unsuccessfully so far). Thanks for any suggestions!
--
Steve C

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
losing text from text box when pasting selection to word rallyworker Excel Discussion (Misc queries) 0 April 13th 07 08:44 AM
Finding text in a cell and returning a value based on that text [email protected] Excel Discussion (Misc queries) 5 January 10th 07 06:01 PM
Finding a date & Selection.End(xlDown) Clivey_UK Excel Programming 8 April 26th 06 06:40 PM
finding empty selection Chimanrao Excel Programming 6 November 11th 05 03:25 AM
Finding Specific Text in a Text String Peter Gundrum Excel Worksheet Functions 9 April 10th 05 07:21 PM


All times are GMT +1. The time now is 06:47 AM.

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"