How can I use the NetworkDays function in VBA with an example?

The NetworkDays function in VBA is a tool that allows users to calculate the number of working days between two given dates, taking into account weekends and any specified holidays. This function is commonly used in financial modeling and project management to determine timelines and deadlines. To use the NetworkDays function, users must provide the start and end dates, as well as a list of any holidays to be excluded. For example, if a project starts on January 1st and is due on January 31st, but there are 3 holidays in between, the NetworkDays function would return 23 as the number of working days. This function can also be used in conjunction with other VBA functions to automate complex calculations.

Use NetworkDays in VBA (With Example)


You can use the NetworkDays method in VBA to find the number of whole working days between specific start and end dates.

Note that working days exclude weekends and holidays.

Here is one common way to use this method in practice:

Sub CalculateNetworkDays()

Dim i As Integer

For i = 2 To 9
    Range("C" & i) = WorksheetFunction.NetworkDays(Range("A" & i), Range("B" & i))
Next i

End Sub 

This particular example calculates the number of working days between the start dates in the range A2:A9 and the end dates in the range B2:B9 and displays the results in the range C2:C9.

The following example shows how to use this syntax in practice.

Example: How to Use NetworkDays in VBA

Suppose we have the following list of start dates and end dates in Excel:

Suppose we would like to use the NetworkDays method in VBA to calculate the number of whole working days between the start and end dates in each row.

We can create the following macro to do so:

Sub CalculateNetworkDays()

Dim i As Integer

For i = 2 To 9
    Range("C" & i) = WorksheetFunction.NetworkDays(Range("A" & i), Range("B" & i))
Next i

End Sub 

When we run this macro, we receive the following output:

Column C shows the number of whole working days between the start and end dates in each row.

For example:

  • The number of working days between 1/2/2023 and 1/3/2023 is 2. (since both of these dates are weekdays).
  • The number of working days between 1/5/2023 and 1/8/2023 is 2.
  • The number of working days between 1/10/2023 and 1/20/2023 is 9.

Note: You can find the complete documentation for the NetworkDays method in VBA .

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

x