Workdays between two dates (1 Viewer)

ECEK

Registered User.
Local time
Today, 06:29
Joined
Dec 19, 2012
Messages
717
I have created a module called Work_days with the following code from a website.

https://msdn.microsoft.com/en-us/library/office/ff191987.aspx


Code:
Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer 
 
 Dim WholeWeeks As Variant 
 Dim DateCnt As Variant 
 Dim EndDays As Integer 
 
 On Error GoTo Err_Work_Days 
 
 BegDate = DateValue(BegDate) 
 EndDate = DateValue(EndDate) 
 WholeWeeks = DateDiff("w", BegDate, EndDate) 
 DateCnt = DateAdd("ww", WholeWeeks, BegDate) 
 EndDays = 0 
 
 Do While DateCnt <= EndDate 
 If Format(DateCnt, "ddd") <> "Sun" And _ 
 Format(DateCnt, "ddd") <> "Sat" Then 
 EndDays = EndDays + 1 
 End If 
 DateCnt = DateAdd("d", 1, DateCnt) 
 Loop 
 
 Work_Days = WholeWeeks * 5 + EndDays 
 
Exit Function 
 
 Err_Work_Days: 
 
 ' If either BegDate or EndDate is Null, return a zero 
 ' to indicate that no workdays passed between the two dates. 
 
 If Err.Number = 94 Then 
 Work_Days = 0 
 Exit Function 
 Else 
' If some other error occurs, provide a message. 
 MsgBox "Error " & Err.Number & ": " & Err.Description 
 End If 
 
End Function

I have complied and saved the module and it shows up under my Modules list I have the following in my Query:

Workingdays: Work_days([BegDate],[EndDate])

however when I run the query it says:

Undefined function 'Work_days' in expression

Am i missing something fundamental here?
 

RuralGuy

AWF VIP
Local time
Yesterday, 23:29
Joined
Jul 2, 2005
Messages
13,826
The Module name and the Function name can NOT be the same.
 

ECEK

Registered User.
Local time
Today, 06:29
Joined
Dec 19, 2012
Messages
717
Simple !! many thanks RuralGuy.
 

RuralGuy

AWF VIP
Local time
Yesterday, 23:29
Joined
Jul 2, 2005
Messages
13,826
You're welcome. It happens all of the time. ;)
 

RuralGuy

AWF VIP
Local time
Yesterday, 23:29
Joined
Jul 2, 2005
Messages
13,826
Why not go ahead and use the Thread Tool at the top of the thread to mark this thread as Solved.
 

Users who are viewing this thread

Top Bottom