Vlookup #N/A

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Team,

I have code where I'm using vlookup function to compare two sheets. Below code works god:

Code:
Sub vlookup_copy()
    Dim wkbSource As Workbook
    Dim wksSource As Worksheet
    Dim wksDest As Worksheet
    Dim strFile As String
    Dim Rng As String
    Dim sfullpath As String
 
 
    Set wksDest = Worksheets("Sheet1")
 
    MsgBox "Open file with source data"
    strFile = Application.GetOpenFilename( _
        FileFilter:="Excel Files (*.xls), *.xls)", _
        Title:="Select a File", _
        MultiSelect:=False)
 
 
    If strFile = "False" Then
        Exit Sub
    End If
 
    Set wkbSource = Workbooks.Open(strFile)
    Set wksSource = wkbSource.Worksheets("Sheet1")
 
    With wksSource
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(.Rows.Count, LastColumn).End(xlUp).Row
        Rng = Range(.Cells(1, 1), .Cells(LastRow, LastColumn)).Address
    End With
 
    sfullpath = "[" & wksSource.Parent.Name & "]" & wksSource.Name
 
 
    wksDest.Activate
    x_rows = Application.WorksheetFunction.CountA(Columns(1))
 
    Range("B1").Select
    ActiveCell.Formula = "=VLOOKUP(A1,'" & sfullpath & "'!" & Rng & ",4,0)"
 
    Selection.AutoFill Destination:=Range("B1:B" & x_rows), Type:=xlFillDefault
 
    Application.DisplayAlerts = False
    wkbSource.Close
    Set wkbSource = Nothing
    Set wksSource = Nothing
    Application.DisplayAlerts = True
 
End Sub

But when looking value does not exist in array, macro display result as "#NA". Please help me, how to change the code. I would like for example to display note like "looking value not found".

king regards,
PvK
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Forestq,
Add error check in the formula itself.
Thus:
ActiveCell.Formula = "=IF(ISERROR(VLOOKUP(A1," & sfullpath & "!" & Rng & ",4,0)),""Lookup value not found"",VLOOKUP(A1," & sfullpath & "!" & Rng & ",4,0))"

(in Excel 2007 and above you can use IFERROR() to make it better readable)
 
Upvote 0
Thanks so much drsarao, works great.

Also, when I run function from workbook, I can do something like this and works also great ;).
Thanks for help once again!

= IF(ISNA(VLOOKUP(A1,Sheet2!$A:$C,2,FALSE)),"NOT FOUND",VLOOKUP(A1,Sheet2!$A:$C,2,FALSE))

regards,
PvK
 
Upvote 0
You are welcome.
In fact, ISNA() is better in this particular case.
Because ISERROR() reacts to ALL the errors eg #NUM,#VALUE etc.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

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