Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Simple Excel VBA Question

I am trying to create a Vlookup function with an additional argument to
return if the vlookup function evaluates to #N/A (Below is the code).

It works fine unless the function evaluates to #N/A.

I would greatly appreciate any help,
Michael

Function VLOOKUP_NA(VAL1, Range, OFFSET, TF, NAVALUE)

If WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) < False Then
VLOOKUP_NA = NAVALUE
ElseIf WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) = False Then
VLOOKUP_NA = WorksheetFunction.VLookup(VAL1, Range, OFFSET, TF)
End If

End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Simple Excel VBA Question

You might consider using a wrapper function for VLOOKUP instead - that
way it can be flexible enough to use with HLOOKUP, or MATCH, too:

For instance:

=IfNA(VLOOKUP(A1,J:K,2,FALSE),"NA VALUE")

or just

=IFNA(MATCH(A1,J:J,FALSE))


Here's one implementation:

Public Function IfNA(ByRef vTest As Variant, _
Optional ByRef vDefault As Variant = vbNullString) As Variant
If vTest = CVErr(xlErrNA) Then
IfNA = vDefault
Else
IfNA = vTest
End If
End Function




In article ,
Michael Saathoff wrote:

I am trying to create a Vlookup function with an additional argument to
return if the vlookup function evaluates to #N/A (Below is the code).

It works fine unless the function evaluates to #N/A.

I would greatly appreciate any help,
Michael

Function VLOOKUP_NA(VAL1, Range, OFFSET, TF, NAVALUE)

If WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) < False Then
VLOOKUP_NA = NAVALUE
ElseIf WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) = False Then
VLOOKUP_NA = WorksheetFunction.VLookup(VAL1, Range, OFFSET, TF)
End If

End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default Simple Excel VBA Question

Hello,

In case of an error your function does not get further within the
isna() call, I think.

I suggest:
Function VLOOKUP_NA(VAL1, Range, OFFSET, TF, NAVALUE)

On Error Resume Next
VLOOKUP_NA = WorksheetFunction.VLookup(VAL1, Range, OFFSET, TF)
If Err.Number < 0 Then VLOOKUP_NA = NAVALUE

End Function

Regards,
Bernd

  #4   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Simple Excel VBA Question

My experience has been when invoking Vlookup in VBA with
WorksheetFunction.VLookup
I always get a run-time error "cannot get the vlookup property of the
worksheetfunction class" any time vlookup evaluates to NA.

But when I use Application.Vlookup, no run time errors.



"Michael Saathoff" wrote:

I am trying to create a Vlookup function with an additional argument to
return if the vlookup function evaluates to #N/A (Below is the code).

It works fine unless the function evaluates to #N/A.

I would greatly appreciate any help,
Michael

Function VLOOKUP_NA(VAL1, Range, OFFSET, TF, NAVALUE)

If WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) < False Then
VLOOKUP_NA = NAVALUE
ElseIf WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) = False Then
VLOOKUP_NA = WorksheetFunction.VLookup(VAL1, Range, OFFSET, TF)
End If

End Function

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Simple Excel VBA Question

Thanks for pointing me to chip's web page - great info there.
I'm still pretty green at VBA programming, and this discussion
group is fantastic!

Dave


"JMB" wrote:

chip has some discussion on his site
http://www.cpearson.com/excel/Callin...onsFromVBA.htm

but I've not yet found an explanation for exactly why that happens. it
seems to behave like that for match, hlookup, lookup, and ceiling. I guess
it is something to be cognizant of if the function can return an error (such
as #NA or #NUM).


"Dave" wrote:

When I use the WorksheetFunction.Find, the module always aborts
with "run-time error#1004 cannot get the Find property of the
worksheet function class". Using Application.Find works fine.

Is this true for all of the WorksheetFunction properties?

Dave

"JMB" wrote:

My experience has been when invoking Vlookup in VBA with
WorksheetFunction.VLookup
I always get a run-time error "cannot get the vlookup property of the
worksheetfunction class" any time vlookup evaluates to NA.

But when I use Application.Vlookup, no run time errors.



"Michael Saathoff" wrote:

I am trying to create a Vlookup function with an additional argument to
return if the vlookup function evaluates to #N/A (Below is the code).

It works fine unless the function evaluates to #N/A.

I would greatly appreciate any help,
Michael

Function VLOOKUP_NA(VAL1, Range, OFFSET, TF, NAVALUE)

If WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) < False Then
VLOOKUP_NA = NAVALUE
ElseIf WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) = False Then
VLOOKUP_NA = WorksheetFunction.VLookup(VAL1, Range, OFFSET, TF)
End If

End Function



  #6   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Simple Excel VBA Question

Some additional links that you may find have helpful information (and/or some
nifty downloads)

http://www.decisionmodels.com/
http://erlandsendata.no/english/
http://www.contextures.com/
http://j-walk.com/
http://vb.mvps.org/
http://mcgimpsey.com/
http://xcell05.free.fr/
http://xldynamic.com/
http://mvps.org (look for the excel link on the right).



"Dave" wrote:

Thanks for pointing me to chip's web page - great info there.
I'm still pretty green at VBA programming, and this discussion
group is fantastic!

Dave


"JMB" wrote:

chip has some discussion on his site
http://www.cpearson.com/excel/Callin...onsFromVBA.htm

but I've not yet found an explanation for exactly why that happens. it
seems to behave like that for match, hlookup, lookup, and ceiling. I guess
it is something to be cognizant of if the function can return an error (such
as #NA or #NUM).


"Dave" wrote:

When I use the WorksheetFunction.Find, the module always aborts
with "run-time error#1004 cannot get the Find property of the
worksheet function class". Using Application.Find works fine.

Is this true for all of the WorksheetFunction properties?

Dave

"JMB" wrote:

My experience has been when invoking Vlookup in VBA with
WorksheetFunction.VLookup
I always get a run-time error "cannot get the vlookup property of the
worksheetfunction class" any time vlookup evaluates to NA.

But when I use Application.Vlookup, no run time errors.



"Michael Saathoff" wrote:

I am trying to create a Vlookup function with an additional argument to
return if the vlookup function evaluates to #N/A (Below is the code).

It works fine unless the function evaluates to #N/A.

I would greatly appreciate any help,
Michael

Function VLOOKUP_NA(VAL1, Range, OFFSET, TF, NAVALUE)

If WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) < False Then
VLOOKUP_NA = NAVALUE
ElseIf WorksheetFunction.IsNA(WorksheetFunction.VLookup(V AL1, Range, OFFSET,
TF)) = False Then
VLOOKUP_NA = WorksheetFunction.VLookup(VAL1, Range, OFFSET, TF)
End If

End Function

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
Simple Excel Question Maurice Excel Discussion (Misc queries) 6 August 3rd 09 09:19 PM
IF formula-simple question; simple operator Rich D Excel Discussion (Misc queries) 4 December 6th 07 03:36 PM
Simple Excel Question? Jo[_2_] Excel Programming 2 July 11th 07 04:46 PM
very simple VBA question for Excel Giose Excel Programming 1 March 14th 07 06:51 PM
Simple Simple Excel usage question BookerW Excel Discussion (Misc queries) 1 June 23rd 05 10:06 PM


All times are GMT +1. The time now is 06:14 PM.

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"