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

Can someone pick holes in this code as it fails.

ActiveCell.Formula = "=if datepart(""yyyy"",(e2)) =
datepart(""yyyy"",now()+1),date(1,month(e2),day(e2 )),date(0,month(e2),day(e2))"


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Datepart problem

how does it fail?

Tim

--
Tim Williams
Palo Alto, CA


"Ron Dean" wrote in message
. nl...
Can someone pick holes in this code as it fails.

ActiveCell.Formula = "=if datepart(""yyyy"",(e2)) =

datepart(""yyyy"",now()+1),date(1,month(e2),day(e2 )),date(0,month(e2),day(e2
))"




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Datepart problem

On Thu, 5 Jan 2006 20:59:05 +0100, "Ron Dean" wrote:

Can someone pick holes in this code as it fails.

ActiveCell.Formula = "=if datepart(""yyyy"",(e2)) =
datepart(""yyyy"",now()+1),date(1,month(e2),day(e 2)),date(0,month(e2),day(e2))"


DATEPART is not an Excel worksheet function

Maybe you could replace it with the YEAR worksheet function.

Also, by entering either a 1 or a 0 as the first argument of the DATE function,
this will give you either 1900 or 1901. Depending on what you are doing with
this data, you may run into a problem as Excel believes that 29 Feb 1900 is a
legitimate date, and that there are 366 days in 1900.


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Datepart problem

I'm using it in Excel VBA to sort previous and next year's dates.

Even using this simple code I get "application-defined or object-defined
error"

ActiveCell.Formula = "=if datepart((""yyyy"",[e2]) = ""00"",""N"",""y"""")"

I think I have braces in the wrong place

thanks

++++++++++++++++++++++++++

"Ron Rosenfeld" wrote in message
...
On Thu, 5 Jan 2006 20:59:05 +0100, "Ron Dean" wrote:

Can someone pick holes in this code as it fails.

ActiveCell.Formula = "=if datepart(""yyyy"",(e2)) =
datepart(""yyyy"",now()+1),date(1,month(e2),day( e2)),date(0,month(e2),day(e2))"


DATEPART is not an Excel worksheet function

Maybe you could replace it with the YEAR worksheet function.

Also, by entering either a 1 or a 0 as the first argument of the DATE
function,
this will give you either 1900 or 1901. Depending on what you are doing
with
this data, you may run into a problem as Excel believes that 29 Feb 1900
is a
legitimate date, and that there are 366 days in 1900.


--ron



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Datepart problem

Sorry that should have read "I am trying to sort old and next year's dates
on month and day

==============
"Ron Dean" wrote in message
. nl...
I'm using it in Excel VBA to sort previous and next year's dates.

Even using this simple code I get "application-defined or object-defined
error"

ActiveCell.Formula = "=if datepart((""yyyy"",[e2]) =
""00"",""N"",""y"""")"

I think I have braces in the wrong place

thanks

++++++++++++++++++++++++++

"Ron Rosenfeld" wrote in message
...
On Thu, 5 Jan 2006 20:59:05 +0100, "Ron Dean" wrote:

Can someone pick holes in this code as it fails.

ActiveCell.Formula = "=if datepart(""yyyy"",(e2)) =
datepart(""yyyy"",now()+1),date(1,month(e2),day (e2)),date(0,month(e2),day(e2))"


DATEPART is not an Excel worksheet function

Maybe you could replace it with the YEAR worksheet function.

Also, by entering either a 1 or a 0 as the first argument of the DATE
function,
this will give you either 1900 or 1901. Depending on what you are doing
with
this data, you may run into a problem as Excel believes that 29 Feb 1900
is a
legitimate date, and that there are 366 days in 1900.


--ron







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Datepart problem

On Fri, 6 Jan 2006 10:48:50 +0100, "Ron Dean" wrote:

I'm using it in Excel VBA to sort previous and next year's dates.

Even using this simple code I get "application-defined or object-defined
error"

ActiveCell.Formula = "=if datepart((""yyyy"",[e2]) = ""00"",""N"",""y"""")"

I think I have braces in the wrong place

thanks


Maybe I am not understanding what you are trying to do.

What you ARE doing is setting the Formula property of ActiveCell. In other
words, your statement will write the contents of that string into whatever the
ActiveCell is in your Active Worksheet.

That will not work for the reasons I previously stated -- the formula is not
valid within Excel.

If you really want to do that, the proper format of your statement is:

ActiveCell.Formula = "=If(DatePart(""yyyy"", e2) = ""00"", ""N"", ""y"")"

What that will do is write your formula into ActiveCell. ActiveCell will then
show a #NAME? error since DatePart is not a valid Excel worksheet function.


--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Datepart problem

Point taken, I am now using YEAR and it works fine.
However if you go to HELP in Excel VBA there is a description of DatePart
Function. This is what I based my formula on.

Many thanks Ron

+++++++++++++++++
"Ron Rosenfeld" wrote in message
...
On Fri, 6 Jan 2006 10:48:50 +0100, "Ron Dean"
wrote:

I'm using it in Excel VBA to sort previous and next year's dates.

Even using this simple code I get "application-defined or object-defined
error"

ActiveCell.Formula = "=if datepart((""yyyy"",[e2]) =
""00"",""N"",""y"""")"

I think I have braces in the wrong place

thanks


Maybe I am not understanding what you are trying to do.

What you ARE doing is setting the Formula property of ActiveCell. In
other
words, your statement will write the contents of that string into whatever
the
ActiveCell is in your Active Worksheet.

That will not work for the reasons I previously stated -- the formula is
not
valid within Excel.

If you really want to do that, the proper format of your statement is:

ActiveCell.Formula = "=If(DatePart(""yyyy"", e2) = ""00"", ""N"", ""y"")"

What that will do is write your formula into ActiveCell. ActiveCell will
then
show a #NAME? error since DatePart is not a valid Excel worksheet
function.


--ron



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Datepart problem

On Fri, 6 Jan 2006 14:26:39 +0100, "Ron Dean" wrote:

Point taken, I am now using YEAR and it works fine.
However if you go to HELP in Excel VBA there is a description of DatePart
Function. This is what I based my formula on.


DatePart is a valid **VBA** function. It is NOT an Excel *Worksheet* function.

I'm glad you've got it working.


--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
Colon at the end of excel file name(ex: problem.xls:1, problem.xls financeguy New Users to Excel 2 January 15th 10 01:15 AM
Started out as an Access problem. Now an Excel problem RobertM Excel Discussion (Misc queries) 2 April 26th 06 07:30 PM
problem with a conditional max problem Brian Cornejo Excel Discussion (Misc queries) 1 February 18th 05 06:25 PM
Problem when multipple users access shared xl-file at the same time, macrocode for solve this problem? OCI Excel Programming 0 May 16th 04 10:40 PM


All times are GMT +1. The time now is 06:44 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"