2 Looping Vlookups Error Handling

radonwilson

Board Regular
Joined
Jun 23, 2021
Messages
50
Office Version
  1. 2019
Platform
  1. Windows
How to handle the error of 2 looping vlookups which are using the same counter/offsetting variable? I have successfully handled errors for vlookup 1 until my second vlookup1 started running. As soon as my vlookup 2 falls into error it immediately goes to the error label which in results affecting my vlookup 1 , how can I handle errors of both these vlookups separately?

Here is my code, please have a look. I don't want to use On Error Resume Next.


test.xlsm
ABCDEFGHIJK
1Customer NameUnit Price
2Allen Rosenblatt15.74VLOOKUP 1VLOOKUP 2
3Barry French216.85Customer NameUnit PriceCustomer NameUnit Price
4Carl Ludwig20.88Carl Ludwig20.88Edward Hooks500.98
5Carlos Soltero8.4Carlos SolteroNot FoundEugene Barchas 1
6Claudia Miner5.28Claudia Miner 1Not FoundJack Garza40.96
7Clay Rozendal195.99Neola Schneider 1Not FoundJim Radford221.98
8Don Miller2.88Sylvia FoulstonNot FoundJulia West 1
9Edward Hooks500.98
10Eugene Barchas625.85
11Jack Garza40.96
12Jim Radford221.98
13Julia West95.95
14Muhammed MacIntyre38.94
15Neola Schneider39.89
16Sylvia Foulston201.96
Sheet1


VBA Code:
Option Explicit

Sub vlookup()

Dim c As Integer

On Error GoTo Last


For c = 1 To 5

Range("G" & c + 3).Value = Application.WorksheetFunction.vlookup( _
Range("f" & c + 3).Value, Range("a:b"), 2, 0)

Next c


For c = 1 To 5

Range("k" & c + 3).Value = Application.WorksheetFunction.vlookup( _
Range("j" & c + 3).Value, Range("a:b"), 2, 0)

Next c


Exit Sub
Last:
If Err.Number = 1004 Then
Range("G" & c + 3).Value = "Not Found"
Resume Next
End If

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Here's one way you could do it:

VBA Code:
Sub MyVlookup()

    With Range("G4:G8")
        .Formula = "=IFERROR(VLOOKUP(F4,A:B,2,),""Not Found"")"
        .Value = .Value
    End With
   
    With Range("K4:K8")
        .Formula = "=IFERROR(VLOOKUP(J4,A:B,2,),""Not Found"")"
        .Value = .Value
    End With
   
End Sub
 
Upvote 0
VBA Code:
Sub test()
For Each cell In Range("F4:F8")
    If Range("A2:A16").Find(cell) Is Nothing Then
        cell.Offset(, 1).Value = "Not Found"
    Else
        cell.Offset(, 1).Value = Range("A2:A16").Find(cell).Offset(, 1)
    End If
Next
    For Each cell In Range("J4:J8")
    If Range("A2:A16").Find(cell) Is Nothing Then
        cell.Offset(, 1).Value = "Not Found"
    Else
        cell.Offset(, 1).Value = Range("A2:A16").Find(cell).Offset(, 1)
    End If
Next
End Sub
 
Upvote 0
Another possibility to try

VBA Code:
Sub LookupValues()
  With Range("G4:K" & Columns("F:J").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row)
    Union(.Columns(1), .Columns(5)).FormulaR1C1 = "=if(RC[-1]="""","""",iferror(vlookup(RC[-1],C1:C2,2,0),""Not Found""))"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Rather than using general error handling, you should check each lookup like
VBA Code:
Sub vlookup()

Dim c As Integer
Dim Ans As Variant


For c = 1 To 5
   Ans = Application.vlookup(Range("f" & c + 3).Value, Range("a:b"), 2, 0)
   If IsError(Ans) Then
      Range("G" & c + 3).Value = "Not Found"
   Else
      Range("G" & c + 3).Value = Ans
   End If
Next c


For c = 1 To 5
   Ans = Application.vlookup(Range("j" & c + 3).Value, Range("a:b"), 2, 0)
   If IsError(Ans) Then
      Range("k" & c + 3).Value = "Not Found"
   Else
      Range("k" & c + 3).Value = Ans
   End If
Next c


End Sub
 
Upvote 0
Solution
Rather than using general error handling, you should check each lookup like
VBA Code:
Sub vlookup()

Dim c As Integer
Dim Ans As Variant


For c = 1 To 5
   Ans = Application.vlookup(Range("f" & c + 3).Value, Range("a:b"), 2, 0)
   If IsError(Ans) Then
      Range("G" & c + 3).Value = "Not Found"
   Else
      Range("G" & c + 3).Value = Ans
   End If
Next c


For c = 1 To 5
   Ans = Application.vlookup(Range("j" & c + 3).Value, Range("a:b"), 2, 0)
   If IsError(Ans) Then
      Range("k" & c + 3).Value = "Not Found"
   Else
      Range("k" & c + 3).Value = Ans
   End If
Next c


End Sub
Is there is any difference between the 2 codes given below?

Rich (BB code):
IsError(Application.vlookup(Range("f" & c + 3).Value, Range("a:b"), 2, 0))

Rich (BB code):
IsError(Application.worksheetfuntion.vlookup(Range("f" & c + 3).Value, Range("a:b"), 2, 0))

As you can see that in the first code vlookup is written right after the Application. and if I am writing it in the same way I am not getting errors.

But If I am writing it as the per second code I am getting errors.

So what is the difference between writing these two?
 
Upvote 0
If the VLookup fails, Application.Vlookup will return an error value which can be tested using IsError().

But if you're using Application.WorksheetFunction.Vlookup (and spelling WorksheetFunction correctly) and the Vlookup fails, you'll get a runtime error and be prompted to stop the code or go into debug mode.

To use WorksheetFunction, you'd need to wrap like this:
VBA Code:
On Error Resume Next
ans = Application.WorksheetFunction.VLookup(Range("f" & C + 3).Value, Range("a:b"), 2, 0)
If Err Then
    MsgBox Err.Number
    'do something?
End If
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,225,136
Messages
6,183,067
Members
453,147
Latest member
Lacey D

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