|
请参考附件:- Option Explicit
- Private Function test_days(s_date As Range, e_date As Range, sp_holidays As Range, norest_days As Range) As Integer
- ' Dim start_date, end_date 前面代码是尝试使用时间或者单元格引用,目前使用具体时间不work
- ' If IsObject(s_date) Then
- ' start_date = s_date.Value
- ' Else
- ' start_date = s_date
- ' End If
- ' If IsObject(e_date) Then
- ' end_date = e_date.Value
- ' Else
- ' end_date = e_date
- ' End If
- If TypeName(start_date) <> "Date" Or TypeName(end_date) <> "Date" Then Exit Function
- If end_date - start_date < 0 Then Exit Function
- If s_date.Count > 1 Or e_date.Count > 1 Then '判断起始终止日期单元格
- MsgBox "起始日期和终止日期只能是唯一单元格", 16, "错误"
- Exit Function
- End If
- If TypeName(s_date.Value) <> "Date" Or TypeName(e_date.Value) <> "Date" Then '判断,选择起始和终止时间的单元格是否为日期值,如果不是就退出
- MsgBox "起始时间或者终止时间单元格内容不是日期值", 16, "错误"
- Exit Function
- End If
- If (e_date.Value - s_date.Value) < 0 Then
- MsgBox "目前暂不能支持终止时间在起始时间之前的计算", 16, "错误"
- End If
- Dim rng As Range
- For Each rng In norest_days
-
- If TypeName(rng.Value) <> "Date" Then '判断删减休息日的单元格引用是否是日期值,如果不是退出
- MsgBox "单元格" & rng.Address & "不是日期型数据", 16, "错误"
- Exit Function
- End If
-
- If rng.Value < s_date.Value Or rng.Value > e_date.Value Then
- MsgBox "单元格" & rng.Address & "日期越界,不在起始终止日期范围内", 16, "错误"
- Exit Function
- End If
-
- If WorksheetFunction.Weekday(rng.Value, 2) <> 6 And WorksheetFunction.Weekday(rng.Value, 2) <> 7 Then '判断是否为周6,日系统函数确认的休息日,如果不是就退出
- MsgBox "单元格" & rng.Address & "内容不是系统内假期", 16, "错误"
- Exit Function
- End If
-
- Next
- If sp_holidays Is Nothing Then
- test_days = WorksheetFunction.networkdays(s_date, e_date)
- Else
- test_days = WorksheetFunction.networkdays(s_date, e_date, sp_holidays)
- End If
- Dim no_rest As Integer
- no_rest = norest_days.Count
- test_days = test_days + no_rest
- End Function
-
复制代码 |
|