Use find function while looping.

vbaNewby

Board Regular
Joined
Jan 26, 2011
Messages
138
Hello guru's,

Currently I have something to this sort:

Code:
 Set FoundCell = Cells.Find(what:=myCell, After:=ActiveCell, LookIn:=xlFormulas _
                , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=True)
                If FoundCell Is Nothing Then
                     'move on, do more stuff
                End If
                If Not FoundCell Is Nothing Then
                       'set my range copy and paste
I would like to modify this to loop through the spreadsheet and keep finding myCell and then in turn set my range / copy / paste until nothing else is found in the sheet.

Can someone provide me with an example to do so???

Thanks in advance!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
From VBA Help:

Code:
With Worksheets(1).Range("a1:a500")
    Set c = .Find(2, lookin:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Interior.Pattern = xlPatternGray50
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With
 
Upvote 0
From VBA Help:

Code:
With Worksheets(1).Range("a1:a500")
    Set c = .Find(2, lookin:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Interior.Pattern = xlPatternGray50
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With

Hi Gary, thanks for the reply. What does the "c" variable represent? My range?
 
Upvote 0
It's a range variable.

Code:
Dim C as Range
It's pretty much what you had except it also uses "FindNext" and remembers the address of the first cell it found. Then it keeps using FindNext until it rolls around to the first cell found again.

I guess C is the same thing you called "FoundCell"

Gary
 
Last edited:
Upvote 0
It's a range variable.

Code:
Dim C as Range
It's pretty much what you had except it also uses "FindNext" and remembers the address of the first cell it found. Then it keeps using FindNext until it rolls around to the first cell found again.

I guess C is the same thing you called "FoundCell"

Gary

Yes, I get the logic it was using. I was select a different sheet in the middle of the loop so I had to set it back once I was done with processing.

Thanks for the tip!
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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