VBA Find failure

Guraknugen

New Member
Joined
Mar 15, 2017
Messages
36
Test code:
Code:
Option Explicit

Sub SearchTest()
    Dim Search As Range
    Dim MyRange As Range
    
    Set MyRange = Worksheets(1).Range("A1:A8")
    
    Set Search = MyRange.Find(What:="0", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="00", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="000", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="0000", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="1", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="11", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="111", LookIn:=xlValues)
    Debug.Print Search.Row
    Set Search = MyRange.Find(What:="1111", LookIn:=xlValues)
    Debug.Print Search.Row
End Sub
Type the following in the following cells:
Code:
A1: '0
A2: '00
A3: '000
A4: '0000
A5: '1
A6: '11
A7: '111
A8: '1111
Expected result (?) in the monitor window:
Code:
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8
Result in the monitor window:
Code:
 2 
 2 
 3 
 4 
 5 
 6 
 7 
 8

Why is the first line in the monitor 2 instead of 1, and what can I do about it?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Upvote 0
Thank you for replying.
See the documentation here:

https://msdn.microsoft.com/VBA/Excel-VBA/articles/range-find-method-excel

Specifically, the "After" parameter:



You can get your required output like this:

Code:
    Set Search = MyRange.Find(What:="0", LookIn:=xlValues, After:=MyRange(MyRange.Count))

etc.

WBD

Thanks, that was it.
I actually read about the After parameter on that very page, but I completely misunderstood it for some reason.
At least I found a clue to it, because I discovered that if I move all the cells one step down and search A1:A9, it also worked.

It still doesn't work when the list isn't sorted, but I found that one out myself:
Code:
LookAt:=xlWhole
Because otherwise, when searching for "0", it stops if it first finds "00", "000" or "0000".

Thank you for your help!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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