• Networkdays (Excel 2003)

    Author
    Topic
    #457014

    Hans – you wrote this marvelous piece of code for me. How could I alter this to also exclude Sundays?

    Public Function sixdayworkweekbetween(rngInStart As Range, rngInEnd As Range, rngHolidays As Range) As Long
    ‘ Arguments are Start Date cell, End Date cell, and range of Holidays in valid Excel date format
    Dim rngCell As Range
    Dim lngStartDate As Long, lngEndDate As Long, lngNextDay As Long, lngWorkDayCount As Long
    Dim lngIncr As Long, lngC As Long

    Application.Volatile
    lngStartDate = CLng(rngInStart.Value)
    lngEndDate = CLng(rngInEnd.Value)

    lngNextDay = lngStartDate
    Do
    lngNextDay = lngNextDay + 1
    lngIncr = 1
    If Weekday(lngNextDay, vbMonday) = 2 Then ‘ Monday, don’t count it
    lngIncr = 0
    Else
    For Each rngCell In rngHolidays
    If lngNextDay = CLng(rngCell.Value) Then ‘ Holiday, don’t count it
    lngIncr = 0
    Exit For
    End If
    Next rngCell
    End If
    If lngIncr = 0 Then Debug.Print “Holiday or Sunday: ” & CDate(lngNextDay)
    ‘ increment work day count by 1, or 0 if a Sunday or Holiday
    lngWorkDayCount = lngWorkDayCount + lngIncr
    Debug.Print CDate(lngNextDay); lngWorkDayCount
    Loop Until lngNextDay = lngEndDate
    sixdayworkweekbetween = lngWorkDayCount

    End Function

    Thanks!

    Viewing 0 reply threads
    Author
    Replies
    • #1144455

      In fact, it was JohnBF, not I who wrote this function for you – see post 656,446.

      You have modified it incorrectly – as it is now, it skips Tuesdays, not Mondays:

      If Weekday(lngNextDay, vbMonday) = 2 Then

      With the vbMonday argument, Monday is the first day of the week, Tuesday the second day, etc. To skip Mondays, use

      If Weekday(lngNextDay, vbSunday) = 2 Then

      To skip both Sundays and Mondays, use

      If Weekday(lngNextDay, vbSunday) <= 2 Then

      Note: please read this reply in the Lounge itself. The e-mail version may be garbled.

    Viewing 0 reply threads
    Reply To: Networkdays (Excel 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: