?
Solved

Access 2010 VBA Function Calculate Workdays

Posted on 2014-12-02
6
Medium Priority
?
1,085 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
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.

 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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.

762 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