PublicPropertyGetIncludeLastDate()AsBoolean
在默認情況下,BusinessDateDiff函數不包括你輸入的最後一天。將該屬性值設為真,程序把你輸入的最後一天包括在計算中。
方法
PublicSubHolidayAdd(dHolidayAsDate)
添加一個假期列表,以便在計算時排除這些假期。在默認情況下,沒有添加任何假期。
PublicSubHolidayRemove(dHolidayAsDate)
從假期列表移除假期
PublicSubHolidayClear()
清除全部假期列表
代碼清單
首先建立一個新的類模塊,名為cBusinessDates,將下面的代碼粘貼進去。
OptionExplicit
PublicIncludeSaturdaysAsBoolean
PublicIncludeSundaysAsBoolean
PrivatembIncludeFirstDateAsBoolean
PrivatembIncludeLastDateAsBoolean
PrivateHolidaysAsNewCollection
PublicSubHolidayAdd(dHolidayAsDate)
OnErrorResumeNext
Holidays.AdddHoliday,"D"&dHoliday
IfErrThen
Err.Clear
EndIf
EndSub
PublicSubHolidayRemove(dHolidayAsDate)
OnErrorResumeNext
Holidays.Remove"D"&dHoliday
IfErrThen
Err.Clear
EndIf
EndSub
PublicSubHolidayClear()
DimxAsLong
Forx=1ToHolidays.Count
Holidays.Remove1
Next
EndSub
PublicPropertyLetIncludeFirstDate(bIncAsBoolean)
mbIncludeFirstDate=bInc
EndProperty
PublicPropertyLetIncludeLastDate(bIncAsBoolean)
mbIncludeLastDate=bInc
EndProperty
PublicPropertyGetIncludeFirstDate()AsBoolean
IncludeFirstDate=mbIncludeFirstDate
EndProperty
PublicPropertyGetIncludeLastDate()AsBoolean
IncludeLastDate=mbIncludeLastDate
EndProperty
'該函數不把dDate2包含在計算中,如果dDate2是星期日,而你又選擇了計算星期日,該日期仍不計算在內。
PublicFunctionBusinessDateDiff(ByValdDate1AsDate,ByValdDate2AsDate)AsLong
DimdCurDateAsDate
DimdLastDateAsDate
DimdFirstDateAsDate
DimlDayCountAsLong
DimeDayAsVbDayOfWeek
DimdHolidayAsVariant
'調整應將哪一個日期包括在計算中(第一個還是第二個)
IfIncludeFirstDateThen
dFirstDate=dDate1
Else
dFirstDate=dDate1 1
EndIf
IfIncludeLastDateThen
dLastDate=dDate2 1
Else
dLastDate=dDate2
EndIf
'在所有日期中循環,並更新日期計數
dCurDate=dFirstDate
DoWhiledCurDate<>dLastDate
eDay=WeekDay(dCurDate)
IfIncludeSaturdaysAndeDay=vbSaturdayThen
lDayCount=lDayCount 1
EndIf
IfIncludeSundaysAndeDay=vbSundayThen
lDayCount=lDayCount 1
EndIf
IfeDay>=vbMondayAndeDay<=vbFridayThen
lDayCount=lDayCount 1
EndIf
dCurDate=dCurDate 1
Loop
'根據假期調整日期計數
ForEachdHolidayInHolidays
'如果假期在你所輸入的日期范圍之內
IfCDate(dHoliday)>=dFirstDateAndCDate(dHoliday)<=dLastDateThen
eDay=WeekDay(CDate(dHoliday))
IfIncludeSaturdaysAndeDay=vbSaturdayThen
lDayCount=lDayCount-1
EndIf
IfIncludeSundaysAndeDay=vbSundayThen
lDayCount=lDayCount-1
EndIf
IfeDay>=vbMondayAndeDay<=vbFridayThen
lDayCount=lDayCount-1
EndIf
EndIf
Next
BusinessDateDiff=lDayCount
EndFunction
PrivateSubClass_Initialize()
IncludeFirstDate=True
IncludeLastDate=False
IncludeSundays=False
IncludeSaturdays=False
EndSub