View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
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