ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   UDF that returns a date (https://www.excelbanter.com/excel-programming/404506-re-udf-returns-date.html)

Pete_UK

UDF that returns a date
 
The UDF should return a number to the cell, which can be formatted as
a date to suit your requirements. The number is an integer measuring
the number of elapsed days, with 1st Jan 1900 = Day 1. Excel assumes
(incorrectly) that 1900 was a leap year, so you need to take this into
account. The serial value for today's date (17th Jan 2008) is 39464.

Hope this helps.

Pete

On Jan 17, 11:31*pm, ep wrote:
Hi all,

I have a c# udf accessed as an automation add-in that needs to return dates
to Excel. I know I can't manipulate the cell's NumberFormat from within the
UDF, but is there a way to return a value that will be formatted in Excel as
a date? It seems any string value I return will default to general. Let me
know if any other info would help.

thanks



Pete_UK

UDF that returns a date
 
Dates are just integers to Excel, but the cell can be formatted to
display them in different ways, so that if you apply a date format to
the cell, then Excel will interpret the number according to the rules
I outlined earlier and display it in the chosen manner (i.e dd/mm/yy,
or mm/dd/yyyy, or mmm-dd etc. Consequently, the cell which contains
=your_udf( ... ) should also be formatted as a date in the appropriate
style.

Hope this helps.

Pete

On Jan 18, 12:25*am, ep wrote:
Hey Pete,

Thanks for the quick reply. If my udf just returns an int, won't Excel just
auto format it as a number? Is it even possible to return a value that Excel
will recognize and format as a date?

ep



"Pete_UK" wrote:
The UDF should return a number to the cell, which can be formatted as
a date to suit your requirements. The number is an integer measuring
the number of elapsed days, with 1st Jan 1900 = Day 1. Excel assumes
(incorrectly) that 1900 was a leap year, so you need to take this into
account. The serial value for today's date (17th Jan 2008) is 39464.


Hope this helps.


Pete


On Jan 17, 11:31 pm, ep wrote:
Hi all,


I have a c# udf accessed as an automation add-in that needs to return dates
to Excel. I know I can't manipulate the cell's NumberFormat from within the
UDF, but is there a way to return a value that will be formatted in Excel as
a date? It seems any string value I return will default to general. Let me
know if any other info would help.


thanks- Hide quoted text -


- Show quoted text -




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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com