I appreciate the response; but this calculation just count the business days between dates selected. I need the actual business day based on the date in the field like in my example. Thanks
Me.txtBusinessDays = BusinessDays(dteStart, dteEnd)
Sorry; I'm not following you. If the date field contains 1/12/2016; the business day for that month would be 8. This is counting from the 1st of the month only Monday - Friday. Exclude weekends.
Private Sub cmdCalculate_Click()
Dim dteStart As Date
Dim dteEnd As Date
If Not IsDate(Me.txtStart) Then
MsgBox "Please enter a start date", vbOKOnly, "error"
Me.txtStart.SetFocus
Exit Sub
End If
If Not IsDate(Me.txtEnd) Then
MsgBox "Please enter an ending date", vbOKOnly, "Error"
Me.txtEnd.SetFocus
Exit Sub
End If
dteStart = Me.txtStart
dteEnd = Me.txtEnd
Me.txtBusinessDays = BusinessDays(dteStart, dteEnd)
End Sub
Public Function fnBusinessDay(dt As Variant) As Integer
Dim i As Integer
Dim dtTemp As Variant
i = 0
If IsNull(dt) Then
fnbusiness = 0
Else
dtTemp = DateSerial(Year(dt), 1, i)
While dtTemp < dt
If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 Then i = i + 1
dtTemp = dtTemp + 1
Wend
fnBusinessDay = i
End If
End Function
Code:Public Function fnBusinessDay(dt As Variant) As Integer Dim i As Integer Dim dtTemp As Variant i = 0 If IsNull(dt) Then fnbusiness = 0 Else dtTemp = DateSerial(Year(dt), 1, i) While dtTemp < dt If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 Then i = i + 1 dtTemp = dtTemp + 1 Wend fnBusinessDay = i End If End Function
that would be easy just add, a Holiday table and modify the code:
If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 And _
Nz( _
DCount("*","HolidayTable","[DateField] = #" & Format(dtTemp, "mm/dd/yyyy") & "#"), 0) = 0 Then i = i + 1
This code worked. But users just informed me that instead of the business day starting over each month; it continues as if it counts the business day of the year i.e. February 1 should be business day 1; instead it comes up as business day 20. How do I modify this to start over when the month change please. Thanks
Public Function fnBusinessDay(dt As Variant) As Integer
Dim i As Integer
Dim dtTemp As Variant
i = 0
If IsNull(dt) Then
fnbusiness = 0
Else
If Year(dt) > Year(Date) Then
dtemp = DateSerial(Year(Date) - 1, 2, i) + 1
Else
dtTemp = DateSerial(Year(dt), 2, i) + 1
End If
While dtTemp <= dt
If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 Then i = i + 1
dtTemp = dtTemp + 1
Wend
fnBusinessDay = i
End If
End Function
Public Function fnBusinessDay(dt As Variant) As Integer
Dim i As Integer
Dim dtTemp As Variant
i = 0
If IsNull(dt) Then
fnbusiness = 0
Else
dtTemp = DateSerial(Year(dt), 1, i)
While dtTemp < dt
If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 And _
Nz( _
DCount("*", "tblHolidays", "[HolidayDte] = #" & Format(dtTemp, "mm/dd/yyyy") & "#"), 0) = 0 Then i = i + 1
Wend
fnBusinessDay = i
End If
End Function
Public Function fnBusinessDay(dt As Variant) As Integer
Dim i As Long
Dim dtTemp As Variant
i = 0
If IsNull(dt) Then
fnbusiness = 0
Else
dtTemp = DateSerial(Year(dt), [b]Month(dt)[/b], i)
While dtTemp < dt
If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 And _
Nz( _
DCount("*", "tblHolidays", "[HolidayDte] = #" & Format(dtTemp, "mm/dd/yyyy") & "#"), 0) = 0 Then i = i + 1
[b]dtTemp = dtTemp +1[/b]
Wend
fnBusinessDay = i
End If
End Function
Public Function fnBusinessDay(dt As Variant) As Integer
Dim i As Long
Dim dtTemp As Variant
i = 0
If IsNull(dt) Then
fnbusiness = 0
Else
dtTemp = DateSerial(Year(dt), Month(dt), i)
While dtTemp < dt
If InStr("Sat/Sun", Format(dtTemp, "ddd")) = 0 And _
Nz( _
DCount("*", "tblHolidays", "[HolidayDte] = #" & Format(dtTemp, "mm/dd/yyyy") & "#"), 0) = 0 Then i = i + 1
[b]End If[/b]
dtTemp = dtTemp +1
Wend
fnBusinessDay = i
End If
End Function