Number of working days in a month with VBA

Skippy

Board Regular
Joined
Mar 3, 2002
Messages
194
The code below gives me the total number of days in a month. But I would like only the working days. There are many examples using the NetworkDays function but this function is not available in VBA (as far as I can see). Can someone help?
Code:
Sub FillWeekdays()
    Dim x As Long
    Dim m As Integer
    Dim y As Integer
    Dim DaysInMonth As Integer
    Dim DestinationRange As Range
    Dim SourceRange As Range
    
    With Worksheets("Data")
        ' define last empty row
        x = .Range("B65536").End(xlUp).Row + 1
        
        ' add new starting date
        .Range("A" & x).Value = frmInput.cboMonth.Text & " 1, " & _
            frmInput.cboYear.Text
        
        ' define variable for number of the month
        m = Month(.Range("A" & x).Value)
        
        ' define variable for the year
        y = frmInput.cboYear.Text
        
        ' get number of days in month
        DaysInMonth = DateSerial(y, m + 1, 1) - DateSerial(y, m, 1)
  
    End With
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You can actually use Networkdays in VBA, you just need to add a reference
to atpvbaen.xls.

That should be available if you have the Analysis Toolpak VBA add-in installed.
 
Upvote 0
In VBA if you have the Add-In I mentioned installed then you can use the functions
from it if you add a reference to that library.

To do this goto Tools>References... and select atpvbaen.xls.

Now if you goto the Object Browser all the available functions (and subs)
will be listed in the VBA Functions and Subs class.
 
Upvote 0
Thanks... I did what you suggested. Will now see if I can the function to work with my code :-D
 
Upvote 0
Hmmm... I have the ToolPak installed and NetWorkDays shows in the Object Browser. I tried using Application.WorkbookFunction.NetworkDays but it is not supported.. Is there another way to get the number of working days in a month with VBA?
 
Upvote 0
Don't use Application.WorksheetFunction.:)
 
Upvote 0
Thanks Norie... the following works after following your advice. A tip of the hat :-D
Code:
Sub FillWeekdays()
    Dim x As Integer
    Dim DestinationRange As Range
    Dim SourceRange As Range

   With Worksheets("Data")
        ' define last empty row
        x = .Range("B65536").End(xlUp).Row + 1
        
        ' add new starting date
        .Range("A" & x).Value = frmInput.cboMonth.Text & " 1, " & _
            frmInput.cboYear.Text
        
        ' define variable for number of the month
        m = Month(.Range("A" & x).Value)
        
        ' define variable for the year
        y = frmInput.cboYear.Text
        
        ' get number of "working" days in month
        DaysInMonth = networkdays(DateSerial(y, m, 1), DateSerial(y, m + 1, 1)) - 1
     End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,574
Messages
6,173,145
Members
452,502
Latest member
PQCurious

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