Solved

Find 3rd working day of the following month for a given date

Posted on 2014-01-09
14
442 Views
Last Modified: 2014-01-10
I need to calculate three (3) working (or week) days following the last day of the month for a given date. For example, if WEDate = 1/3/14, the result should be 2/5/14 (3rd working (or week) day of the following month. (I'm using the following to return the last day of WEDate's month: LDOM: DateSerial(Year([WEDate]),Month([WEDate])+1,0). Any help is greatly appreciated. Thank you.
0
Comment
Question by:jaguar5554
  • 7
  • 5
  • 2
14 Comments
 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
Need we take holidays into account?
0
 

Author Comment

by:jaguar5554
Comment Utility
Yes -- my apologies for not including that important detail!
0
 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
US Holidays? Any year? As far I can tell tell, only New Year's Day, Independence Day and Labor day could fall within the first week, but with multiple years, you still have a multitude of possible holidays.
0
 

Author Comment

by:jaguar5554
Comment Utility
US Holidays and current year; that is, current year of the WEDate. Specifically, I only need to see the 3rd working (or week) day of the month following the WEDate being evaluated. Clear as mud? :-)
0
 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
Not really. A date in December will go over to the next year.
You might need a table of US Holidays by year. Which could be as few as 3 dates or as many as you have years of data times three.
0
 

Author Comment

by:jaguar5554
Comment Utility
I see. I'm assuming there is no way for the DateSerial function to specify a day v. weekday?
0
 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
No.  I have a partial solution, but there is a bug which I have to mull over at the local tavern.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
You can use the simple looping function below:

datDateFrom = DateSerial(Year(datDate), Month(datDate) + 1, 1)
datDateNext = DateAddWorkdays(datDateFrom, 3)

Public Function DateAddWorkdays(ByVal datDate As Date, Optional ByVal intWorkdays = 1) As Date

' Add intWorkdays to datDate ignoring weekends and holidays.
'
' 2010-10-07, Cactus Data ApS, CPH

    Dim dbs         As DAO.Database
    Dim rst         As DAO.Recordset
    
    Dim intDays     As Integer
    Dim strDate     As String
    Dim strSQL      As String
    
    strDate = Format(datDate, "yyyy\/mm\/dd")
    
    Set dbs = CurrentDb
    strSQL = "Select Top " & intWorkdays & " HolidayDate From tblHoliday Where HolidayDate >= #" & strDate & "#"
    Set rst = dbs.OpenRecordset(strSQL)
    
    While intDays < intWorkdays
        datDate = DateAdd("d", 1, datDate)
        intDays = intDays + 1
        ' Skip weekends.
        If Weekday(datDate, vbMonday) > Weekday(vbFriday, vbMonday) Then
            datDate = DateAdd("d", 1 + 7 - Weekday(datDate, vbMonday), datDate)
        End If
        ' Skip holidays.
        Do
            rst.FindFirst "HolidayDate = #" & Format(datDate, "yyyy\/mm\/dd") & "#"
            datDate = DateAdd("d", Abs(Not rst.NoMatch), datDate)
        Loop Until rst.NoMatch
    Wend
    
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing
    
    DateAddWorkdays = datDate
    
End Function

Open in new window

Of course, adjust table and field name to those of yours.
/gustav
0
 

Author Comment

by:jaguar5554
Comment Utility
That, gustav, is a beautiful thing! I should visit the tavern more often myself! LOL

I changed datDateNext = DateAddWorkdays(datDateFrom, 3)
to datDateNext = DateAddWorkdays(datDateFrom, 2) and on intial spot check am getting the exact results I'm looking for. All I need to do is update the tblHoliday table at the beginning of the new year. Genius. Thank you so much!
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
Great! You are welcome.

/gustav
0
 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
Just to give credit-I visited the tavern. Did not come up with a solution. Gustav may or may not have visited a tavern.
0
 

Author Comment

by:jaguar5554
Comment Utility
Scratch that! I changed it back to datDateNext = DateAddWorkdays(datDateFrom, 3)
(the data I was testing was entered for the wrong year!!). Your original expression works perfectly. Thank you Thank you again. Expert!
0
 

Author Closing Comment

by:jaguar5554
Comment Utility
I combed the Internet and none of the solutions I found worked as expected. This solution is flawless. Gustav is an expert and generous in sharing his expertise.
0
 

Author Comment

by:jaguar5554
Comment Utility
jerryb30 -- I hope you had an enjoyable visit at the pub.
gustav -- treat yourself to a visit at the pub.
:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Outlook Free & Paid Tools
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now