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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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 :biggrin:
 
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 :biggrin:
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,221,475
Messages
6,160,056
Members
451,615
Latest member
soroosh

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