View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default How can I trap this Vlookup error in a VBA macro

Here is a summary (for vlookup, but also for Match and probably for all the
other lookup functions)

Application.WorksheetFunction.Vlookup(args)
WorksheetFunction.Vlookup(args)

both raise a trappable error (1004 error) when the value is not found

Application.Vlookup(args)

This can be controlled or accounted for with with normal error handling such
as
On Error Resume next
or
On error goto Label


returns the equivalent of #N/A and can be checked with IsError

dim res as Variant
res = Application.Vlookup(args)
if iserror(res) then
' value was not found
else
' value was found
End if

When using find, use a construct like

dim rng as Range

set rng = range.Find(value)
if not rng is nothing then
' rng holds a reference to the found cell
else
' the search value was not found
End if



--
Regards,
Tom Ogilvy


"Carim" wrote in message
oups.com...
Richard,

Could the following help ...
Result = Application.VLookup(x, MyRange,2,0)
if iserror(Result) then

to detect where somthing is going wrong ...

HTH
Carim