Vlookup VBA code error handling

haf19

New Member
Joined
Jul 15, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
i have a code that vlookups an ID number and brings in a name from a different sheet.
However some of the rows are blank, as in no ID number - or they have two IDs in one cell

How do i insert an error handing code to skip blank rows or unrecognised values?

this is my current code.
Sub vlookup()

For i = 2 to 300

Sheets("test1").Cells(i,34).Value=Application.WorksheetsFunction.vlookup _
(Sheets("test1").Cells(i,33).Value,Sheets("test2").range("A:H"),7, False)

Next
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi & welcome to MrExcel.
How about
VBA Code:
Sub vlookup()
   Dim Res As Variant
   Dim i As Long
   
   For i = 2 To 300
      Res = Application.vlookup(Sheets("test1").Cells(i, 33).Value, Sheets("test2").Range("A:H"), 7, False)
      If Not IsError(Res) Then Sheets("test1").Cells(i, 34).Value = Res
   Next
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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