+ Reply to Thread
Results 1 to 8 of 8

Effective Days function in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2009
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2003
    Posts
    34

    Effective Days function in VBA

    Hi all,

    I am after a piece of coding to incorporate into a user defined function as follows:

    =UDF(start_date , finish_date , [1 or 2 or 3] , 'range of public holiday dates')

    1 = calendar days (i.e. finish_date - start_date)
    2 = business days (i.e. finish_date - start_date, less weekends and public holidays)
    3 = working days (i.e. finish_date - start_date, less Sundays and public holidays)

    This is similar to the 'NETWORKDAYS.INTL' standard function, however I cannot use that for two reasons; first the standard 'NETWORKDAYS.INTL' function does not calculate calendar days, and second I need the coding to be part of a larger user defined function that already has a start_date and finish_date field in it.

    Any help would be appreciated.

    Thanks.
    Last edited by M@N; 06-15-2016 at 02:02 AM. Reason: Solved

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Effective Days function in VBA

    Maybe :

    Public Function EffectiveDays(startDate As Date, finishDate As Date, method As Long, Optional rngHoliday)
      Dim d1 As Date, d2 As Date, d As Long, i As Long, v As Variant
      d1 = Int(startDate): d2 = Int(finishDate)
      If d1 > d2 Then: v = d1: d1 = d2: d2 = v
      d = DateDiff("d", d1, d2) + 1
      Select Case method
        Case 2
          For i = CLng(d1) To CLng(d2)
              If (i Mod 7 = 0) Or (i Mod 7 = 1) Then d = d - 1
          Next i
        Case 3
          For i = CLng(d1) To CLng(d2)
              If (i Mod 7 = 1) Then d = d - 1
          Next i
      End Select
      If Not IsMissing(rngHoliday) Then
         For Each v In rngHoliday
             If IsDate(v) Then
                i = Int(v)
                If i >= d1 And i <= d2 Then d = d - 1
             End If
         Next v
      End If
      EffectiveDays = d
    End Function
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Effective Days function in VBA

    Here's another version

    Just check that Karedogs version doesn't count public holidays that also fall on a weekend. These need to be excluded from the count as they overlap.

    Function workdays(date1 As Date, date2 As Date, switch As Single, pubhols As Range) As Single
    Dim cell As Range
    Dim count, weekends As Single
    count = 0
    weekends = 0
    
    If switch = 1 Then
        result = date2 - date1 + 1
    End If
    
    If switch = 2 Then
        For i = date1 To date2
            If WorksheetFunction.Weekday(i, 16) < 3 Then 'counts the weekends
            weekends = weekends + 1
            End If
        Next i
        result = date2 - date1 + 1 - weekends
    End If
    
    If switch = 3 Then
    
    For i = date1 To date2
            If WorksheetFunction.Weekday(i, 16) < 3 Then 'counts the weekends
            weekends = weekends + 1
            End If
        Next i
    
    For Each cell In pubhols
        If cell.Value >= date1 And cell.Value <= date2 And WorksheetFunction.Weekday(cell.Value, 16) > 2 Then
            count = count + 1 ' a pub holiday that doesn't fall on a weekend
        End If
     Next cell
        result = date2 - date1 + 1 - count - weekends
    End If
    
    
    workdays = result
    
    
    End Function
    Happy with my advice? Click on the * reputation button below

  4. #4
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Effective Days function in VBA

    And here's the file with the UDF
    Attached Files Attached Files

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Effective Days function in VBA

    Good catch Crooza, I miss that point.

    Fixed UDF :
    Public Function EffectiveDays(startDate As Date, finishDate As Date, method As Long, Optional rngHoliday)
      Dim d1 As Date, d2 As Date, d As Long, i As Long, v As Variant
      d1 = Int(startDate): d2 = Int(finishDate)
      If d1 > d2 Then: v = d1: d1 = d2: d2 = v
      d = DateDiff("d", d1, d2) + 1
      Select Case method
        Case 2
          For i = CLng(d1) To CLng(d2)
              If (i Mod 7 = 0) Or (i Mod 7 = 1) Then d = d - 1
          Next i
        Case 3
          For i = CLng(d1) To CLng(d2)
              If (i Mod 7 = 1) Then d = d - 1
          Next i
      End Select
      If Not IsMissing(rngHoliday) Then
         For Each v In rngHoliday
             If IsDate(v) Then
                i = Int(v)
                If i >= d1 And i <= d2 Then
                   Select Case method
                     Case 2
                       If (i Mod 7 <> 0) And (i Mod 7 <> 1) Then d = d - 1
                     Case 3
                       If (i Mod 7 <> 1) Then d = d - 1
                   End Select
                End If
             End If
         Next v
      End If
      EffectiveDays = d
    End Function

  6. #6
    Registered User
    Join Date
    05-07-2009
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Effective Days function in VBA

    Karedog /Crooza, thank you both. Works a treat!

  7. #7
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Effective Days function in VBA

    Great. Glad it works how you wanted

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Effective Days function in VBA

    You are welcome M@N, thanks for the rep. points and marking the thread as solved.


    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2016, 09:18 AM
  2. [SOLVED] More effective rank function
    By adandsar in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-02-2015, 04:52 PM
  3. DAYS function: correctly calculating the number of days
    By dadpad in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-25-2015, 09:18 PM
  4. Replies: 4
    Last Post: 04-10-2013, 02:37 AM
  5. SUMIF Function if within 30 days, 60 days, etc
    By Geomin3 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-25-2013, 05:39 AM
  6. Replies: 5
    Last Post: 03-22-2012, 04:51 AM
  7. Effective interest paid - need formula/function
    By Pasko1 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-18-2005, 05:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1