Error reading range

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,120
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
VBA Code:
Look = xlComments
SearchPart = xlPart
rCol= "C2:C58"
Set Found = Range(rCol).Find(What:=TextBox1.Text, lookIn:=Look, Lookat:=SearchPart)
If Not Found Is Nothing Then
    Set FirstFound = Found
    Do
        Debug.Print Found, Found.Address
        Set Found = Range(rCol).FindNext(After:=Found)
    Loop Until FirstFound.Address = Found.Address
End If
The last address it prints is C59. That's one more than the range should have and I get error
Run-time error '1004':
Unable to get the FindNext property of the Range class


But if rCol is "C:C" there is no error.
Is how I'm defining the rows or range wrong?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Change this line:

VBA Code:
Loop Until FirstFound.Address = Found.Address

For this:

VBA Code:
Loop Until FirstFound.Address = Found.Address Or Intersect(Range(rCol), Found) Is Nothing
 
Upvote 0
Solution
Or Intersect(Range(rCol), Found) Is Nothing
Doing more tests I found that this applies only when you use xlComments in the Lookin parameter. Because when you use xlvalues it is not necessary.

🧙‍♂️
 
Upvote 0
I found that this applies only when you use xlComments in the Lookin parameter
Probably also applies to xlCommentsThreaded (I haven't tested it yet and is only in later versions of Excel). xlFormulas you also don't need it with.
 
Upvote 0
Thanks Dante, very glad to have a solution. I only just found you can search comments like this and for what I'm doing it's brilliant.
LookIn can alternate between xlValues and xlComments and your fix handles both.
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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