problem with Find next matching cell (1 Viewer)

smiler44

Registered User.
Local time
Today, 13:38
Joined
Jul 15, 2008
Messages
641
problem with Find next matching cell resolved

The code below is to find matches to the string, word.
If there is more then 1 match in a column the code works fine. If there is only 1 match then it finds this one match twice. Instead of finding the 1 match twice, how do I exit sub if there is no second match?

smiler44


On Error GoTo errorhandler ' if no match found go to errorhandler

Cells.find(what:=word).Activate

If ActiveCell.Text = word Then
MsgBox ("matchfound")
pause (1)
Cells.FindNext(After:=ActiveCell).Activate

errorhandler: Exit Sub ' no match found
End If
 
Last edited:

smiler44

Registered User.
Local time
Today, 13:38
Joined
Jul 15, 2008
Messages
641
Having done a search on the internet I found some code and have modified it to meet my needs. The original code is at
http://jimtut.blogspot.com/2008/07/visual-basic-vba-code-to-find-text-in.html

I have modified it to :-

Code:
Sub eachtab()
'searches each tab for a match
Dim word As String ' whats in sheet1 col A to find
Dim col As String
Dim counter1 As String
counter1 = 1
col = "a"
both = col + counter1
Range(both).Select
word = Sheet1.Range(both) ' the string to find
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
 
Dim searchText As String
searchText = word
 
Dim r As Range
Dim findNext As Integer
findNext = vbYes
'The firstFind and looped vars are used to determine if we've looped thru every find on this page.
Dim firstFoundCell As String
firstFoundCell = ""
Dim looped As Boolean
looped = False
 
Do While findNext = vbYes And Not looped
' Here's the search!
Set r = Cells.find(searchText, ActiveCell, xlValues, xlPart, xlByRows, xlNext, False)
If r Is Nothing Then
' Nothing was found. Exit this WHILE loop (the FOR loop will still go to the next sheet).
Exit Do
Else
r.Activate
' Check/Set the firstFoundCell and the looped variable.
If firstFoundCell = "" Then
firstFoundCell = r.Address
Else
' We've found at least one cell already. Check to see if we've looped back to the firstFoundCell.
If r.Address = firstFoundCell Then
looped = True
Else
End If
End If ' end of checking for firstFoundCell
' If we haven't looped around, prompt the user to findNext. (If we have looped, the WHILE loop will finish, causing the FOR loop to go to the next sheet.)
If Not looped Then
'would insert hypa link here?
'findNext = MsgBox("Find Next?", vbYesNo)
MsgBox ("insert hypalink")
End If
End If
Loop ' end of Do While loop
If findNext = vbYes Then
Exit Sub 'MsgBox ("No more matches found!")
End If
 
End Sub
smiler44
 
Last edited:

boblarson

Smeghead
Local time
Today, 05:38
Joined
Jan 12, 2001
Messages
32,059
Hey Smiler - It would be great if you could use the CODE tags around your code so it isn't so hard to read and it would keep the indenting (IF you are indenting at all). To do so just place the word CODE between two square brackets [ ] at the start of the code and put /CODE inside two square brackets [ ] at the end of the code.
 

smiler44

Registered User.
Local time
Today, 13:38
Joined
Jul 15, 2008
Messages
641
I have never got the hang of indenting. I can not read code well enough to be able to follow it and associate the top part of the indented code with the bottom part. Sorry I do not understand about the square brackets but should I have posted the code like this?
Code:
Sub eachtab()
'searches each tab for a match
Dim word As String ' whats in sheet1 col A to find
Dim col As String
Dim counter1 As String
counter1 = 1
col = "a"
both = col + counter1
Range(both).Select
word = Sheet1.Range(both) ' the string to find
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''


Dim searchText As String
searchText = word

Dim r As Range
Dim findNext As Integer
findNext = vbYes
'The firstFind and looped vars are used to determine if we've looped thru every find on this page.
Dim firstFoundCell As String
firstFoundCell = ""
Dim looped As Boolean
looped = False

Do While findNext = vbYes And Not looped
' Here's the search!
Set r = Cells.find(searchText, ActiveCell, xlValues, xlPart, xlByRows, xlNext, False)
If r Is Nothing Then
' Nothing was found. Exit this WHILE loop (the FOR loop will still go to the next sheet).
Exit Do
Else
r.Activate
' Check/Set the firstFoundCell and the looped variable.
If firstFoundCell = "" Then
firstFoundCell = r.Address
Else
' We've found at least one cell already. Check to see if we've looped back to the firstFoundCell.
If r.Address = firstFoundCell Then
looped = True
Else
End If
End If ' end of checking for firstFoundCell
' If we haven't looped around, prompt the user to findNext. (If we have looped, the WHILE loop will finish, causing the FOR loop to go to the next sheet.)
If Not looped Then
'would insert hypa link here?
'findNext = MsgBox("Find Next?", vbYesNo)
MsgBox ("insert hypalink")
End If
End If
Loop ' end of Do While loop
If findNext = vbYes Then
Exit Sub 'MsgBox ("No more matches found!")
End If

End Sub

[code]
 

boblarson

Smeghead
Local time
Today, 05:38
Joined
Jan 12, 2001
Messages
32,059
No, the last
Code:
 at the very end should have a / between the [ and the c in code.

like this (read the post as shown and follow its instructions):
[img]http://downloads.btabdevelopment.com/screenshots/codetag01.png[/img]

Also, I am not good at remembering to indent so I use Smart Indenter (FREE) from here:
http://www.oaltd.co.uk/Indenter/Default.htm
 

smiler44

Registered User.
Local time
Today, 13:38
Joined
Jul 15, 2008
Messages
641
Have modified the original posting of the code and can see it has put it into a "seperate" window that could be easier to follow. There will not be any indents of mine. Will try to remember in future to use the square brackets, sorry.
I'll have a look at the web site you gave me.

smiler44
 

boblarson

Smeghead
Local time
Today, 05:38
Joined
Jan 12, 2001
Messages
32,059
Have modified the original posting of the code and can see it has put it into a "seperate" window that could be easier to follow. There will not be any indents of mine. Will try to remember in future to use the square brackets, sorry.
I'll have a look at the web site you gave me.

smiler44

Thumbs up to you for doing that. It really does help make it easier to read. And definitely look into Smart Indenter as it saves me a whole lot of time worrying about indenting my code and it helps you sometimes find missing End IF's and such as it will sometimes be fairly obvious where there is a missing one when you use the tool and then look at it.

 

Users who are viewing this thread

Top Bottom