How to find previous Monday (date) from specific date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Does anybody know how to find previous Monday date from speficied date.
For example myDate = '22-09-2004' and the previous Sunday should be
'13-09-2004'

TIA
Gabriel
 
Gabriel,

It's very easy to do with a user defined function in VBA, only you're not
clear in whether you are looking for the last Monday or Sunday! Anyway, the
idea is:

Function Last_Monday_Before(vDate As Date)
If Weekday(vDate, vbSunday) = 2 Then
Last_Monday_Before = vDate - 7
Else
Last_Monday_Before = vDate - Weekday(vDate, vbSunday) + 2
End If
End Function

Or

Function Last_Sunday_Fefore(vDate As Date)
If Weekday(vDate, vbSunday) = 1 Then
Last_Sunday_Fefore = vDate - 7
Else
Last_Sunday_Fefore = vDate - Weekday(vDate, vbSunday) + 1
End If
End Function

Paste the code in a general module and save. Then you can use the function
like, for instance:

Last_Monday_Before([SomeDateFieldOrControl])

or

Last_Monday_Before(#24/09/2004#)

in any query, form, report, code procedure or macro action argument, and it
wil return the calculated date based on the parameter in the parantheses.

HTH,
Nikos

Last
 
Hi Nikos,

Thank for your ideas, it works greats and very appreciated for your help.

Gabriel

Nikos Yannacopoulos said:
Gabriel,

It's very easy to do with a user defined function in VBA, only you're not
clear in whether you are looking for the last Monday or Sunday! Anyway, the
idea is:

Function Last_Monday_Before(vDate As Date)
If Weekday(vDate, vbSunday) = 2 Then
Last_Monday_Before = vDate - 7
Else
Last_Monday_Before = vDate - Weekday(vDate, vbSunday) + 2
End If
End Function

Or

Function Last_Sunday_Fefore(vDate As Date)
If Weekday(vDate, vbSunday) = 1 Then
Last_Sunday_Fefore = vDate - 7
Else
Last_Sunday_Fefore = vDate - Weekday(vDate, vbSunday) + 1
End If
End Function

Paste the code in a general module and save. Then you can use the function
like, for instance:

Last_Monday_Before([SomeDateFieldOrControl])

or

Last_Monday_Before(#24/09/2004#)

in any query, form, report, code procedure or macro action argument, and it
wil return the calculated date based on the parameter in the parantheses.

HTH,
Nikos

Last
Gabriel said:
Hi

Does anybody know how to find previous Monday date from speficied date.
For example myDate = '22-09-2004' and the previous Sunday should be
'13-09-2004'

TIA
Gabriel
 
Gabriel,

It's very easy to do with a user defined function in VBA, only you're not
clear in whether you are looking for the last Monday or Sunday! Anyway, the
idea is:

Function Last_Monday_Before(vDate As Date)
If Weekday(vDate, vbSunday) = 2 Then
Last_Monday_Before = vDate - 7
Else
Last_Monday_Before = vDate - Weekday(vDate, vbSunday) + 2
End If
End Function

Or

Function Last_Sunday_Fefore(vDate As Date)
If Weekday(vDate, vbSunday) = 1 Then
Last_Sunday_Fefore = vDate - 7
Else
Last_Sunday_Fefore = vDate - Weekday(vDate, vbSunday) + 1
End If
End Function

Paste the code in a general module and save. Then you can use the function
like, for instance:

Last_Monday_Before([SomeDateFieldOrControl])

or

Last_Monday_Before(#24/09/2004#)

in any query, form, report, code procedure or macro action argument, and it
wil return the calculated date based on the parameter in the parantheses.

HTH,
Nikos

Last
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top