Solved

Access 2010 VBA Function Calculate Workdays

Posted on 2014-12-02
6
1,062 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
[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
  • 3
  • 3
6 Comments
 
LVL 58
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 58
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
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)

 
LVL 58

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

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)

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

688 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