Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Access (https://www.askmehelpdesk.com/forumdisplay.php?f=441)
-   -   Access Date Functions (https://www.askmehelpdesk.com/showthread.php?t=1633)

  • Jul 31, 2003, 06:44 AM
    smile4life
    Access Date Functions
    I need to count the number of workdays between a range of dates. I know how to simply count the number of days, but can't seem to figure out how I would exclude non-workdays. Any ideas?

    Thanks.
  • Nov 26, 2003, 08:17 AM
    tadds
    Access Date Functions
    Hi

    I've just did a quick hack for you. Here's the code that
    you can use. Did a quick test in Excel VBA. Should work
    in any VBA (Access, Excel, etc.)
    Code:

    Function GetDayType(dd As Date) As Byte

    '--- This function computes day type based on given
    Date (dd)
    ' Return Codes:
    ' 1 - Weekday (Monday to Friday)
    ' 2 - Saturday
    ' 3 - Sunday

    If Weekday(dd, 2) >= 1 And Weekday(dd, 2) <= 5 Then
    GetDayType = 1
    ElseIf Weekday(dd, 2) = 6 Then
    GetDayType = 2
    ElseIf Weekday(dd, 2) = 7 Then
    GetDayType = 3
    End If
    End Function

    Sub Simulate_Get_Weekdays()
    Dim StartDate As Date
    Dim EndDate As Date
    Dim j As Integer '-- day difference
    Dim i As Integer '-- for loop index
    Dim k As Integer '-- counter for weekdays

    StartDate = #11/1/2003#
    EndDate = #11/15/2003#

    j = DateDiff("d", StartDate, EndDate)
    k = 0

    For i = 1 To j

    '--- Tip: If you use different return code, you
    can count weekends too!
    If (GetDayType(DateSerial(Year(StartDate), Month
    (StartDate), Day(StartDate) + i)) = 1) Then
    k = k + 1
    End If
    Next i

    MsgBox k '-- number of weekdays
    End Sub

    Enjoy!

  • All times are GMT -7. The time now is 05:37 PM.