|
本帖最后由 sunya_0529 于 2022-6-16 13:07 编辑
这种时间存储格式的,用公式非常麻烦。写了一个自定义函数——
- Option Explicit
- Function KaoQin(rngKQ As Range, rngSJ As Range, str As String)
- 'rngKQ 定义“考勤组”所在单元格区域
- 'rngSJ 定义“考勤时间”所在单元格区域
- 'str 定义要统计的数据类别
- With Application.WorksheetFunction
- If Not rngKQ Is Nothing And Not rngSJ Is Nothing Then
- Dim c As Range, i%, d As Double
- Const w As Integer = 8 '定义每日标准工作时数
- For Each c In rngSJ
- d = 0 '求每天两个时间之差,精确到0.5小时,不足0.5小时的忽略不计
- If Len(c.Value) > 0 Then
- d = CDate(Split(c.Value, Chr(10))(1)) - CDate(Split(c.Value, Chr(10))(0))
- d = Hour(d) + Minute(d) / 60 + Second(d) / 3600
- d = Int(d / 0.5) * 0.5
- End If
- Select Case str
- Case "考勤" '统计有时间记录的出勤天数
- If d > 0 Then KaoQin = KaoQin + 1
- Case "节假日" '统计标记为“节”的节假日加班时数
- i = i + 1
- If rngKQ.Cells(i).Value = "节" Then
- KaoQin = KaoQin + .Max(d - w, 0)
- End If
- Case "休息日" '统计标记为“休”的休息日加班时数
- i = i + 1
- If rngKQ.Cells(i).Value = "休" Then
- KaoQin = KaoQin + .Max(d - w, 0)
- End If
- Case "工作日" '统计工作日加班时数
- i = i + 1
- If rngKQ.Cells(i).Value <> "休" And rngKQ.Cells(i).Value <> "节" Then
- KaoQin = KaoQin + .Max(d - w, 0)
- End If
- End Select
- Next
- End If
- End With
- End Function
复制代码
|
|