Error Handling with Selection.Find not working fine

lkarthik25

New Member
Joined
Jul 6, 2014
Messages
1
I have an excel where I have to check if an account is present in which of two account listings. Else, I have to go to the next account. The code I am using is:
Code:
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">[COLOR=#00008B]For[/COLOR] x=[COLOR=#800000]1[/COLOR] [COLOR=#00008B]to[/COLOR] acc_num
Sheets([COLOR=#800000]"List1"[/COLOR]).[COLOR=#00008B]Select
[/COLOR]Range([COLOR=#800000]"A1:A100"[/COLOR]).[COLOR=#00008B]Select
[/COLOR][COLOR=#00008B]On[/COLOR] [COLOR=#00008B]Error[/COLOR] [COLOR=#00008B]GoTo[/COLOR] CheckList2
Selection.Find(what:=x).Activate
[COLOR=#00008B]GoTo[/COLOR] Found

CheckList2:
Sheets([COLOR=#800000]"List2"[/COLOR]).[COLOR=#00008B]Select
[/COLOR]Range([COLOR=#800000]"A1:A100"[/COLOR]).[COLOR=#00008B]Select
[/COLOR][COLOR=#00008B]On[/COLOR] [COLOR=#00008B]Error[/COLOR] [COLOR=#00008B]GoTo[/COLOR] NotFound
Selection.Find(what:=x).Activate

Found:[COLOR=#808080]'More Code to execute if account is found

[/COLOR]NotFound:
[COLOR=#00008B]On[/COLOR] [COLOR=#00008B]Error[/COLOR] [COLOR=#00008B]GoTo[/COLOR] [COLOR=#800000]0
[/COLOR][COLOR=#00008B]Next[/COLOR] x
</code>

When an account is not found in List1 I still get the error pop-up: Run-time '91' : Object variable or With block not set.
I am unable to see why the error handling is not doing its job. Please help me out.
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You need a Resume statement to reset the error handler. But it's better to simply test if the value is found:

Code:
Dim rngFound as range
For x=1 to acc_num
Set rngFound = Sheets("List1").Range("A1:A100").Find(what:=x) 
If rngFound is nothing then
Set rngFound = Sheets("List2").Range("A1:A100").Find(what:=x)
End if

If not rngFound is nothing then
RngFound.activate
'More Code to execute if account is found
End if
Next x
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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