Question: vba Range.FindNext v Range.Find - what have I missed ?
Hi
( I am still not too familiar with posting here, so apologies if I have anything not quite right )
I’ve been doing a few VBA codes that are all variations of searching down a long column. (Often it involves looking for many occurrences consecutively, either to produce a list of them all or to select one or more ( when doing a LookAt Part type option thingy) )
I am not a computer or coding professional, and don’t have so much experience, so what have I missed? …
I was expecting some advantage from the .FindNext compared with using the .Find when doing the sort of thing that I have been doing.
Take a simplified example where I want to find the two words with rOh in them
/ A B C
1
2
3 rOh3
4
5
6
7 rOh7
8
9
10
So I want the Debug.Print output of
rOh3
rOh7
This is typical of the sort of code I have seen suggested, that is to say what I find over the internet and in tutorials:
Sub VBAFindNext()
Dim FirstrngFnd As Range, rngFnd As Range
Set FirstrngFnd = Range("B1:B10").Find(What:="roh", LookAt:=xlPart)
If FirstrngFnd Is Nothing Then Exit Sub
Set rngFnd = FirstrngFnd
Debug.Print FirstrngFnd.Value
Do
Set rngFnd = Range("B1:B10").FindNext(rngFnd)
If Not rngFnd = FirstrngFnd Then Debug.Print rngFnd.Value
Loop While Not rngFnd = FirstrngFnd
End Sub
I am doing a code like this, which seems a bit simpler and more flexible…
Sub FindTheNext()
Dim rngFnd As Range
Set rngFnd = Range("B1:B10").Find(What:="roh", LookAt:=xlPart)
If rngFnd Is Nothing Then Exit Sub
Do While Not rngFnd Is Nothing
Debug.Print rngFnd.Value
Set rngFnd = Range("B" & rngFnd.Row + 1 & ":B10").Find(What:="roh", LookAt:=xlPart)
Loop
End Sub
So I was just wondering if I have missed anything? I could write the latter code as this, but I don’t see any improvement
Sub TheNextVBAFindNext()
Dim rngFnd As Range
Set rngFnd = Range("B1:B10").Find(What:="roh", LookAt:=xlPart)
If rngFnd Is Nothing Then Exit Sub
Do While Not rngFnd Is Nothing
Debug.Print rngFnd.Value
Set rngFnd = Range("B" & rngFnd.Row + 1 & ":B10").FindNext(rngFnd.Offset(1, 0))
Loop
End Sub
So the question: I am hoping someone with more experience or someone that understands more deeply into the workings of these things can explain any reasons that might make the first code preferable. I may be missing something important in my ignorance. I suppose the specific question is “What is the point of .FindNext”. Is it that it just saves typing the search criteria again, or is there more to it than that. I was thinking, possibly naively , that for a very long column my code might be a bit better as it looks each time at a shortened range, … but whether that is true will depend I guess on exactly what is going on “behind the scenes”, which I don’t know. Does anyone else know that?, and can they explain it in simple terms if possible.
Thanks
Alan
P.s. I have read that some people think that FindNext is broken. But I have not seen any hard details to prove that yet.
https://msdn.microsoft.com/de-de/vba/excel-vba/articles/range-findnext-method-excel https://msdn.microsoft.com/de-de/vba/excel-vba/articles/range-find-method-excel .FindNext failing after a .Find function (excel vba)
The actual codes I have are typically a lot more complicated, that is why I am trying to understand a bit more of what is going on: https://www.excelforum.com/excel-programming-vba-macros/1186516-smarter-search-process.html#post4664009