VLOOKUP does return an errorif an excat value is sought
but doesn't exist - just be sure that the Range_Lookup
value is set to False
In a worksheet you'd need to use an IF statement
eg
=IF(ISNA(VLOOKUP(E7,K6:L80,2,FALSE)),"NO MATCH",VLOOKUP
(E7,K6:L80,2,FALSE))
This return the 2 column in the table if there's a match,
otherwise returns the message.
In VBA, use "Application.WorksheetFunction.VLookup"
- but do it in a function where you can trap the error
eg
Sub LookupTest()
MsgBox GetValue(Range("E7"), Range("K4:L800"), 2)
End Sub
Function GetValue(Target As String, _
table As Range, _
Col As Long) As Variant
Dim result As Variant
On Error Resume Next
result = _
Application.WorksheetFunction.VLookup(Target, table,
Col, False)
If Err.Number <> 0 Then
Err.Clear
result = "No Match"
End If
GetValue = result
On Error GoTo 0
End Function
Patrick Molloy
Microsoft Excel MVP