View Single Post
 
Old 10-20-2021, 09:15 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

One way would be to use the Weekday function to establish if each day in the range is a weekday and count those that are. e.g.
Code:
Sub CalculateDateDifference()
Dim xStartDate As Date
Dim sDate As String
Dim xEndDate As Date, dTestDate As Date
Dim xDay As Long, lCount As Long, i As Long
    On Error Resume Next
    xEndDate = Date
    sDate = InputBox("Enter the start date", "Start Date", "")
    If IsDate(sDate) = False Then
        MsgBox "Please input a valid date.", vbInformation, "Date Calculation"
        Exit Sub
    End If
    xStartDate = CDate(sDate)
    If xStartDate > xEndDate Then
        MsgBox "The start date must be before the end date!", vbInformation, "Date Calculation"
        Exit Sub
    End If
    xDay = DateDiff("d", xStartDate, xEndDate)
    lCount = 0: dTestDate = xStartDate
    For i = 1 To xDay
        If Weekday(dTestDate) > 1 And Weekday(dTestDate) < 7 Then lCount = lCount + 1
        dTestDate = dTestDate + 1
    Next i
    MsgBox "There are " & lCount & " work days from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "Date Calculation"
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote