Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing error when using Find and value is not in defined range

Status
Not open for further replies.

esswired

Technical User
Jul 13, 2004
3
US
Hello,
I have written code to search for a name in a range (a column). When the name is not there, I get an Error 91 which stops my code. I know the value will not always be there and would like to capture the error rather than have the code abort. The On Error statement I have does not do this. Can you help? The code is below.
Thanks

Columns(WhoCol).Select 'search for active name in Who column only
On Error GoTo No_Loc_Yet 'trap for unfound name
Selection.Find(What:=Active_Name, LookAt:=xlWhole, SearchOrder:=xlByRows).Activate
 
In your error handler, you should have something similar to the following:

Code:
If err.number = 91 then
   resume next
else
   msgbox(err.number)
endif

I hope this helps.
 


Hi,

This looks like Excel VBA. Would be better to post in Forum707.

Do this...
Code:
   Dim rFound as Range

   Set rFound = Columns(WhoCol).Find(What:=Active_Name, LookAt:=xlWhole, SearchOrder:=xlByRows)

   If Not rFound Is Nothing Then
     'Found the lookup value. Now process

   Else
     'NOT Found -- some alternative process???

   End If


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you Skip. Works like a charm and got me looking into VBA which I am enjoying very much.
 
Code:
On Error GoTo No_Loc_Yet    'trap for unfound name

This doesn't handle your error by itself, it just sends you to the portion of code labelled:
Code:
No_Loc_Yet:

Under that is where you need to write code to handle the error, such as checking what the Err.Number is and then doing something appropriate for that error (like in BlueJay07's code).
 
Thanks, Joe. I did have that and ended up using the VBA solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top