excel vba .find not returning range

ksharpe212

New Member
Joined
Jun 26, 2011
Messages
1
I am trying to use the .find function in excel vba. I am trying to find the first occurrence of a name in a list of names.

I have declared a range variable and set up the .find function statement (with the required parameters (attributes?)) and finally gotten it to run through the code with no error.

However, if I query the range variable (?find_shaun) in the immediate window, it returns the value in the cell where it found the name being sought. It doesn't return a range.

I tried to select it and thought I could query the range variable for the row but trying to select it, I get an error: method 'Range' of object _'Global' failed. It doesn't seem to be a range reference at all, just the value of the name it was searching for.

Here is my code:

Sub find_name()
'
' macro find_name searches for a trainer name in the trainer list in table2
'
Dim a As Integer
Dim find_shaun As Range
Dim trainer_name As Range
Dim trainer As String

Sheets("Sheet1").Select
' Set find_shaun = Range("table2[trainer]")
Set trainer_name = Range("g2:g2")
Range("table2[trainer]").Select
With ActiveSheet
Set find_shaun = Selection.Find( _
What:=trainer_name, _
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
End With

Range("find_shaun").Select '<--- here is where I get the method of 'range' of object _'global' failed
a = ActiveCell.Row

End Sub

Can anyone tell me what I've done wrong or what I'm missing in the functioning of the find function.

thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

You have defined (Dim) find_shaun as Range, so i think you can simply use
a = find_shaun.row

and delete
Range("find_shaun").Select

HTH

M.
 
Last edited:
Upvote 0
Complementing my previous post

better to check if anything was found, so

Code:
If Not find_shaun Is Nothing then
    a = find_shaun.Row
End If

M.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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