Results 1 to 4 of 4
  1. #1
    SunTop is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Aug 2016
    Posts
    93

    working days

    Hello Everyone



    I downloaded this code from here:

    https://msdn.microsoft.com/en-us/library/dd327646(v=office.12).aspx

    this code calculates the number of working days between two dates. The code subtracts the weekends (Sunday & Saturday) and the dates listed in the holidays table. I want this code to be modified so that the weekend is only one day (Friday).

    Public Function Workdays(ByRef startDate As Date, _
    ByRef endDate As Date, _
    Optional ByRef strHolidays As String = "Holidays" _
    ) As Integer
    ' Returns the number of workdays between startDate
    ' and endDate inclusive. Workdays excludes weekends and
    ' holidays. Optionally, pass this function the name of a table
    ' or query as the third argument. If you don't the default
    ' is "Holidays".
    On Error GoTo Workdays_Error
    Dim nWeekdays As Integer
    Dim nHolidays As Integer
    Dim strWhere As String

    ' DateValue returns the date part only.
    startDate = DateValue(startDate)
    endDate = DateValue(endDate)

    nWeekdays = Weekdays(startDate, endDate)
    If nWeekdays = -1 Then
    Workdays = -1
    GoTo Workdays_Exit
    End If

    strWhere = "[Holiday] >= #" & startDate _
    & "# AND [Holiday] <= #" & endDate & "#"

    ' Count the number of holidays.
    nHolidays = DCount(Expr:="[Holiday]", _
    Domain:=strHolidays, _
    Criteria:=strWhere)

    Workdays = nWeekdays - nHolidays

    Workdays_Exit:
    Exit Function

    Workdays_Error:
    Workdays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Workdays"
    Resume Workdays_Exit

    End Function




    Public Function Weekdays(ByRef startDate As Date, _
    ByRef endDate As Date _
    ) As Integer
    ' Returns the number of weekdays in the period from startDate
    ' to endDate inclusive. Returns -1 if an error occurs.
    ' If your weekend days do not include Saturday and Sunday and
    ' do not total two per week in number, this function will
    ' require modification.
    On Error GoTo Weekdays_Error

    ' The number of weekend days per week.
    Const ncNumberOfWeekendDays As Integer = 2

    ' The number of days inclusive.
    Dim varDays As Variant

    ' The number of weekend days.
    Dim varWeekendDays As Variant

    ' Temporary storage for datetime.
    Dim dtmX As Date

    ' If the end date is earlier, swap the dates.
    If endDate < startDate Then
    dtmX = startDate
    startDate = endDate
    endDate = dtmX
    End If

    ' Calculate the number of days inclusive (+ 1 is to add back startDate).
    varDays = DateDiff(Interval:="d", _
    date1:=startDate, _
    date2:=endDate) + 1

    ' Calculate the number of weekend days.
    varWeekendDays = (DateDiff(Interval:="ww", _
    date1:=startDate, _
    date2:=endDate) _
    * ncNumberOfWeekendDays) _
    + IIf(DatePart(Interval:="w", _
    Date:=startDate) = vbSunday, 1, 0) _
    + IIf(DatePart(Interval:="w", _
    Date:=endDate) = vbSaturday, 1, 0)

    ' Calculate the number of weekdays.
    Weekdays = (varDays - varWeekendDays)

    Weekdays_Exit:
    Exit Function

    Weekdays_Error:
    Weekdays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Weekdays"
    Resume Weekdays_Exit
    End Function

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Here is a much simpler procedure for what you want.
    Code:
    Public Function NetWorkdays(dteStart As Date, dteEnd As Date) As Integer
       Dim intGrossDays As Integer
       Dim dteCurrDate As Date
       Dim i As Integer
       ' If the end date is earlier, swap the dates.
       If dteEnd < dteStart Then
          dteCurrDate = StartDate
          StartDate = EndDate
          EndDate = dteCurrDate
       End If
       intGrossDays = Abs(DateDiff("d", dteStart, dteEnd))
       NetWorkdays = 0
       For i = 0 To intGrossDays
          dteCurrDate = dteStart + i
    '      If Weekday(dteCurrDate, vbMonday) < 6 Then '<-- Use this for Sat and Sun
          If Weekday(dteCurrDate, vbMonday) <> 5 Then '<-- modified for just Friday
             NetWorkdays = NetWorkdays + 1
          End If
       Next i
    End Function

  3. #3
    SunTop is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Aug 2016
    Posts
    93
    thanks for your replay RuralGuy

    The vba editor is giving me a "Variable not defined error"

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    My bad...sorry!
    Code:
    Public Function NetWorkdays(dteStart As Date, dteEnd As Date) As Integer
       Dim intGrossDays As Integer
       Dim dteCurrDate As Date
       Dim i As Integer
       ' If the end date is earlier, swap the dates.
       If dteEnd < dteStart Then
          dteCurrDate = dteStart
          dteStart = dteEnd
          dteEnd = dteCurrDate
       End If
       intGrossDays = DateDiff("d", dteStart, dteEnd)
       NetWorkdays = 0
       For i = 0 To intGrossDays
          dteCurrDate = dteStart + i
          If Weekday(dteCurrDate, vbMonday) <> 5 Then
             NetWorkdays = NetWorkdays + 1
          End If
       Next i
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to Add 2, 5 or 30 working days to a date
    By hazeleyre23 in forum Access
    Replies: 8
    Last Post: 06-30-2016, 04:14 AM
  2. First and Last Working Days of Month
    By BLFOSTER in forum Queries
    Replies: 5
    Last Post: 05-01-2014, 03:14 PM
  3. Working Days From Todays Date
    By TimMoffy in forum Queries
    Replies: 2
    Last Post: 03-26-2013, 08:54 AM
  4. Working days module help for noob
    By AndycompanyZ in forum Modules
    Replies: 5
    Last Post: 07-27-2011, 01:43 AM
  5. calculate no. of working days
    By JOSE LUIS in forum Access
    Replies: 1
    Last Post: 02-01-2010, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums