Networkdays in VBA

lucianot

Board Regular
Joined
Nov 5, 2005
Messages
83
I know there are several posts about this on the boards, but still can't get it to work:

My goal is to get the number of workdays between two dates in a VBA function. I tried simply using the Advanced Toolpak function NETWORKDAYS, but I get the error message "Sub or function not defined". I did reference to atpvbaen.xls in the Tools > References menu, so I don't know why Excel is not recognizing the function.

I used the following function to test it:

Function CalcNetDays(dInitial As Date, dEnd As Date)
CalcNetDays = NetworkDays(dInitial, dEnd)
End Function

Any reason the function is not working?
Also, I need to add the holidays to the function. The holidays are listed on a worksheet column. Do I simply add the range to the function, like below?

CalcNetDays = NetworkDays(dInitial, dEnd, Range("Holidays"))
 
It's an XLA file. However, what language are you using? Look in your Project Explorer (Ctrl-R), in the VBE, with no open Workbooks. What do you see? You should see some XLA files, pending which Add-ins you've installed. What are their names?
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I don't know why it isn't working for you, but I'm thinking that it might be a good idea to use your Office CD to either reinstall Excel and all its components or run a Repair on Office.
 
Upvote 0
Not just yet. And I don't mean VBA when I say language, I mean what is your local version of Office, e.g., Italian?
 
Upvote 0
My Excel is in English. The file is ATPVBAEN.XLAM.

I tried that and now I get "Cannot run the macro ATPVBAEN.XLAM!NetworkDays".
 
Upvote 0
In your Project Explorer, what does it say in Parenthesis? There should be two names, Name and (Name), you want the latter.

I must admit, I have not tested this in Excel 2007. :confused:

Can you post the exact code that's failing? I want to see your syntax.
 
Upvote 0
I used the same syntax a in your post:

Function Nwdys() As Long
Nwdys = Application.Run("ATPVBAEN.XLAM!Networkdays", Date - 2, Date)
End Function


I just tried the different files names.
 
Upvote 0
Cut your own, it is trivial code

Code:
Public Function WorkingDays(StartDate As Date, _
                            ByVal EndDate As Date, _
                            Optional ByVal Holidays As Range) As Long
Dim mpDays As Long
Dim mpHolidays As Variant
Dim i As Long

    If Not Holidays Is Nothing Then mpHolidays = Application.Transpose(Holidays)
    For i = StartDate To EndDate
    
        If Weekday(i, vbMonday) < 6 Then
        
            If IsError(Application.Match(i, mpHolidays, 0)) Then
                mpDays = mpDays + 1
            End If
        End If
    Next i
    WorkingDays = mpDays
   
End Function
 
Upvote 0
You can even have configurable working days

Code:
Public Function WorkingDays(StartDate As Date, _
                            ByVal EndDate As Date, _
                            Optional ByVal Holidays As Range, _
                            Optional ByVal Workdays As Variant) As Long
Dim mpDays As Long
Dim mpHolidays As Variant
Dim mpWorkdays As Variant
Dim i As Long

    If Not Holidays Is Nothing Then mpHolidays = Application.Transpose(Holidays)
    
    If IsMissing(Workdays) Then
    
        mpWorkdays = Array(2, 3, 4, 5, 6)
    Else
    
        mpWorkdays = Workdays
    End If
    
    For i = StartDate To EndDate
    
        If Not IsError(Application.Match(Weekday(i), mpWorkdays, 0)) Then
        
            If IsError(Application.Match(i, mpHolidays, 0)) Then
                mpDays = mpDays + 1
            End If
        End If
    Next i
    WorkingDays = mpDays
   
End Function
 
Upvote 0
Adding Application.WorksheetFunction in front of the function call works for me.
I had to add ATP VBA add-in into my Excel 2010 first (by going to Options> Add-in> Check the ATP VBA box, click OK). My working code looks like this

Dim datedif2 As Double
datedif2 = Application.WorksheetFunction.NetworkDays(duedate, Today, ThisWorkbook.Worksheets("Holidays").Range("A5:A25"))


Thank you!
 
Upvote 0

Forum statistics

Threads
1,217,394
Messages
6,136,353
Members
450,006
Latest member
DaveLlew

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top