1

我已经修改了这个访问功能,所以我可以根据工作日的数量计算出结束日期。

但是,我还需要考虑英国的银行假期

有人知道我该怎么做吗?

Public Function CountDays(startDate As Date, NoOfDays As Integer) As Date
' Function to count no of working days
Dim tmpNo As Integer
Dim tmpDate As Date
Dim tmpStartDate As Date
Dim i As Integer

tmpNo = NoOfDays

tmpStartDate = startDate

tmpDate = startDate

i = 0

Do Until i = NoOfDays

If Weekday(tmpDate) = 1 Or Weekday(tmpDate) = 7 Then
tmpNo = tmpNo + 1
Else
i = i + 1
End If
tmpDate = tmpDate + 1
Loop

CountDays = DateAdd("d", tmpNo, tmpStartDate)
End Function

干杯

罗斯

4

2 回答 2

2

无需每天循环。稍微计算一下就可以得到工作日的数量,然后在表格中查找该时间范围内的假期数量。所以:

TotalDays = DateDiff("d", startDate, endDate) + 1

TotalWeekends = (DateDiff("ww", startDate, endDate) * 2) + _ 
((DatePart("w", startDate) = vbSunday) *-1) + _ 
((DatePart("w", endDate) = vbSaturday) *-1)

TotalHolidays = DCount("*","tblHolidays","Holiday Between #" & startdate & "# And #" & enddate & "#")

TotalWorkingDays = TotalDays - TotalWeekends - TotalHolidays 

顺便说一句,如果您在日期前后使用 #,Access 会假定它是美国格式。您可以改用引号。

于 2013-03-07T15:38:46.487 回答
0

由于英国的银行假日每年都不同,因此您需要创建一个表来存储这些日期,然后在代码中检查此表并增加 tmpNo 值(如果日期存在)。

通过添加额外的“Else If”子句来更改代码,如下所示:

Do Until i = NoOfDays
    If Weekday(tmpDate) = 1 Or Weekday(tmpDate) = 7 Then
        tmpNo = tmpNo + 1
    Else If DCount("*","tblExceptionDates","dtmDate = #" & tmpDate & "#") > 0 Then
        tmpNo = tmpNo + 1
    Else
        i = i + 1
    End If
    tmpDate = tmpDate + 1
Loop
于 2013-03-07T13:54:58.253 回答