Find method issue

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
Can someone please tell me why I get an error on the fourth line of the following:
Code:
Dim name As String
Dim Loc As Range
name = ActiveCell.Offset(Range("A4").Value, 0)
Set Loc = Range("A63500:A65500").Find(What:=name, LookIn:=xlValues)
ActiveCell.Value = Loc.Offset(0, 4).Value
Thank you.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
This code caters for your "name" variable not being found

If you do expect it to be in the range then ensure you are matching part strings rather than whole strings if necessary, check for inconsistent whitespaces in the search or result strings etc

Cheers

Dave


Code:
  Dim name As String
    Dim Loc As Range
    name = ActiveCell.Offset(Range("A4").Value, 0)
    Set Loc = Range("A63500:A65500").Find(What:=name, LookIn:=xlValues)
    If Not Loc Is Nothing Then
        ActiveCell.Value = Loc.Offset(0, 4).Value
    Else
        MsgBox "value not found"
    End If
 
Upvote 0
You are correct...the issue was the value not being found...I need to make sure this is covered.
Thank you!
 
Upvote 0
You didn't say what the error was but I'd guess that it's because the data is not found. You need to include some error handling in case the value is not located by the Find method e.g.

Code:
Dim name As String
Dim Loc As Range
name = ActiveCell.Offset(Range("A4").Value, 0)

On Error Resume Next
Set Loc = Range("A63500:A65500").Find(What:=name, LookIn:=xlValues)
On Error GoTo 0

If Loc Is Nothing Then
    MsgBox "Data not found"
Else
    ActiveCell.Value = Loc.Offset(0, 4).Value
End If

HTH
DK
 
Upvote 0
Yes; in this case it will not be a message box since I am just processing big chunks of data, but an error handler is definately in order. Thank you!
 
Upvote 0
I'm not seeing how your code is Compileing:

name = ActiveCell.Offset(Range("A4").Value, 0)

Throws a "Type Mismatch" for me.

From Help:

expression.Offset(RowOffset, ColumnOffset)
 
Upvote 0
Range("A4") is the row value and this part of the code is OK. It was an issue of not finding the value in the list. Problem is now solved. Thank you.
 
Upvote 0
The active cell is what I am trying to populate. In a cell directly above (the number of rows is the value found in A4) is a string. This string then become the variable "name". I want to hunt for "name" in range "A63500:A6550" which will be at the range called "Loc" and then return a value that is 4 cells over from "Loc". Works now.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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