Solved

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

Posted on 2014-01-09
14
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 51

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 51

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

630 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