将工作日添加到开始日期以创建结束日期(包括银行假日)

时间:2013-03-07 12:19:20

标签: ms-access access-vba

我已经修改了这个访问功能,所以我可以根据工作天数确定结束日期。

但我也需要考虑英国银行假期

任何人都知道我该怎么做?

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
欢呼

罗斯

2 个答案:

答案 0 :(得分: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会假定它采用美国格式。你可以改用引号。

答案 1 :(得分: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