PC Review


Reply
Thread Tools Rate Thread

Convert Date

 
 
Ranjit kurian
Guest
Posts: n/a
 
      19th Mar 2009
From the below code iam trying to compare my active cell with previous date
, if the active cell is less than previous month then the result need to be
Yes, else No.

The problem iam facing from the below code in variable mmyy, when i run
macro this variable is shown as Text inspite of Date because of which its not
able to compare with previous date.

Example: my date column is copied from MSAccess so the date is in exact this
formate SEP-08

sub test()
PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
ActiveCell.Offset(1, 0).Select
a = ActiveCell
m = DatePart("m", a)
If (m < 10) Then
m = 0 & m
End If
y = Right(a, 2)
mmyy = m & y
MsgBox mmyy & vbLf & PrvMth
If mmyy < PrvMth Then
ActiveCell.Offset(0, 1) = "Yes"
Else
ActiveCell.Offset(-1, 0).Select
End If
End If
End If
End sub
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      19th Mar 2009
You can get the previous month from the Date function using the Month
function...

PreviousMonth = Month(Date) - 1

And assuming the ActiveCell contains a real date, you can get its month the
same way...

MonthForActiveCell = Month(Range(ActiveCell.Value))

Since both of these will be numbers, you can compare them directly...

If MonthForActiveCell < PreviousMonth Then

--
Rick (MVP - Excel)


"Ranjit kurian" <(E-Mail Removed)> wrote in message
news:5E62248F-1451-4FB8-81AC-(E-Mail Removed)...
> From the below code iam trying to compare my active cell with previous
> date
> , if the active cell is less than previous month then the result need to
> be
> Yes, else No.
>
> The problem iam facing from the below code in variable mmyy, when i run
> macro this variable is shown as Text inspite of Date because of which its
> not
> able to compare with previous date.
>
> Example: my date column is copied from MSAccess so the date is in exact
> this
> formate SEP-08
>
> sub test()
> PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
> ActiveCell.Offset(1, 0).Select
> a = ActiveCell
> m = DatePart("m", a)
> If (m < 10) Then
> m = 0 & m
> End If
> y = Right(a, 2)
> mmyy = m & y
> MsgBox mmyy & vbLf & PrvMth
> If mmyy < PrvMth Then
> ActiveCell.Offset(0, 1) = "Yes"
> Else
> ActiveCell.Offset(-1, 0).Select
> End If
> End If
> End If
> End sub


 
Reply With Quote
 
Ranjit kurian
Guest
Posts: n/a
 
      20th Mar 2009
Hi Rick,

Thanks for the reply, but the below code "MonthForActiveCell =
Month(Range(ActiveCell.Value))" was not wroking for me, so when i removed the
Range from the code "mmyy = Month(ActiveCell.Value) & Year(ActiveCell.Value)"
its working fine, but if my date is as of Dec-2008 it will show it as 122009,
pls advise ...




"Rick Rothstein" wrote:

> You can get the previous month from the Date function using the Month
> function...
>
> PreviousMonth = Month(Date) - 1
>
> And assuming the ActiveCell contains a real date, you can get its month the
> same way...
>
> MonthForActiveCell = Month(Range(ActiveCell.Value))
>
> Since both of these will be numbers, you can compare them directly...
>
> If MonthForActiveCell < PreviousMonth Then
>
> --
> Rick (MVP - Excel)
>
>
> "Ranjit kurian" <(E-Mail Removed)> wrote in message
> news:5E62248F-1451-4FB8-81AC-(E-Mail Removed)...
> > From the below code iam trying to compare my active cell with previous
> > date
> > , if the active cell is less than previous month then the result need to
> > be
> > Yes, else No.
> >
> > The problem iam facing from the below code in variable mmyy, when i run
> > macro this variable is shown as Text inspite of Date because of which its
> > not
> > able to compare with previous date.
> >
> > Example: my date column is copied from MSAccess so the date is in exact
> > this
> > formate SEP-08
> >
> > sub test()
> > PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
> > ActiveCell.Offset(1, 0).Select
> > a = ActiveCell
> > m = DatePart("m", a)
> > If (m < 10) Then
> > m = 0 & m
> > End If
> > y = Right(a, 2)
> > mmyy = m & y
> > MsgBox mmyy & vbLf & PrvMth
> > If mmyy < PrvMth Then
> > ActiveCell.Offset(0, 1) = "Yes"
> > Else
> > ActiveCell.Offset(-1, 0).Select
> > End If
> > End If
> > End If
> > End sub

>
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      20th Mar 2009
Yes, the I added the Range call by mistake. I'll show you how to get the
mmyy value you want, but first I want to reiterate that you don't need that
in order to test if...

MonthForActiveCell < PreviousMonth

the If..Then test I gave you will do that test (once the erroneous Range
call is removed)...

PreviousMonth = Month(Date) - 1
MonthForActiveCell = Month(ActiveCell.Value)
If MonthForActiveCell < PreviousMonth Then
' Put "active cell date less than previous month" code here
End If

Okay, just in case you want to do it the way you started out...

mmyyy = Format(Month(ActiveCell.Value), "00") & _
Format(ActiveCell.Value, "yy")

--
Rick (MVP - Excel)


"Ranjit kurian" <(E-Mail Removed)> wrote in message
news:491FC4BB-722D-48A6-BEC4-(E-Mail Removed)...
> Hi Rick,
>
> Thanks for the reply, but the below code "MonthForActiveCell =
> Month(Range(ActiveCell.Value))" was not wroking for me, so when i removed
> the
> Range from the code "mmyy = Month(ActiveCell.Value) &
> Year(ActiveCell.Value)"
> its working fine, but if my date is as of Dec-2008 it will show it as
> 122009,
> pls advise ...
>
>
>
>
> "Rick Rothstein" wrote:
>
>> You can get the previous month from the Date function using the Month
>> function...
>>
>> PreviousMonth = Month(Date) - 1
>>
>> And assuming the ActiveCell contains a real date, you can get its month
>> the
>> same way...
>>
>> MonthForActiveCell = Month(Range(ActiveCell.Value))
>>
>> Since both of these will be numbers, you can compare them directly...
>>
>> If MonthForActiveCell < PreviousMonth Then
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "Ranjit kurian" <(E-Mail Removed)> wrote in message
>> news:5E62248F-1451-4FB8-81AC-(E-Mail Removed)...
>> > From the below code iam trying to compare my active cell with previous
>> > date
>> > , if the active cell is less than previous month then the result need
>> > to
>> > be
>> > Yes, else No.
>> >
>> > The problem iam facing from the below code in variable mmyy, when i run
>> > macro this variable is shown as Text inspite of Date because of which
>> > its
>> > not
>> > able to compare with previous date.
>> >
>> > Example: my date column is copied from MSAccess so the date is in exact
>> > this
>> > formate SEP-08
>> >
>> > sub test()
>> > PrvMth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
>> > ActiveCell.Offset(1, 0).Select
>> > a = ActiveCell
>> > m = DatePart("m", a)
>> > If (m < 10) Then
>> > m = 0 & m
>> > End If
>> > y = Right(a, 2)
>> > mmyy = m & y
>> > MsgBox mmyy & vbLf & PrvMth
>> > If mmyy < PrvMth Then
>> > ActiveCell.Offset(0, 1) = "Yes"
>> > Else
>> > ActiveCell.Offset(-1, 0).Select
>> > End If
>> > End If
>> > End If
>> > End sub

>>
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert date (m/dd/yyyy) in text format into an excel recognised date Rob P Microsoft Excel Programming 3 30th Apr 2010 12:40 AM
Convert a julian gregorian date code into a regular date =?Utf-8?B?Um9iZXJ0?= Microsoft Excel Worksheet Functions 3 13th Jun 2006 07:03 PM
How to convert normal date to Julian date upto milliseconds precision Rajat Microsoft C# .NET 2 21st Mar 2006 06:06 AM
How to Convert Days(Text field) into Date By giving the start date FA Microsoft Access Forms 3 2nd Dec 2005 08:26 PM
How to convert user-defined custom format date string to date value abcabcabc Microsoft VB .NET 1 19th Aug 2005 12:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:44 AM.