Solved

Excel NetworkDays function for Access

Posted on 2007-03-28
4
1,886 Views
Last Modified: 2012-06-21
Does anyone know how to make a function for SQL that will behave the same way as Excel's networkdays.  Here is my excel formula

=IF(K125=TODAY(),0,NETWORKDAYS(K125,TODAY()-1))

Note*  If K125 = 03/28/07 which is today, so the result would be 0 or If K125=03/26/07, the result would be 2


0
Comment
Question by:cstraim
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
adraughn earned 250 total points
ID: 18809648
Put this in a module. When passed two dates it will eliminate weekends.

Option Compare Database
Option Explicit

  Public Function DateDiffW(BegDate, EndDate) As Integer
  On Error Resume Next
      Const SUNDAY = 1
      Const SATURDAY = 7
      Dim NumWeeks As Integer


      If BegDate > EndDate Then
         DateDiffW = 0
      Else
         Select Case WeekDay(BegDate)
            Case SUNDAY: BegDate = BegDate + 1
            Case SATURDAY: BegDate = BegDate + 2
         End Select
         Select Case WeekDay(EndDate)
            Case SUNDAY: EndDate = EndDate - 2
            Case SATURDAY: EndDate = EndDate - 1
         End Select
         NumWeeks = DateDiff("ww", BegDate, EndDate)
         DateDiffW = NumWeeks * 5 + WeekDay(EndDate) - WeekDay(BegDate)
      End If
   End Function
0
 
LVL 13

Expert Comment

by:adraughn
ID: 18809689
For SQL:

Declare @aDate datetime,
      @bDate datetime
Set @aDate = '4/22/2005'
Set @bDate = '5/12/2005'

Select
      dayDiff = datediff(dy,@aDate,@bDate),
      fullweeks = datediff(dy,@aDate,@bDate)/7,
      weekdays = datediff(dy,@aDate,@bDate)/7*2,
      startday = datepart(dw,@aDate),
      extraDays = datediff(dy,@aDate,@bDate)%7,
      estraWeekDays = case
                  when datepart(dw,@aDate)=1 and datediff(dy,@aDate,@bDate)%7<6 then 1
                  when datepart(dw,@aDate)>1 and datepart(dw,@aDate)+datediff(dy,@aDate,@bDate)=7 then 1
                  when datepart(dw,@aDate)>1 and datepart(dw,@aDate)+datediff(dy,@aDate,@bDate)>7 then 2
                  else 0 end,
      weekdayDiff = datediff(dy,@aDate,@bDate) - (datediff(dy,@aDate,@bDate)/7*2 +
                  case when datepart(dw,@aDate)=1 and datediff(dy,@aDate,@bDate)%7<6 then 1
                  when datepart(dw,@aDate)=1 and datediff(dy,@aDate,@bDate)%7=6 then 2
                  when datepart(dw,@aDate)>1 and datepart(dw,@aDate)+datediff(dy,@aDate,@bDate)=7 then 1
                  when datepart(dw,@aDate)>1 and datepart(dw,@aDate)+datediff(dy,@aDate,@bDate)>7 then 2
                  else 0 end)
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 18809944
try this function

Function GetNetWorkDays(startDate As Date, endDate As Date) As Integer

Dim objFunction As MSOWCFLib.OCATP
Set objFunction = New MSOWCFLib.OCATP
GetNetWorkDays = objFunction.NetworkDays(startDate, endDate)
Set objFunction = Nothing
End Function


Note**
you have to add to your references;

Tools>References

                 Microsoft Office Web Components Function Library
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18809981
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question