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

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default variable for Date

I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth equals is
VBA.

In worksheet formulas the following returns the values you want.

Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())

Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))

In VBA. [Note that Date replaces TODAY()]

CurrMth = Month(Date) & " " & Year(Date)

PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
& " " & Year(WorksheetFunction.EDate(Date, -1))

However, WorksheetFunction.EDate does not work in VBA in XL2002. Not sure
about xl2003 but it does work in xl2007.

--
Regards,

OssieMac


"Ranjit kurian" wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default variable for Date

Some more answers that might be closer to what you want.

On worksheet
Current Month and Year

=TEXT(TODAY(),"mmm/yy")

Previous Month and Year

=TEXT(EDATE(TODAY(),-1),"mmm/yy")

in VBA

CurrMth = Format(Date, "mmm/yy")
PrevMth = Format(WorksheetFunction.EDate(Date, -1), "mmm/yy")

Note again that WorksheetFunction.EDate does not work in VBA in some earlier
versions of XL.

--
Regards,

OssieMac


"OssieMac" wrote:

I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth equals is
VBA.

In worksheet formulas the following returns the values you want.

Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())

Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))

In VBA. [Note that Date replaces TODAY()]

CurrMth = Month(Date) & " " & Year(Date)

PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
& " " & Year(WorksheetFunction.EDate(Date, -1))

However, WorksheetFunction.EDate does not work in VBA in XL2002. Not sure
about xl2003 but it does work in xl2007.

--
Regards,

OssieMac


"Ranjit kurian" wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default variable for Date

The EDate worksheetfunction isn't available in XL2003 VBA, please use
this instead:

Dim CurrMth As String
CurrMth = Format$(DateAdd("m", -1, Date), "mmm/yy")

Note: "Date" returns the current date.
Also note: use "Format$" instead of "Format" when you are using the
returned value as a string, this version of the function is quicker.

best regards
Peder Schmedling

On Jan 23, 5:54*am, OssieMac
wrote:
Some more answers that might be closer to what you want.

On worksheet
Current Month and Year

=TEXT(TODAY(),"mmm/yy")

Previous Month and Year

=TEXT(EDATE(TODAY(),-1),"mmm/yy")

in VBA

CurrMth = Format(Date, "mmm/yy")
PrevMth = Format(WorksheetFunction.EDate(Date, -1), "mmm/yy")

Note again that WorksheetFunction.EDate does not work in VBA in some earlier
versions of XL.

--
Regards,

OssieMac

"OssieMac" wrote:
I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth equals is
VBA.


In worksheet formulas the following returns the values you want.


Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())


Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))


In VBA. [Note that Date replaces TODAY()]


CurrMth = Month(Date) & " " & Year(Date)


PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
* * & " " & Year(WorksheetFunction.EDate(Date, -1))


However, WorksheetFunction.EDate does not work in VBA in XL2002. Not sure
about xl2003 but it does work in xl2007.


--
Regards,


OssieMac


"Ranjit kurian" wrote:


I would like to give a variable for my date column, can some one advise me


Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)


CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 83
Default variable for Date

Iam working in VBA.

Iam getting an error(object doesn't support) for the previous month Code.

Iam using an excel VBA, in excel i have a column called as MyDate which
contains only months like Jan/09, Dec/08, Nov/08,Oct/08 etc... i have
changed the format manually to mmm/yy format, but when i go to each cell its
still in the format of m/d/yyyy, because of which the vba code is giving an
error to me, as my date are not fixed, i need to change the formate of excel
into mmm/yy

Iam using the variables to an IF funtions, if the CurrMth is Jan/09 then the
result should be Yes, if the PrevMth is Dec-08 the result should be AYes


"OssieMac" wrote:

Some more answers that might be closer to what you want.

On worksheet
Current Month and Year

=TEXT(TODAY(),"mmm/yy")

Previous Month and Year

=TEXT(EDATE(TODAY(),-1),"mmm/yy")

in VBA

CurrMth = Format(Date, "mmm/yy")
PrevMth = Format(WorksheetFunction.EDate(Date, -1), "mmm/yy")

Note again that WorksheetFunction.EDate does not work in VBA in some earlier
versions of XL.

--
Regards,

OssieMac


"OssieMac" wrote:

I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth equals is
VBA.

In worksheet formulas the following returns the values you want.

Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())

Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))

In VBA. [Note that Date replaces TODAY()]

CurrMth = Month(Date) & " " & Year(Date)

PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
& " " & Year(WorksheetFunction.EDate(Date, -1))

However, WorksheetFunction.EDate does not work in VBA in XL2002. Not sure
about xl2003 but it does work in xl2007.

--
Regards,

OssieMac


"Ranjit kurian" wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default variable for Date

What version of Excel are you using?

--
Regards,

OssieMac


"Ranjit kurian" wrote:

Iam working in VBA.

Iam getting an error(object doesn't support) for the previous month Code.

Iam using an excel VBA, in excel i have a column called as MyDate which
contains only months like Jan/09, Dec/08, Nov/08,Oct/08 etc... i have
changed the format manually to mmm/yy format, but when i go to each cell its
still in the format of m/d/yyyy, because of which the vba code is giving an
error to me, as my date are not fixed, i need to change the formate of excel
into mmm/yy

Iam using the variables to an IF funtions, if the CurrMth is Jan/09 then the
result should be Yes, if the PrevMth is Dec-08 the result should be AYes


"OssieMac" wrote:

Some more answers that might be closer to what you want.

On worksheet
Current Month and Year

=TEXT(TODAY(),"mmm/yy")

Previous Month and Year

=TEXT(EDATE(TODAY(),-1),"mmm/yy")

in VBA

CurrMth = Format(Date, "mmm/yy")
PrevMth = Format(WorksheetFunction.EDate(Date, -1), "mmm/yy")

Note again that WorksheetFunction.EDate does not work in VBA in some earlier
versions of XL.

--
Regards,

OssieMac


"OssieMac" wrote:

I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth equals is
VBA.

In worksheet formulas the following returns the values you want.

Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())

Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))

In VBA. [Note that Date replaces TODAY()]

CurrMth = Month(Date) & " " & Year(Date)

PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
& " " & Year(WorksheetFunction.EDate(Date, -1))

However, WorksheetFunction.EDate does not work in VBA in XL2002. Not sure
about xl2003 but it does work in xl2007.

--
Regards,

OssieMac


"Ranjit kurian" wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 83
Default variable for Date

Iam using Excel 2003.

"OssieMac" wrote:

What version of Excel are you using?

--
Regards,

OssieMac


"Ranjit kurian" wrote:

Iam working in VBA.

Iam getting an error(object doesn't support) for the previous month Code.

Iam using an excel VBA, in excel i have a column called as MyDate which
contains only months like Jan/09, Dec/08, Nov/08,Oct/08 etc... i have
changed the format manually to mmm/yy format, but when i go to each cell its
still in the format of m/d/yyyy, because of which the vba code is giving an
error to me, as my date are not fixed, i need to change the formate of excel
into mmm/yy

Iam using the variables to an IF funtions, if the CurrMth is Jan/09 then the
result should be Yes, if the PrevMth is Dec-08 the result should be AYes


"OssieMac" wrote:

Some more answers that might be closer to what you want.

On worksheet
Current Month and Year

=TEXT(TODAY(),"mmm/yy")

Previous Month and Year

=TEXT(EDATE(TODAY(),-1),"mmm/yy")

in VBA

CurrMth = Format(Date, "mmm/yy")
PrevMth = Format(WorksheetFunction.EDate(Date, -1), "mmm/yy")

Note again that WorksheetFunction.EDate does not work in VBA in some earlier
versions of XL.

--
Regards,

OssieMac


"OssieMac" wrote:

I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth equals is
VBA.

In worksheet formulas the following returns the values you want.

Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())

Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))

In VBA. [Note that Date replaces TODAY()]

CurrMth = Month(Date) & " " & Year(Date)

PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
& " " & Year(WorksheetFunction.EDate(Date, -1))

However, WorksheetFunction.EDate does not work in VBA in XL2002. Not sure
about xl2003 but it does work in xl2007.

--
Regards,

OssieMac


"Ranjit kurian" wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default variable for Date

A date contains a day, a month, a year, and a time of day (midnight unless
specified). So a date can not contain a full month. You can format the
display (a worksheet cell, a messagebox, ...) to hide the day and time but
it is still there. You can in fact format the date as mostly anything, it
will not change the real date content.

Likewise, "Jan/09" is not a date because it has no day. It will never equal
a date variable, it might equal a string containing a formatted date
display, but if so you would depend too much on regional settings for my
european taste.

You must either work with real dates and compare its month and year
components, or convert the date to a string to compare it with another
sting. I would prefer to work with real dates, as you can do real math with
them. Strings are prety stupid, and depending on -oh, I wrote that already.

I can't find your code samples, so I can't provide working suggestions.

HTH. Best wishes Harald

"Ranjit kurian" skrev i melding
...
Iam working in VBA.

Iam getting an error(object doesn't support) for the previous month Code.

Iam using an excel VBA, in excel i have a column called as MyDate which
contains only months like Jan/09, Dec/08, Nov/08,Oct/08 etc... i have
changed the format manually to mmm/yy format, but when i go to each cell
its
still in the format of m/d/yyyy, because of which the vba code is giving
an
error to me, as my date are not fixed, i need to change the formate of
excel
into mmm/yy

Iam using the variables to an IF funtions, if the CurrMth is Jan/09 then
the
result should be Yes, if the PrevMth is Dec-08 the result should be AYes


"OssieMac" wrote:

Some more answers that might be closer to what you want.

On worksheet
Current Month and Year

=TEXT(TODAY(),"mmm/yy")

Previous Month and Year

=TEXT(EDATE(TODAY(),-1),"mmm/yy")

in VBA

CurrMth = Format(Date, "mmm/yy")
PrevMth = Format(WorksheetFunction.EDate(Date, -1), "mmm/yy")

Note again that WorksheetFunction.EDate does not work in VBA in some
earlier
versions of XL.

--
Regards,

OssieMac


"OssieMac" wrote:

I am not sure if you want to work in VBA code or in worksheet formulas
because TODAY() is a worksheet formula and your CurrMth and PrevMth
equals is
VBA.

In worksheet formulas the following returns the values you want.

Current month and year
= MONTH(TODAY()) & " " & YEAR(TODAY())

Previous month and year
=MONTH(EDATE(TODAY(),-1)) & " " & YEAR(EDATE(TODAY(),-1))

In VBA. [Note that Date replaces TODAY()]

CurrMth = Month(Date) & " " & Year(Date)

PrevMth = Month(WorksheetFunction.EDate(Date, -1)) _
& " " & Year(WorksheetFunction.EDate(Date, -1))

However, WorksheetFunction.EDate does not work in VBA in XL2002. Not
sure
about xl2003 but it does work in xl2007.

--
Regards,

OssieMac


"Ranjit kurian" wrote:

I would like to give a variable for my date column, can some one
advise me

Example:
I have column in this formate mmm/yy, my below variable should
identify the
Month & Year, i need to variables one is for current month(Jan/09),
and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default variable for Date

In code:

Dim CurMth As String
Dim PrvMth As String

CurMth = Format(Date, "mmyyyy")
PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyyyy")

MsgBox CurMth & vbLf & PrvMth



I like 2 digit months. You may not want that.

Ranjit kurian wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 83
Default variable for Date

Thanks its working fine, but i have a doubt on PrvMth

Suppose if my CurMth is Feb/09, will the PrvMth give the result as Jan/09

"Dave Peterson" wrote:

In code:

Dim CurMth As String
Dim PrvMth As String

CurMth = Format(Date, "mmyyyy")
PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyyyy")

MsgBox CurMth & vbLf & PrvMth



I like 2 digit months. You may not want that.

Ranjit kurian wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())


--

Dave Peterson



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default variable for Date

I'm gonna say yes.

Dim CurMth As String
Dim PrvMth As String
Dim myDate As Date

myDate = DateSerial(2009, 2, 1) 'Use Date for today's date.

CurMth = Format(myDate, "mmyyyy")
PrvMth = Format(DateSerial(Year(myDate), Month(myDate) - 1, 1), "mmyyyy")

MsgBox CurMth & vbLf & PrvMth

=========
But read the response from Harald Staff. I'm not sure how the data in your
worksheet gets used in the code.

Ranjit kurian wrote:

Thanks its working fine, but i have a doubt on PrvMth

Suppose if my CurMth is Feb/09, will the PrvMth give the result as Jan/09

"Dave Peterson" wrote:

In code:

Dim CurMth As String
Dim PrvMth As String

CurMth = Format(Date, "mmyyyy")
PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyyyy")

MsgBox CurMth & vbLf & PrvMth



I like 2 digit months. You may not want that.

Ranjit kurian wrote:

I would like to give a variable for my date column, can some one advise me

Example:
I have column in this formate mmm/yy, my below variable should identify the
Month & Year, i need to variables one is for current month(Jan/09), and the
other for Previous Month(Dec/08)

CurrMth = Month(Today()) & Year(Today())
PrvMth = Month(Today()) - 1 & Year(Today())


--

Dave Peterson


--

Dave Peterson
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
add 1 day to date variable DaveMZ[_2_] Excel Programming 1 April 10th 07 05:50 PM
Setting a Date variable scott Excel Programming 4 January 28th 05 01:12 AM
Assign a Date Variable within VBA Q John Excel Programming 9 December 21st 04 04:46 PM
calc constant date from variable date & return with ability to rn. SusieQ'sQuest Excel Worksheet Functions 1 November 9th 04 08:51 PM
Compare Date in Cell VS a Variable Date to Hide Row JimI Excel Programming 3 October 10th 04 04:26 PM


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

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"