Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 296
Default How can I trap this Vlookup error in a VBA macro

Hi,

I have a need to identify whether a Job Number appears in a specific
range, and take different actions in a mcaro dependent on whether it
is or isn't present.

As a function this would simply be

=If(Iserror( Vlookup(x,MyRange,1,false)),"This","That")

However I'm having difficulties building this into a macro using
Application.WorksheetFunction. I keep stumbling on the 'Unable to get
the Vlookup property of the Worksheet Function Class" where the job
number doesn't exist

I've also tried MyRange.Find(what:=JobNo, after ............etc.,
but get the 'Object Variable with Block Variable not set' error, where
the jobs doesn't exist.

How can I construct an

If "JobNo exists" Then
"This"
else
"That"
End If

test?


Usual TIA

__
Richard Buttrey
Grappenhall, Cheshire, UK
__________________________
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How can I trap this Vlookup error in a VBA macro


Hi Richard,

Is your "JobNo" structured in a particular way ?

Carim


--
Carim
------------------------------------------------------------------------
Carim's Profile: http://www.excelforum.com/member.php...o&userid=33259
View this thread: http://www.excelforum.com/showthread...hreadid=531155

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default How can I trap this Vlookup error in a VBA macro

Strange indeed. Get around with on error:

Sub test()
Dim i As Long
On Error Resume Next
i = WorksheetFunction.Match(9, Range("A1:A12"), 0)
On Error GoTo 0
If i = 0 Then MsgBox "Not Found"
End Sub

HTH
--
AP

"Richard Buttrey" a écrit dans le
message de ...
Hi,

I have a need to identify whether a Job Number appears in a specific
range, and take different actions in a mcaro dependent on whether it
is or isn't present.

As a function this would simply be

=If(Iserror( Vlookup(x,MyRange,1,false)),"This","That")

However I'm having difficulties building this into a macro using
Application.WorksheetFunction. I keep stumbling on the 'Unable to get
the Vlookup property of the Worksheet Function Class" where the job
number doesn't exist

I've also tried MyRange.Find(what:=JobNo, after ............etc.,
but get the 'Object Variable with Block Variable not set' error, where
the jobs doesn't exist.

How can I construct an

If "JobNo exists" Then
"This"
else
"That"
End If

test?


Usual TIA

__
Richard Buttrey
Grappenhall, Cheshire, UK
__________________________



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 510
Default How can I trap this Vlookup error in a VBA macro

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default How can I trap this Vlookup error in a VBA macro

Hi Richard,
In another thread some time ago i found that VLookup() produces a
runtime error when the value can not be found. You can use the
following to snap that event.
Code:

On Error Resume Next

'VLookUp()

If Err.Number < 0 Then
'If not found
Debug.Print Err.Number
Debug.Print Err.Description
Else
' If found
End If
On Error Goto ErorrHndl

Regards,
Bondi



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 296
Default How can I trap this Vlookup error in a VBA macro

On Sat, 8 Apr 2006 03:22:45 -0500, Carim
wrote:


Hi Richard,

Is your "JobNo" structured in a particular way ?

Carim


It's always 5 characters, with an initial alpha and four numerics if
that helps.

e.g. M0123

Thanks

__
Richard Buttrey
Grappenhall, Cheshire, UK
__________________________
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 510
Default How can I trap this Vlookup error in a VBA macro

Hi Richard,

An alternative to your test could be :
If cell Like "[A-Z]####" Then
once the pattern is identified or not, do This or That ...

HTH
Cheers
Carim

  #8   Report Post  
Posted to microsoft.public.excel.programming
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



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default How can I trap this Vlookup error in a VBA macro

Carim,

That's the way to go. Notice that it works only if you use
Application.VLookup. If you use WorksheetFunction.VLookup you will get your
original runtime error.

hth,

Doug


"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



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 296
Default How can I trap this Vlookup error in a VBA macro

On 8 Apr 2006 01:50:15 -0700, "Bondi" wrote:

Hi Richard,
In another thread some time ago i found that VLookup() produces a
runtime error when the value can not be found. You can use the
following to snap that event.
Code:

On Error Resume Next

'VLookUp()

If Err.Number < 0 Then
'If not found
Debug.Print Err.Number
Debug.Print Err.Description
Else
' If found
End If
On Error Goto ErorrHndl

Regards,
Bondi


Hi Bondi

Thanks for that suggestion, I hadn't thought of trapping the error
number and using that - rather neat.

It also avoids the problem of forcing the program flow to an error
handler, which by definition is outside any Looping part of the
Procedure.

Regards

PS, thanks also to the others who responded.

__
Richard Buttrey
Grappenhall, Cheshire, UK
__________________________
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to trap an error I get in a macro Mary Excel Programming 2 March 31st 06 10:45 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error Trap Kirk P. Excel Programming 2 September 8th 05 09:51 PM
error trap Rhonda[_3_] Excel Programming 2 October 22nd 03 07:07 PM


All times are GMT +1. The time now is 04:30 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"