ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Convert Date (https://www.excelbanter.com/excel-programming/362792-convert-date.html)

StephanieH

Convert Date
 
I have a macro that picks up a date in cell and adds a month to it. Now I
need to take the first date it picked up and create two new variables using
that date. The first variable I need is to simply pick out the year from the
first date Ie if FirstDate = 3/1/2006, I need myYear to be "2006". The
second variable I need to set is the FirstDate in YYYY_MM format, ie
"2006_03".

How do you convert the date to these two formats? I've tried
myYear = FirstDate(format, "YYYY") but get "Argument not optional"
and
myYear = FirstDate&format("YYYY") but get "Syntax error"

Andrew Taylor

Convert Date
 
myYear = Format(FirstDate, "YYYY")
mySecondVariable = Format(FirstDate, "YYYY_MM")


StephanieH wrote:
I have a macro that picks up a date in cell and adds a month to it. Now I
need to take the first date it picked up and create two new variables using
that date. The first variable I need is to simply pick out the year from the
first date Ie if FirstDate = 3/1/2006, I need myYear to be "2006". The
second variable I need to set is the FirstDate in YYYY_MM format, ie
"2006_03".

How do you convert the date to these two formats? I've tried
myYear = FirstDate(format, "YYYY") but get "Argument not optional"
and
myYear = FirstDate&format("YYYY") but get "Syntax error"



Ardus Petus

Convert Date
 
'----------------------------------------------------
Dim myYear as string
dim MyYearMonth as string

myYear = format(FirstDate,"yyyy")
myYearMonth =f ormat(FirstDate,"yyyy_mm")
'----------------------------------------------------

HTH
--
AP

"StephanieH" a écrit dans le message
de news: ...
I have a macro that picks up a date in cell and adds a month to it. Now I
need to take the first date it picked up and create two new variables
using
that date. The first variable I need is to simply pick out the year from
the
first date Ie if FirstDate = 3/1/2006, I need myYear to be "2006". The
second variable I need to set is the FirstDate in YYYY_MM format, ie
"2006_03".

How do you convert the date to these two formats? I've tried
myYear = FirstDate(format, "YYYY") but get "Argument not optional"
and
myYear = FirstDate&format("YYYY") but get "Syntax error"




StephanieH

Convert Date
 
Yay. It works perfectly.

Thanks for your help Ardus.



"Ardus Petus" wrote:

'----------------------------------------------------
Dim myYear as string
dim MyYearMonth as string

myYear = format(FirstDate,"yyyy")
myYearMonth =f ormat(FirstDate,"yyyy_mm")
'----------------------------------------------------

HTH
--
AP

"StephanieH" a écrit dans le message
de news: ...
I have a macro that picks up a date in cell and adds a month to it. Now I
need to take the first date it picked up and create two new variables
using
that date. The first variable I need is to simply pick out the year from
the
first date Ie if FirstDate = 3/1/2006, I need myYear to be "2006". The
second variable I need to set is the FirstDate in YYYY_MM format, ie
"2006_03".

How do you convert the date to these two formats? I've tried
myYear = FirstDate(format, "YYYY") but get "Argument not optional"
and
myYear = FirstDate&format("YYYY") but get "Syntax error"






All times are GMT +1. The time now is 11:24 AM.

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