Solved

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

Posted on 2014-01-09
14
470 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
ID: 39769076
Need we take holidays into account?
0
 

Author Comment

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

Expert Comment

by:jerryb30
ID: 39769115
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:jaguar5554
ID: 39769127
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
ID: 39769141
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
ID: 39769277
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
ID: 39769634
No.  I have a partial solution, but there is a bug which I have to mull over at the local tavern.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39770603
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
ID: 39771246
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
ID: 39771254
Great! You are welcome.

/gustav
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 39771264
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
ID: 39771269
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
ID: 39771284
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
ID: 39771292
jerryb30 -- I hope you had an enjoyable visit at the pub.
gustav -- treat yourself to a visit at the pub.
:-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

830 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