Team,
I have code where I'm using vlookup function to compare two sheets. Below code works god:
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
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: