Understanding Find in VBA

Mindpsyche

Well-known Member
Joined
Mar 19, 2012
Messages
760
Hello,I have been trying to understand how to use the find in vba (ctrl+f). I have read up that it is very versatile and can be used instead of loops in many cases.Anyway, this is the code from the 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.Value = 5            Set c = .FindNext(c)        Loop While Not c Is Nothing And c.Address <> firstAddress    End IfEnd With[\CODE]The part i dont understand is Loop while Not c is nothing And c.address <> firstaddress Does c.address not change throughout the entire find procedure? i.e. is the c.address the address of the first cell where the value 2 is found?If anyone can throw some light on this, and also if you have any nice links which give a clearer understanding of Find, i would be grateful. (For example: I have seen an example of someone find to lookup the font.bold of a cell, but i can't find the link)...Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
:
:
i.e. is the c.address the address of the first cell where the value 2 is found?If anyone can throw some light on this, and also if you have any nice links which give a clearer understanding of Find, i would be grateful. (For example: I have seen an example of someone find to lookup the font.bold of a cell, but i can't find the link)...Thanks
You are correct about the c.address part. The Do...while loop construct needs to stop at a definite point / condition.

This happens with normal CTRL + F as well. If you keep on hitting "Find Next" then it comes to the first address again. So in VBA, this will mean infinite loop if we didn't define the exit / stop condition for the loop.

Try to google : Examples of .FIND method VBA and you will end up with threads like this.
 
Upvote 0
You are correct about the c.address part. The Do...while loop construct needs to stop at a definite point / condition.

This happens with normal CTRL + F as well. If you keep on hitting "Find Next" then it comes to the first address again. So in VBA, this will mean infinite loop if we didn't define the exit / stop condition for the loop.

Try to google : Examples of .FIND method VBA and you will end up with threads like this.


Thanks man.
 
Upvote 0

Forum statistics

Threads
1,203,534
Messages
6,055,947
Members
444,839
Latest member
laurajames

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