VBA Vlookup issue

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I have a macro that opens up a different workbook and uses the worksheetfunction.vlookup to find a value and store it in a variable and then close that workbook.

The problem I am running into is the macro crashing when the vlookup value isn't present in that workbook.

Any ideas for a fix?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You need to use an error handling variable for the VLOOKUP, also change it to Application.Vlookup instead of WorksheetFunction.Vlookup. By having it as an Application object instead of a WorksheetFunction object, it returns a trappable error.

Code:
Dim x as Variant
x = Application.Vlookup(*your formula here*)
If Not IsError(x) Then
    ' What you want to do when the Vlookup returns a value
Else
    ' What you want to do when the Vlookup returns an error (no value found)
End If
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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