我有这个:
Public Function WorkingDays(StartDate As Date, _
ByVal EndDate As Date, _
Optional ByVal Holidays As Range, _
Optional ByVal Workdays As Variant) As Long
Dim mpDays As Long
Dim mpHolidays As Variant
Dim mpWorkdays As Variant
Dim i As Long
If Not Holidays Is Nothing Then mpHolidays = Application.Transpose(Holidays)
If IsMissing(Workdays) Then
mpWorkdays = Array(2, 3, 4, 5, 6)
Else
mpWorkdays = Workdays
End If
For i = StartDate To EndDate
If Not IsError(Application.Match(Weekday(i), mpWorkdays, 0)) Then
If IsError(Application.Match(i, mpHolidays, 0)) Then
mpDays = mpDays + 1
End If
End If
Next i
WorkingDays = mpDays
End Function
但是很慢, 我在我的工作簿中使用此功能,而我的表单中有130K记录。如何改进?
答案 0 :(得分:4)
您是否看到Excel具有NETWORKDAYS和WORKDAYS功能?
为什么不计算两个日期之间的工作日天数,然后循环假期以查看每个日期是否介于StartDate和EndDate之间,而不是循环遍历从StartDate到EndDate的日期。 p>
答案 1 :(得分:0)
此链接具有Networkdays的公式版本(不使用实际的Networkdays功能)和VBA版本:http://www.cpearson.com/excel/betternetworkdays.aspx
答案 2 :(得分:0)
我改变计算工作日的方法,它在00:00:32工作
Public Function WrkDaysCount(StartDate As Date, _
ByVal endDate As Date) As Long
Dim DayStart As Long
Dim DayEnd As Long
Dim daytot As Long
Dim Nrweeks As Long
DayStart = Weekday(StartDate, vbMonday)
DayEnd = endDate - StartDate + DayStart
Nrweeks = Int(DayEnd / 7)
daytot = DayEnd - (fest * 2) - DayStart + 1
WrkDaysCount = daytot
End Function