Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default VBA - Find every occurance of string in workbook, write to 1st she



Private Sub cmdSearch_Click()

Dim rng As Range, sh As Worksheet
Dim r, c As Integer
Dim Found As Boolean
Dim firstAddress As String
Dim strWhatToFind As String

c = 4
r = 15
Found = False
Reenter:
strWhatToFind = Application.InputBox("Enter a word or phrase...",
"Search")
If strWhatToFind = "" Then
Response = MsgBox("Please enter search criteria.", vbOKOnly, "Oops!")
GoTo Reenter
End If
If strWhatToFind < "False" Then
For Each sh In ThisWorkbook.Worksheets
If sh.Name < ThisWorkbook.Worksheets(1).Name Then

Set rng = sh.Cells.Find(What:=WhatToFind, _
After:=Range("IV65536"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False), _
SearchFormat:=False)
If Not rng Is Nothing Then
firstAddress = rng.Address
Do
Worksheets(1).Cells(r, c).Value = _
rng.Value
r = r + 1
Set rng = sh.Cells.FindNext(rng)
Loop While Not rng Is Nothing And _
rng.Address < firstAddress
Found = True
End If
End If
Next
End If
Worksheets(1).Activate
If Not Found Then
MsgBox ("Customer Data not found")
Else
MsgBox ("Done")
End If

End Sub

--
Regards,
Tom Ogilvy

"Doug" wrote in message
...
Hi,

I'm trying to seach all worksheets in a workbook [except the first
worksheet] for a string that is supplied from a inputbox on the first
worksheet.

The results of the search should be written to rows on the first worksheet
with links to the matched occurance [like the FindAll in the Find Dialog

box].

I have been working toward the solution - here is my code so far...
------------------------
Private Sub cmdSearch_Click()

Dim rng As Range, sh As Worksheet
Dim r, c As Integer
Dim Found As Boolean
Dim firstAddress As Variant
Dim strWhatToFind As String

c = 4
r = 15
Reenter:
strWhatToFind = Application.InputBox("Enter a word or phrase...",

"Search")
If strWhatToFind = "" Then
Response = MsgBox("Please enter search criteria.", vbOKOnly, "Oops!")
GoTo Reenter
End If
If strWhatToFind < "False" Then
For Each sh In ThisWorkbook.Worksheets
sh.Activate
Set rng = Cells.Find(What:=WhatToFind, _
After:=Range("IV65536"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not rng Is Nothing Then
firstAddress = rng.Address
Do
Worksheets("Title").Cells(r, c).Value =

sh.Range(rng.Address).Value
r = r + 1
Loop While Not rng Is Nothing And rng.Address < firstAddress
Found = True
End If
Next
Worksheets(1).Activate
If Not Found Then
MsgBox ("Customer Data not found")
Else
MsgBox ("Done")
End If
End If
End Sub
---------------
Seems this finds occurances only on worksheet 1, won't find any on other
sheets - and only finds 1 per sheet.

Can someone help?



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
Count occurance of a leter in a text string justaman Excel Worksheet Functions 3 October 11th 08 04:51 AM
find a string of nth occurance & pick next 3 words Eddy Stan Excel Worksheet Functions 7 June 3rd 08 08:44 PM
help to find a string for 4th occurance Eddy Stan Excel Worksheet Functions 1 September 22nd 07 11:13 PM
Find last occurance of character in text string JDay01 Excel Worksheet Functions 2 February 14th 06 04:29 PM
Occurance Number Within a String JohnnyBGood Excel Discussion (Misc queries) 4 December 20th 05 04:59 PM


All times are GMT +1. The time now is 06:44 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"