Getting workdays by month using VBA

bcurrey

Board Regular
Joined
Aug 11, 2011
Messages
110
Office Version
  1. 365
Platform
  1. MacOS
I'm building a query that will run automatically at night using vba code. Some of the calculations will be based on # of working days in the current month and prior month.

Any ideas on how I can have this included in the macro to do automatically? I know how to get # of days in a month, but is there anything that I can do to get days worked?

Appreciate your help.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You can use the weekday function as you are looping though the days of the month to determine the number of weekdays
 
Upvote 0
You can also use this function to return the number of days in a month before doing it:

Code:
Public Function DaysInMonth(dateString As String) As Integer
   Dim myDate As Date
   myDate = DateValue(dateString)
   DaysInMonth = day(Application.WorksheetFunction.EoMonth(myDate, 0))
End Function
 
Upvote 0
I like that suggestion for networkdays.

Putting it together word look something like this:

Code:
Public Function WorkDaysInMonth(myDate As Date) As Integer
    Dim curMonth As Integer
    Dim curYear As Integer

    curMonth = Month(myDate)
    curYear = Year(myDate)
    begindate = CDate(curMonth & "/" & 1 & "/" & curYear)
    enddate = Application.WorksheetFunction.EoMonth(myDate, 0)
    WorkDaysInMonth = Application.WorksheetFunction.NetworkDays(begindate, enddate)
End Function

Sub test()
    MsgBox WorkDaysInMonth("September 14, 2011")
End Sub
 
Upvote 0
Putting it together word look something like this:

Code:
Public Function WorkDaysInMonth(myDate As Date) As Integer
    Dim curMonth As Integer
    Dim curYear As Integer
 
    curMonth = Month(myDate)
    curYear = Year(myDate)
    begindate = CDate(curMonth & "/" & 1 & "/" & curYear)
    enddate = Application.WorksheetFunction.EoMonth(myDate, 0)
    WorkDaysInMonth = Application.WorksheetFunction.NetworkDays(begindate, enddate)
End Function
Here is a modification to a function that I have posted in the past which you might consider using instead... it does not call out to the worksheetfunctions, choosing instead to use native VBA functions only.

Code:
Function CWD(myDate As Date) As Long
  Dim StartDate As Date, EndDate As Date
  StartDate = myDate - Day(myDate) + 1
  EndDate = DateSerial(Year(myDate), Month(myDate) + 1, 0)
  CWD = DateDiff("d", StartDate, EndDate) - DateDiff("ww", StartDate, EndDate) * 2 - (Weekday(EndDate) <> 7) + (Weekday(StartDate) = 1)
End Function

The CWD function name is short for Count Working Days.
 
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