Dealing With An Anticipated Error In A Loop

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this line of code in my application that I know in advance there may be times it will err out, usually because the value .cells(i,3) doesn't exist ... yet.

Code:
.Cells(i, 4) = Application.WorksheetFunction.VLookup(.Cells(i, 3), ws_rd.Range("A:C"), 3, False)

When it does err, I would like to recognize that by executing some code exclusive to that scenario. Since this line is within a loop though, I don't want this to stop the loop, but carry on.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Use Application.Vlookup and store the result in a Variant. Then test it with Iserror(variable) and react accordingly.
 
Upvote 0
That makes too much sense. Thanks Rory.
 
Upvote 0
That makes too much sense. Thanks Rory.

But maybe not enough ...

Code:
       For i = 2 To lrow
            str_recid = Application.WorksheetFunction.VLookup(.Cells(i, 3), ws_rd.Range("A:C"), 3, False)
            If IsError(str_recid) Then
                MsgBox "Value missing."
                'Stop
            End If
       Next i

I'm encountering an error whether I put the vlookup before or after the IsError.
 
Upvote 0
Remove the Worksheetfunction. part:

Code:
str_recid = Application.VLookup(.Cells(i, 3), ws_rd.Range("A:C"), 3, False)

and I assume that str_recid is actually declared as Variant as I suggested?
 
Upvote 0
Yeah ... things are going well now. It was the worksheetfunction that was the issue.
I'll need to do some research whn to use it and when not to!
 
Upvote 0
If you use Worksheetfunction it will cause a run-time error in the code when the function fails. If you use the Application version, it returns an error value that you can test with IsError.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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