Need to skip VLoopkup (no Value) to next step in For Loop

Totti_K

New Member
Joined
Jun 14, 2020
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am writing a simple code to loop through a row of data. I have a for loop that goes from one cell to anohter. When vlookup reach a cell where there is not value, the macro stops there. How can I skip to the next vLoopUp action? If iRow 3 has an error, skip to iRow 4.

For iRow = 2 To 5
sLookUpValue = Worksheets("Item").Range(sDeptColumnLetter & iRow).Value
sResult = Application.VLookup((sLookUpValue), Worksheets("Department").Range("A1:D200"), 4, False)
Worksheets("Item").Range(sDeptColumnLetter & iRow) = sResult
Next iRow
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this
VBA Code:
For iRow = 2 To 5
   sLookUpValue = Worksheets("Item").Range(sDeptColumnLetter & iRow).Value
   On Error Resume Next
     sResult = Application.VLookup((sLookUpValue), Worksheets("Department").Range("A1:D200"), 4, False)
     If Err.Number = False then  Worksheets("Item").Range(sDeptColumnLetter & iRow) = sResult
  On Error GoTo 0
Next iRow
 
Upvote 0
Try this
VBA Code:
For iRow = 2 To 5
   sLookUpValue = Worksheets("Item").Range(sDeptColumnLetter & iRow).Value
   On Error Resume Next
     sResult = Application.VLookup((sLookUpValue), Worksheets("Department").Range("A1:D200"), 4, False)
     If Err.Number = False then  Worksheets("Item").Range(sDeptColumnLetter & iRow) = sResult
  On Error GoTo 0
Next iRow
Thanks. this works perfectly. I just need to add the 'end if'? also the 'On Error Goto 0 is closing of the error catch?
 
Upvote 0
I just need to add the 'end if'?
No
- everything is in ONE line
- the code will fail if you add End If

End If
is required if written like this
VBA Code:
For iRow = 2 To 5
   sLookUpValue = Worksheets("Item").Range(sDeptColumnLetter & iRow).Value
   On Error Resume Next
     sResult = Application.VLookup((sLookUpValue), Worksheets("Department").Range("A1:D200"), 4, False)
     If Err.Number = False Then  
        Worksheets("Item").Range(sDeptColumnLetter & iRow) = sResult
     End IF
  On Error GoTo 0
Next iRow

the 'On Error Goto 0 is closing of the error catch?

On Error Resume Next tells VBA to carry on and run the next line
On Error Goto 0 resets handling, making VBA will stop at the next error

VLookup
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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