How to 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.

And so on.

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

Additional Resources

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

VBA: How to Get Month Name from Date
VBA: How to Use IsDate to Check if Cell is a Date
VBA: How to Use mm/dd/yyyy as Date Format

Leave a Reply

Your email address will not be published. Required fields are marked *