I have a date in a cell in excel formatted as dd/mm/yyyy. I need to calculate 2 working days before that date and insert in another cell. The working days are Mon - Fri. Mon and Tue would only be the problem days. Is this something MS can do?.
Thx,
John
Dates
Moderators: Dorian (MJT support), JRL
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Here's a function that should do the job:
Code: Select all
VBSTART
Function GetWorkingDay(thedate,numdaysbefore)
'subtract 2 days from the date
thedate = DateAdd("d",-2,thedate)
MsgBox thedate
'if result is a Sunday, subract 2 days
if WeekDay(thedate) = 1 then
thedate = DateAdd("d",-2,thedate)
end if
'if result is a Saturday, subract 1 day
if WeekDay(thedate) = 7 then
thedate = DateAdd("d",-1,thedate)
end if
GetWorkingDay = thedate
End Function
VBEND
//example - should return 31st March
VBEval>GetWorkingDay("04/02/2008",2),newdate
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
That code returns Friday for Monday, it may be open to interpretation but I think it should subtract 2 days for both the Sunday and Saturday result:
[code]
VBSTART
Function GetWorkingDay(thedate,numdaysbefore)
'subtract 2 days from the date
thedate = DateAdd("d",-2,thedate)
MsgBox thedate
'if result is a Sunday, or Saturday subtract 2 days
if WeekDay(thedate) = 1 or 7 then
thedate = DateAdd("d",-2,thedate)
end if
GetWorkingDay = thedate
End Function
VBEND
//example - should return Thursday 4/3
VBEval>GetWorkingDay("04/07/2008",2),newdate
MDL>New Date is: %newdate%
[/code]
[code]
VBSTART
Function GetWorkingDay(thedate,numdaysbefore)
'subtract 2 days from the date
thedate = DateAdd("d",-2,thedate)
MsgBox thedate
'if result is a Sunday, or Saturday subtract 2 days
if WeekDay(thedate) = 1 or 7 then
thedate = DateAdd("d",-2,thedate)
end if
GetWorkingDay = thedate
End Function
VBEND
//example - should return Thursday 4/3
VBEval>GetWorkingDay("04/07/2008",2),newdate
MDL>New Date is: %newdate%
[/code]