VBA & Macro Problems

Calculate Workdays in Microsoft Access Excluding Weekends and Holidays

Natalie TaylorNatalie Taylor Nov 25, 2026 871 views

Question details

Calculate working days between two dates in an Access query, excluding weekends and holidays stored in a separate table.

Calculate Workdays in Microsoft Access Excluding Weekends and Holidays
Product
Microsoft Access
Device & OS
not provided
Scenario
An Access database uses a VBA function and a holiday table to calculate working days between start and end dates.
Observed behavior
The existing VBA function has syntax and expression errors that prevent the calculation from working correctly.
Before you start

Confirm the names of your holiday table and date field, and make sure the start and end dates contain valid, non-null dates. The example below uses a table named Holidays and a Date/Time field named HolidayDate.

Solution 1Recommended

Create a Workdays function and use it in an Access query

Use a public VBA function to count weekdays while excluding dates found in the holiday table.

This example counts both the start and end dates when they are working days. It excludes Saturday and Sunday, ignores duplicate holiday entries, and raises an error if the end date is earlier than the start date.

1
Open a standard module

In Access, select Create > Module. In the VBA editor, paste the function below into the standard module. Change Holidays and HolidayDate in the DCount expression if your table or field has a different name.

2
Add the function

Paste this code: Public Function Workdays(startDate As Date, endDate As Date) As Long Dim d As Date Dim total As Long If DateValue(endDate) < DateValue(startDate) Then Err.Raise vbObjectError + 1000, "Workdays", "End date must not precede start date." End If d = DateValue(startDate) Do While d <= DateValue(endDate) If Weekday(d, vbMonday) <= 5 Then If DCount("*", "[Holidays]", "[HolidayDate] >= #" & Format$(d, "mm\/dd\/yyyy") & "# AND [HolidayDate] < #" & Format$(DateAdd("d", 1, d), "mm\/dd\/yyyy") & "#") = 0 Then total = total + 1 End If End If d = DateAdd("d", 1, d) Loop Workdays = total End Function

3
Compile the code

In the VBA editor, select Debug > Compile to identify syntax errors. Save the module with a name other than Workdays, such as modDateFunctions.

4
Add the query expression

Open your query in Design View and enter Difference: Workdays([startDate],[endDate]) in an empty Field cell. Replace the field names if needed, then select Run to view the calculated column.

Create a Workdays function and use it in an Access query
Check your counting convention: The function includes both dates. If your business rules require excluding the start date or handling a different weekend schedule, adjust the function before using the results.
Free Microsoft Office alternative

Calculate Workdays in a WPS Spreadsheet

WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office formats, making it easy to move spreadsheet work. It does not fix or run the Access database function; use WPS Spreadsheets for a separate spreadsheet-based calculation.

Organize start dates, end dates, and holiday lists in a spreadsheetUse NETWORKDAYS for working-day calculations where supportedOpen and save Microsoft Excel XLSX filesWork in a familiar, easy-to-use office interface
microsoft office alternative - wps office

Frequently Asked Questions

How do I call the Workdays function in an Access query?

In query Design View, enter Difference: Workdays([startDate],[endDate]) in an empty Field cell. Use your actual date field names and ensure the function is Public in a standard module.

Does the function include the start and end dates?

Yes. The example counts each endpoint if it is a weekday and is not listed in the holiday table.

What happens if a holiday falls on a weekend?

The example skips weekends before checking the holiday table, so a weekend holiday is not subtracted a second time.

Why does Access report an undefined Workdays function?

Check that the function is declared Public in a standard module, that the module is not named Workdays, and that Debug > Compile completes without errors.

Can the function handle blank dates?

The example expects valid, non-null start and end dates. Filter out records with blank dates in the query or modify the function to handle Null values before calling it.