Find fails with variable in range

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
Can anyone tell me why the following fails? I am trying to improve efficiency by replacing part of the 'Find' range with the variable lRow, but it fails unless I use a specified range.

Code:
Dim name As String
Dim Loc As Range
Dim x As Integer
Dim lRow As Integer
On Error Resume Next
x = 0
lRow = 61501
Do Until x = 251
name = ActiveCell.Offset((addRows + 1) * -1, 0).Value 
Set Loc = Range("A" & lRow - 5  & ":A65500").Find(What:=name, LookIn:=xlValues, Lookat:=xlWhole)
ActiveCell.Value = Loc.Offset(0, cCol).Value
x = x + 1
lRow = Loc.Row
ActiveCell.Offset(0, 1).Select
If I change the range back to "A61500:A65500" it works just fine.
Thanks,
David
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hello All,
Can anyone tell me why the following fails? I am trying to improve efficiency by replacing part of the 'Find' range with the variable lRow, but it fails unless I use a specified range.

Code:
Dim name As String
Dim Loc As Range
Dim x As Integer
Dim lRow As Integer
On Error Resume Next
x = 0
lRow = 61501
Do Until x = 251
name = ActiveCell.Offset((addRows + 1) * -1, 0).Value 
Set Loc = Range("A" & lRow - 5  & ":A65500").Find(What:=name, LookIn:=xlValues, Lookat:=xlWhole)
ActiveCell.Value = Loc.Offset(0, cCol).Value
x = x + 1
lRow = Loc.Row
ActiveCell.Offset(0, 1).Select
If I change the range back to "A61500:A65500" it works just fine.
Thanks,
David

...but lRow - 5 = 61496 ;)
 
Upvote 0
Yes, the top five will be blank...I do not think that is the problem as it fails if I use "A" &lRow & A65500" as well.
 
Upvote 0
I thru that code in a sub and it ran (except for the missing 'Loop'). Granted, I have nothing on my worksheet.
 
Upvote 0
Thanks...I tested all the parts of it and they were fine too. It should work but it does not. Oh well...I will just run it as is...it works but a bit slow.
 
Upvote 0
Found the problem: lRow has a value of about 61500...therefore it must be Dim as Long
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,151
Members
452,891
Latest member
JUSTOUTOFMYREACH

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