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:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,349
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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
 

Forum statistics

Threads
1,082,106
Messages
5,363,180
Members
400,720
Latest member
Pettel

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top