Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Formatting cell poulated by User Defined Function

I have a UDF that goes off and retrieves various pieces of data from a
database based on parameters that the user enters into the UDF. Some of the
data in the DB are strings, and others are Integers or Doubles.

My UDF returns a string so that any of those values can be displayed (i.e.
ints and doubles from the DB are converted to string in my method that calls
the DB).
Public Function MyUDF(param1, param2) As String

If a value coming back from the database is the number 3.51, the UDF returns
it to the calling cell as "3.51". If I try to format this in Excel and ask
it to display one decimal point, for example, it does nothing because it
treats 3.51 as a string.
If I wrap the call to the UDF with =VALUE, that works, but I wanted to avoid
having the user have to do this. I also wanted to avoid having seperate UDFs
for each return type (i.e. one that returns String, one that returns
Integer, etc.)

Does anyone have any suggestions?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Formatting cell poulated by User Defined Function

Have you tried

Public Function MyUDF(param1, param2) As Variant

I'm not sure it would work, but it's the next thing I'd try.
--
HTH,
Barb Reinhardt



"J. Caplan" wrote:

I have a UDF that goes off and retrieves various pieces of data from a
database based on parameters that the user enters into the UDF. Some of the
data in the DB are strings, and others are Integers or Doubles.

My UDF returns a string so that any of those values can be displayed (i.e.
ints and doubles from the DB are converted to string in my method that calls
the DB).
Public Function MyUDF(param1, param2) As String

If a value coming back from the database is the number 3.51, the UDF returns
it to the calling cell as "3.51". If I try to format this in Excel and ask
it to display one decimal point, for example, it does nothing because it
treats 3.51 as a string.
If I wrap the call to the UDF with =VALUE, that works, but I wanted to avoid
having the user have to do this. I also wanted to avoid having seperate UDFs
for each return type (i.e. one that returns String, one that returns
Integer, etc.)

Does anyone have any suggestions?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Formatting cell poulated by User Defined Function

Thanks for the suggestion. I forgot to mention that I tried that as well.
Even though the UDF returns a Variant, the .NET DLL that I call to (another
wrinkle here) comes back as a string. Even though the return type of the UDF
is Variant, it is still a string in the Variant and it is treated as such.

"Barb Reinhardt" wrote:

Have you tried

Public Function MyUDF(param1, param2) As Variant

I'm not sure it would work, but it's the next thing I'd try.
--
HTH,
Barb Reinhardt



"J. Caplan" wrote:

I have a UDF that goes off and retrieves various pieces of data from a
database based on parameters that the user enters into the UDF. Some of the
data in the DB are strings, and others are Integers or Doubles.

My UDF returns a string so that any of those values can be displayed (i.e.
ints and doubles from the DB are converted to string in my method that calls
the DB).
Public Function MyUDF(param1, param2) As String

If a value coming back from the database is the number 3.51, the UDF returns
it to the calling cell as "3.51". If I try to format this in Excel and ask
it to display one decimal point, for example, it does nothing because it
treats 3.51 as a string.
If I wrap the call to the UDF with =VALUE, that works, but I wanted to avoid
having the user have to do this. I also wanted to avoid having seperate UDFs
for each return type (i.e. one that returns String, one that returns
Integer, etc.)

Does anyone have any suggestions?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Formatting cell poulated by User Defined Function

OK, you're over my head now. Next. :)
--
HTH,
Barb Reinhardt



"J. Caplan" wrote:

Thanks for the suggestion. I forgot to mention that I tried that as well.
Even though the UDF returns a Variant, the .NET DLL that I call to (another
wrinkle here) comes back as a string. Even though the return type of the UDF
is Variant, it is still a string in the Variant and it is treated as such.

"Barb Reinhardt" wrote:

Have you tried

Public Function MyUDF(param1, param2) As Variant

I'm not sure it would work, but it's the next thing I'd try.
--
HTH,
Barb Reinhardt



"J. Caplan" wrote:

I have a UDF that goes off and retrieves various pieces of data from a
database based on parameters that the user enters into the UDF. Some of the
data in the DB are strings, and others are Integers or Doubles.

My UDF returns a string so that any of those values can be displayed (i.e.
ints and doubles from the DB are converted to string in my method that calls
the DB).
Public Function MyUDF(param1, param2) As String

If a value coming back from the database is the number 3.51, the UDF returns
it to the calling cell as "3.51". If I try to format this in Excel and ask
it to display one decimal point, for example, it does nothing because it
treats 3.51 as a string.
If I wrap the call to the UDF with =VALUE, that works, but I wanted to avoid
having the user have to do this. I also wanted to avoid having seperate UDFs
for each return type (i.e. one that returns String, one that returns
Integer, etc.)

Does anyone have any suggestions?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Formatting cell poulated by User Defined Function

On Fri, 29 Aug 2008 12:21:01 -0700, J. Caplan
wrote:

I have a UDF that goes off and retrieves various pieces of data from a
database based on parameters that the user enters into the UDF. Some of the
data in the DB are strings, and others are Integers or Doubles.

My UDF returns a string so that any of those values can be displayed (i.e.
ints and doubles from the DB are converted to string in my method that calls
the DB).
Public Function MyUDF(param1, param2) As String

If a value coming back from the database is the number 3.51, the UDF returns
it to the calling cell as "3.51". If I try to format this in Excel and ask
it to display one decimal point, for example, it does nothing because it
treats 3.51 as a string.
If I wrap the call to the UDF with =VALUE, that works, but I wanted to avoid
having the user have to do this. I also wanted to avoid having seperate UDFs
for each return type (i.e. one that returns String, one that returns
Integer, etc.)

Does anyone have any suggestions?


Would it work if you had your UDF return strings as strings and numbers as
doubles?

Perhaps something along the line of:

===========================
Function foo(i) As Variant
If Val(i) Like i Then
foo = CDbl(i)
Else
foo = CStr(i)
End If
End Function
======================

At least in Excel 2007, this will return number values as numbers, and strings
as strings. It will also return something like "3.52" as a number.
--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Formatting cell poulated by User Defined Function

Thanks for the suggestion. After playing around for a while, I found that I
can do just that. The trick is to have the .NET DLL that gets the data from
the database, return back the datatype with each piece of data so that I'll
know what to convert to on the VBA side (i.e CStr, CDbl, CDate, etc.)

"Ron Rosenfeld" wrote:

On Fri, 29 Aug 2008 12:21:01 -0700, J. Caplan
wrote:

I have a UDF that goes off and retrieves various pieces of data from a
database based on parameters that the user enters into the UDF. Some of the
data in the DB are strings, and others are Integers or Doubles.

My UDF returns a string so that any of those values can be displayed (i.e.
ints and doubles from the DB are converted to string in my method that calls
the DB).
Public Function MyUDF(param1, param2) As String

If a value coming back from the database is the number 3.51, the UDF returns
it to the calling cell as "3.51". If I try to format this in Excel and ask
it to display one decimal point, for example, it does nothing because it
treats 3.51 as a string.
If I wrap the call to the UDF with =VALUE, that works, but I wanted to avoid
having the user have to do this. I also wanted to avoid having seperate UDFs
for each return type (i.e. one that returns String, one that returns
Integer, etc.)

Does anyone have any suggestions?


Would it work if you had your UDF return strings as strings and numbers as
doubles?

Perhaps something along the line of:

===========================
Function foo(i) As Variant
If Val(i) Like i Then
foo = CDbl(i)
Else
foo = CStr(i)
End If
End Function
======================

At least in Excel 2007, this will return number values as numbers, and strings
as strings. It will also return something like "3.52" as a number.
--ron

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Formatting cell poulated by User Defined Function

On Sat, 30 Aug 2008 13:03:00 -0700, J. Caplan
wrote:

Thanks for the suggestion. After playing around for a while, I found that I
can do just that. The trick is to have the .NET DLL that gets the data from
the database, return back the datatype with each piece of data so that I'll
know what to convert to on the VBA side (i.e CStr, CDbl, CDate, etc.)


Glad to help. Thanks for the feedback.
--ron
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
Can I Assign A User Defined Function To A Cell? smartin Excel Worksheet Functions 0 June 28th 09 10:25 PM
Which cell is calling a user-defined function? Randy in Calgary Excel Programming 1 February 11th 08 04:04 PM
User Defined Function - Using Cell Range I need help please Excel Programming 3 May 2nd 07 11:14 PM
current cell in user-defined function Julio Kuplinsky Excel Programming 3 December 8th 03 06:24 PM
User defined function & formatting Bob Phillips[_5_] Excel Programming 3 September 10th 03 12:12 AM


All times are GMT +1. The time now is 11:39 PM.

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"