Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default User Defined function - second posting

This has be posted before. I received a couple of replies but unfortunately
the proposed solutions didn't work so I'm posting again.

I have a UDF that is working fine:

Function Pcode_na(Str_Deal As Variant)

'Variable to hold VLookup Range
Dim Decap_Range As Variant

'Constant Range
Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

'User Defined Function
Pcode = Application.WorksheetFunction.VLookup(Str_Deal, Decap_Range, 6,
False)


I'm trying to extend this so it takes into account results that are returned
n/a and replace this with "0"

I have tried:

'User Defined Function
Pcode = Application.WorksheetFunction.if(IsNA(VLookup(Str_ Deal, Decap_Range,
6, False)), 0, (VLookup(Str_Deal, Decap_Range, 6, False)))

When I run this I get and error stating that the function is not defined
(i.e. VLookup).

Is there something I'm missing from this?

Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default User Defined function - second posting

I had a typo in my previous response and didn't assign the results to the
name of the function. This has been tested and works when the workbook
Data.xls is open.

Function Pcode_na(Str_Deal As Variant)

'Variable to hold VLookup Range
Dim Decap_Range As Range
Dim res As Variant

'Constant Range
Set Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

res = Application.VLookup(Str_Deal, Decap_Range, 6, False)
If Not IsError(res) Then
Pcode_na = res
Else
Pcode_na = 0
End If
End Function

--
Regards,
Tom Ogilvy

"PC" <paulm DOT c at iol DOT ie wrote in message
...
This has be posted before. I received a couple of replies but

unfortunately
the proposed solutions didn't work so I'm posting again.

I have a UDF that is working fine:

Function Pcode_na(Str_Deal As Variant)

'Variable to hold VLookup Range
Dim Decap_Range As Variant

'Constant Range
Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

'User Defined Function
Pcode = Application.WorksheetFunction.VLookup(Str_Deal, Decap_Range, 6,
False)


I'm trying to extend this so it takes into account results that are

returned
n/a and replace this with "0"

I have tried:

'User Defined Function
Pcode = Application.WorksheetFunction.if(IsNA(VLookup(Str_ Deal,

Decap_Range,
6, False)), 0, (VLookup(Str_Deal, Decap_Range, 6, False)))

When I run this I get and error stating that the function is not defined
(i.e. VLookup).

Is there something I'm missing from this?

Thanks




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default User Defined function - second posting

PC,

Check for the error, and change the return to 0 if it exists:

Function Pcode_na(Str_Deal As Variant) As Variant
'Variable to hold VLookup Range
Dim Decap_Range As Range

'Constant Range
Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

Pcode_na = Application.VLookup(Str_Deal, Decap_Range, 6, False)

'This is the check that you need
If IsError(Pcode_na) Then Pcode_na = 0

End Function

HTH,
Bernie
MS Excel MVP

"PC" <paulm DOT c at iol DOT ie wrote in message
...
This has be posted before. I received a couple of replies but

unfortunately
the proposed solutions didn't work so I'm posting again.

I have a UDF that is working fine:

Function Pcode_na(Str_Deal As Variant)

'Variable to hold VLookup Range
Dim Decap_Range As Variant

'Constant Range
Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

'User Defined Function
Pcode = Application.WorksheetFunction.VLookup(Str_Deal, Decap_Range, 6,
False)


I'm trying to extend this so it takes into account results that are

returned
n/a and replace this with "0"

I have tried:

'User Defined Function
Pcode = Application.WorksheetFunction.if(IsNA(VLookup(Str_ Deal,

Decap_Range,
6, False)), 0, (VLookup(Str_Deal, Decap_Range, 6, False)))

When I run this I get and error stating that the function is not defined
(i.e. VLookup).

Is there something I'm missing from this?

Thanks




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default User Defined function - second posting

Thanks for your help guys. Both solutions work great. One question: When I
use Application.WorksheetFunction.VLookup the returned value is fine if
there is no error but when there is an error the result is #Value. When I
remove the WorkSheetFunction to leave Application.VLookup as you suggested
all works fine.

When, if ever, is WorkSheetFuntion required.

Again thats for your help.

Paul


"PC" <paulm DOT c at iol DOT ie wrote in message
...
This has be posted before. I received a couple of replies but

unfortunately
the proposed solutions didn't work so I'm posting again.

I have a UDF that is working fine:

Function Pcode_na(Str_Deal As Variant)

'Variable to hold VLookup Range
Dim Decap_Range As Variant

'Constant Range
Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

'User Defined Function
Pcode = Pcode = Application.WorksheetFunction.VLookup(Str_Deal,

Decap_Range, 6,
False)

(Str_Deal, Decap_Range, 6,
False)


I'm trying to extend this so it takes into account results that are

returned
n/a and replace this with "0"

I have tried:

'User Defined Function
Pcode = Application.WorksheetFunction.if(IsNA(VLookup(Str_ Deal,

Decap_Range,
6, False)), 0, (VLookup(Str_Deal, Decap_Range, 6, False)))

When I run this I get and error stating that the function is not defined
(i.e. VLookup).

Is there something I'm missing from this?

Thanks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default User Defined function - second posting

When worksheetfunction is used, Vlookup returns a 1004 type error - which is
suppressed when used in a UDF.

Using just application, it returns the equivalent of #N/A which can be
checked with iserror. It is your choice which way you go. You just have to
write the code to recognize the condition. (for Worksheetfunction you would
use a normal VBA error handler).

--
Regards,
Tom Ogilvy

"PC" <paulm DOT c at iol DOT ie wrote in message
...
Thanks for your help guys. Both solutions work great. One question: When I
use Application.WorksheetFunction.VLookup the returned value is fine if
there is no error but when there is an error the result is #Value. When I
remove the WorkSheetFunction to leave Application.VLookup as you suggested
all works fine.

When, if ever, is WorkSheetFuntion required.

Again thats for your help.

Paul


"PC" <paulm DOT c at iol DOT ie wrote in message
...
This has be posted before. I received a couple of replies but

unfortunately
the proposed solutions didn't work so I'm posting again.

I have a UDF that is working fine:

Function Pcode_na(Str_Deal As Variant)

'Variable to hold VLookup Range
Dim Decap_Range As Variant

'Constant Range
Decap_Range = Workbooks("Data.xls").Sheets("Decap").Range("A3:G5 000")

'User Defined Function
Pcode = Pcode = Application.WorksheetFunction.VLookup(Str_Deal,

Decap_Range, 6,
False)

(Str_Deal, Decap_Range, 6,
False)


I'm trying to extend this so it takes into account results that are

returned
n/a and replace this with "0"

I have tried:

'User Defined Function
Pcode = Application.WorksheetFunction.if(IsNA(VLookup(Str_ Deal,

Decap_Range,
6, False)), 0, (VLookup(Str_Deal, Decap_Range, 6, False)))

When I run this I get and error stating that the function is not defined
(i.e. VLookup).

Is there something I'm missing from this?

Thanks








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
user defined function driller Excel Worksheet Functions 1 November 18th 06 04:51 PM
User Defined Function Samad Excel Discussion (Misc queries) 14 November 16th 05 12:32 PM
User-defined data type; Error: Only User-defined types... tiger_PRM Excel Programming 1 July 18th 04 03:32 PM
USer Defined Function Excel Dummy[_2_] Excel Programming 2 November 5th 03 11:33 AM
User-Defined Function pre-empting Built-in Function? How to undo???? MarWun Excel Programming 1 August 6th 03 09:31 PM


All times are GMT +1. The time now is 10:26 AM.

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

About Us

"It's about Microsoft Excel"