Find and FindNext

abenitez77

Board Regular
Joined
Dec 30, 2004
Messages
149
When I use Find and FindNext, it is looping around the same 2 values it finds.

This is the code I use to search for "X" and when I use FindNext to keep searching, it finds the second "X". Then on the next FindNext it comes back to the first "X" I found. How do I make it stop at the last one that exists?


'Search for "X" markers **************************************************************************************
mySearch5 = "X"
Set iSheetE = Range("A44:O54").Find(What:=mySearch5, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not iSheetE Is Nothing Then
strBanner = iSheetE.Offset(0, 1).Value
strBannerCode = iSheetE.Offset(0, 2).Value
Else
GoTo NoMoreX
End If

'Search Next X
Set iSheetE2 = .FindNext(iSheetE)
If Not iSheetE2 Is Nothing Then
strBanner2 = iSheetE2.Offset(0, 1).Value
strBannerCode2 = iSheetE2.Offset(0, 2).Value
Else
GoTo NoMoreX
End If

'Search Next X
Set iSheetE3 = .FindNext(iSheetE2)
If Not iSheetE3 Is Nothing Then
strBanner3 = iSheetE3.Offset(0, 1).Value
strBannerCode3 = iSheetE3.Offset(0, 2).Value
Else
GoTo NoMoreX
End If

Set iSheetE4 = FindNext(iSheetE3)
If Not iSheetE4 Is Nothing Then
strBanner4 = iSheetE4.Offset(0, 1).Value
strBannerCode4 = iSheetE.Offset(0, 2).Value
Else
GoTo NoMoreX
End If

Set iSheetE5 = .FindNext(iSheetE4)
If Not iSheetE5 Is Nothing Then
strBanner5 = iSheetE5.Offset(0, 1).Value
strBannerCode5 = iSheetE5.Offset(0, 2).Value
Else
GoTo NoMoreX
End If

....
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You need to put the address of the first cell found find in a string variable, and test that in each loop iteration. When you find it again, you're done.
 
Upvote 0
you mean like this below? Also, this time I only have 2 "X" in my range, but there could be a random number of "X" in my range (no more than about 10).

mySearch5 = "X"
Set iSheetE = Range("A44:O54").Find(What:=mySearch5, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not iSheetE Is Nothing Then
strBanner = iSheetE.Offset(0, 1).Value
strBannerCode = iSheetE.Offset(0, 2).Value
myaddr = iSheetE.Address
Else
GoTo NoMoreX
End If

'Search Next X
Set iSheetE2 = .FindNext(myAddr)
If Not iSheetE2 Is Nothing Then
strBanner2 = iSheetE2.Offset(0, 1).Value
strBannerCode2 = iSheetE2.Offset(0, 2).Value
Else
GoTo NoMoreX
End If
 
Upvote 0
Code:
Sub ab()
  Dim rFind         As Range
  Dim sAddr         As String

  With Range("A44:O54")
    Set rFind = .Find(What:="X", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
    If Not rFind Is Nothing Then
      sAddr = rFind.Address
      Do
        ' anything that doesn't clobber the value in rFind
        Set rFind = .FindNext(rFind)
      Loop While rFind.Address <> sAddr
    End If
  End With
End Sub
 
Upvote 0
You're welcome.

I'm more likely to respond to your next question if you put your code in CODE tags.

Code:
your code here
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top