Solved

Access 2010 VBA Function Calculate Workdays

Posted on 2014-12-02
6
968 Views
Last Modified: 2014-12-02
I'm using the code below to calculate workdays and I'm try to figure out how to incorporate holidays in the function. Any suggestions??

 Dim WholeWeeks As Variant
      Dim DateCnt As Variant
      Dim EndDays As Integer

      BegDate = DateValue(BegDate)
      EndDate = DateValue(EndDate)
      WholeWeeks = DateDiff("w", BegDate, EndDate)
      DateCnt = DateAdd("ww", WholeWeeks, BegDate)
      EndDays = 0
      Do While DateCnt < EndDate
         If Format(DateCnt, "ddd") <> "Sun" And _
                          Format(DateCnt, "ddd") <> "Sat" Then
            EndDays = EndDays + 1
         End If
         DateCnt = DateAdd("d", 1, DateCnt)
      Loop
      Work_Days = WholeWeeks * 5 + EndDays
0
Comment
Question by:shieldsco
  • 3
  • 3
6 Comments
 
LVL 57
ID: 40476783
You need to add a table of holidays, then check the dates as you add if it's a holiday or not.  Only way to do it.

You can optimize a bit; get a recordset of holidays between start and end, sorted in ascending sequence.  Then check on each iteration of the loop, and if = skip and fetch the next holiday record.

Jim.
0
 
LVL 57
ID: 40476791
the other way to do it of course is to simply count the number of holidays between start and end and subtract at the end.

Jim.
0
 

Author Comment

by:shieldsco
ID: 40476809
what would the code look like incorporated into the aforementioned code?
0
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.

 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40476843
Work_Days = WholeWeeks * 5 + EndDays  - Dcount("*","tblHolidays","[HolidayDate] Between #" & Format$(BegDate,"MM/DD/YY")  & "#  AND #" & Format$(EndDate,"MM/DD/YY") & "#")

 assuming tblHolidays only contains dates that are not Saturday or Sunday.

Jim.
0
 

Author Comment

by:shieldsco
ID: 40476896
Jim let me test
0
 

Author Closing Comment

by:shieldsco
ID: 40476993
Good Job Jim....Thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

867 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

18 Experts available now in Live!

Get 1:1 Help Now