I have been using the following function to compute the days difference between dates while subtracting out weekends and holidays. Initially, the code would not run due to a null values problem (sometimes records have missing dates). So I modified the code by adding the following:
If IsDate(BegDate) = False Or IsDate(EndDate) = False Then
Exit Function
This may not be the most elegant solution (I'm no coding wiz), but it does allow the code to run. Unfortunately, it also results in a zero as the answer for any computation where a date is missing. Obviously, this can be misleading, since a zero can mean that no days have actually passed, or that one of the dates in the computation is missing. It also screws up things when I'm to compute the average days difference.
My question: how can I modify this function so that it will run, but not return any value at all if a date is missing from the computation?
Here's the entire code below.
Thanks,
Lee
Function WeekdaysMinusHolidays(BegDate, EndDate) As Long
Dim db As Database
Dim holidays As Recordset
Dim d As Long
Dim answer As Long
If IsDate(BegDate) = False Or IsDate(EndDate) = False Then
Exit Function
Else
Set db = CurrentDb
Set holidays = db.OpenRecordset("tblholidays"
holidays.Index = "primarykey"
answer = 0
'pick one:
'For d = BegDate To EndDate 'includes both end points
For d = BegDate + 1 To EndDate 'excludes BegDate
'For d = BegDate To EndDate - 1 'excludes EndDate
'For d = BegDate + 1 To EndDate - 1 'excludes both end points
If Weekday(d) <> 1 And Weekday(d) <> 7 Then 'if not a weekend
holidays.Seek "=", d
If holidays.NoMatch Then 'not a holiday
answer = answer + 1
End If
End If
Next d
holidays.close
db.close
WeekdaysMinusHolidays = answer
End If
End Function
If IsDate(BegDate) = False Or IsDate(EndDate) = False Then
Exit Function
This may not be the most elegant solution (I'm no coding wiz), but it does allow the code to run. Unfortunately, it also results in a zero as the answer for any computation where a date is missing. Obviously, this can be misleading, since a zero can mean that no days have actually passed, or that one of the dates in the computation is missing. It also screws up things when I'm to compute the average days difference.
My question: how can I modify this function so that it will run, but not return any value at all if a date is missing from the computation?
Here's the entire code below.
Thanks,
Lee
Function WeekdaysMinusHolidays(BegDate, EndDate) As Long
Dim db As Database
Dim holidays As Recordset
Dim d As Long
Dim answer As Long
If IsDate(BegDate) = False Or IsDate(EndDate) = False Then
Exit Function
Else
Set db = CurrentDb
Set holidays = db.OpenRecordset("tblholidays"
holidays.Index = "primarykey"
answer = 0
'pick one:
'For d = BegDate To EndDate 'includes both end points
For d = BegDate + 1 To EndDate 'excludes BegDate
'For d = BegDate To EndDate - 1 'excludes EndDate
'For d = BegDate + 1 To EndDate - 1 'excludes both end points
If Weekday(d) <> 1 And Weekday(d) <> 7 Then 'if not a weekend
holidays.Seek "=", d
If holidays.NoMatch Then 'not a holiday
answer = answer + 1
End If
End If
Next d
holidays.close
db.close
WeekdaysMinusHolidays = answer
End If
End Function