VBA Vlook up help - Double onerror goto condition

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hello Friends,

I am trying to vlookup on two range,

if Name not found in single range then check in second range,
if not found in there also then I want result not found.
Below is my attempted code.

ABDEGH
NameScoreNameMatch ScoreNameMatch Score
Sachin105Sachin105Dhoni98
Dhoni98Dhoni98Bravo35
ViratNot FoundBravo35Ponting75
VirenderNot FoundPonting75Brett Lee20
Brett Lee20Brett Lee20Sachin105
Bravo35Yuvaraj111
Kapil Dev Not Found

<tbody>
</tbody>




Code:
Sub Double_Error()
Dim lr As Long, r As Long

Dim myrange1 As Range
Dim myrange2 As Range

Set myrange1 = Sheet1.Range("D:E")
Set myrange2 = Sheet1.Range("G:H")

lr = Sheet1.Range("a500").End(xlUp).Row

Sheet1.Activate

On Error GoTo Error1
For r = 2 To lr
    Cells(r, 2).Value = WorksheetFunction.VLookup(Cells(r, 1), myrange1, 2, False)
Next r
Exit Sub

Error1:
On Error GoTo Error2
    Cells(r, 2).Value = WorksheetFunction.VLookup(Cells(r, 1), myrange2, 2, False)
Resume Next

Error2:
Cells(r, 2).Value = "Not Found"
Resume Next

End Sub
 
Last edited by a moderator:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about
Code:
Sub Double_Error()
   Dim Fnd As Range
   Dim Cl As Range
   
   With Sheet1
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Set Fnd = .Range("D:D").find(Cl.Value, , , xlWhole, , , False, , False)
         If Fnd Is Nothing Then Set Fnd = .Range("G:G").find(Cl.Value, , , xlWhole, , , False, , False)
         If Fnd Is Nothing Then
            Cl.Offset(, 1).Value = "Not Found"
         Else
            Cl.Offset(, 1).Value = Fnd.Offset(, 1).Value
         End If
      Next Cl
   End With

End Sub
 
Upvote 0
Hi Fluff,


Fantastic, it has worked for me. Thanks a lot,

Is it possible to get the same answer through array as I have huge data 100000+row .
In future I want to try Vlookup + with array . Thanks in advance !!

Regards,
Mallesh
 
Upvote 0
Glad to help & thanks for the feedback.

I don't often use arrays & not sure how to do that on this occasion.
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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