Counting Business Days in VBA

Counting business days is easy with the WeekDayCount() and FedHolidays() functions. What's interesting is how we test the WorkingDayCount() function.

Counting Business Days in VBA

In yesterday's article, I published my WeekDayCount() function, which returned the number of weekdays between a starting and ending date.  That function is handicapped by the fact that it does not take into account holidays.  Thus, it is an incomplete attempt to determine the number of business days between two dates.  Let's rectify that now.

Business days

Just to make sure we're all on the same page, let's define what I mean by business days.  For most purposes, a business day is any day except for the following:

  • Saturday
  • Sunday
  • Federal Holiday

Calculating a business day count between two dates

I broke this down into a three step process:

  1. Count the number of days between two dates
  2. Subtract out the Saturdays and Sundays
  3. Subtract out the holidays

I combined steps 1 and 2 into a standalone function named WeekDayCount().  Step 3 is mostly accomplished via the FedHolidays() function I wrote about earlier.  

The FedHolidays() function does not return a count, though.  Instead, it returns a collection of the federal holiday dates between the starting and ending dates we pass to it.  Returning a collection makes the function more flexible than if it only returned a count.  And, since collections provide a .Count property, we can simply and readably return a count if we so choose.

Verifying the calculation

There are ten US federal holiday observances every year:

  • New Year's Day
  • Birthday of Martin Luther King, Jr.
  • Washington's Birthday
  • Memorial Day
  • Independence Day
  • Labor Day
  • Columbus Day
  • Veterans Day
  • Thanksgiving Day
  • Christmas Day

There are 52 weeks in a year.  There are two non-business days per week, which gives us 104 Saturdays/Sundays per year.

There are 365 days in a non-leap year.  So, 365 days - 10 holidays - 104 weekend days = 251 business days.  Thus, in a leap year, we would have 252 business days.

Note that 52 times 7 equals 364.  This means that in a non-leap year, a complete year of 365 days will begin and end on the same day of the week.  If that day happens to be a Saturday or Sunday, then we will have only 250 business days.

?Weekday(#2/6/2011#) = vbSunday
True
?Weekday(#2/5/2012#) = vbSunday
True

Based on the above observations, a complete year could have 250, 251, or 252 working days in it.  Therefore, we will test all three conditions with our doc tests.

The Code

'---------------------------------------------------------------------------------------
' Procedure : WorkingDayCount
' Author    : Mike
' Date      : 11/29/2012
' Purpose   : Returns the number of working days between two dates (inclusive)
' Notes     - WorkingDays are defined as Monday-Friday, less Federal Holidays
'           - FederalHolidays as defined by OPM
'           - Current list of federal holidays valid only for 1997 - 2030
' Usage
'>>> WorkingDayCount(#2/1/2011#,#1/31/2012#)
'251
'>>> WorkingDayCount(#2/1/2012#,#1/31/2013#)
'252
'>>> WorkingDayCount(#2/6/2011#,#2/5/2012#)
'250
'---------------------------------------------------------------------------------------
'
Function WorkingDayCount(StartDate As Date, EndDate As Date) As Long
    WorkingDayCount = WeekDayCount(StartDate, EndDate) - FedHolidays(StartDate, EndDate).Count
End Function

Image by Quang Nguyen vinh from Pixabay