check if myvar is = current date + 3 motnh

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

check if myvar is = current date + 3 motnh

Post by sal21 »

I have this MyVar="01_2012".
How to check if current month date is 01_2012+3 month

Example:
current month date is 02/04/2012 ok, the chek date is true

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: check if myvar is = current date + 3 motnh

Post by HansV »

Like this:

Code: Select all

    Dim arrParts
    Dim d As Date
    arrParts = Split(MyVar, "_")
    d = DateSerial(arrParts(1), arrParts(0), 1)
    If DateDiff("m", d, Date) = 3 Then
        ' Difference is 3 months
    Else
        ' Difference is not 3 months
    End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: check if myvar is = current date + 3 motnh

Post by sal21 »

HansV wrote:Like this:

Code: Select all

    Dim arrParts
    Dim d As Date
    arrParts = Split(MyVar, "_")
    d = DateSerial(arrParts(1), arrParts(0), 1)
    If DateDiff("m", d, Date) = 3 Then
        ' Difference is 3 months
    Else
        ' Difference is not 3 months
    End If

arrrrrggggg.........

Based this code i need to subtract 3 month and return TRE_MESI=01_2012

Sub MENO_TRE_MESI()

Dim arrParts() As String, MY_VAR As String
Dim D As Date, TRE_MESI

MY_VAR = "03_2012"
arrParts = Split(MY_VAR, "_")
D = DateSerial(arrParts(1), arrParts(0), 1)

TRE_MESI = Format(D, "MM_YYYY")

End Sub

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: check if myvar is = current date + 3 motnh

Post by HansV »

But if MY_VAR represents March 2012, and if you subtract 3 months, shouldn't you get December 2011, i.e. TRE_MESI = "12_2011"?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: check if myvar is = current date + 3 motnh

Post by sal21 »

HansV wrote:But if MY_VAR represents March 2012, and if you subtract 3 months, shouldn't you get December 2011, i.e. TRE_MESI = "12_2011"?
yes! Perfect... :clapping:

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: check if myvar is = current date + 3 motnh

Post by HansV »

Change

D = DateSerial(arrParts(1), arrParts(0), 1)

to

D = DateSerial(arrParts(1), arrParts(0) - 3, 1)
Best wishes,
Hans