[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel NetworkDays function for Access

Posted on 2007-03-28
4
Medium Priority
?
1,910 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 1000 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 1000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

873 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