用VBA编写一个Excel表中的判定上班打卡时间是否迟到或者旷工
时间: 2024-12-02 22:18:54 浏览: 38
在VBA(Visual Basic for Applications)中,你可以通过设置条件判断来检查一个Excel工作表上的打卡时间是否算迟到或旷工。这里是一个基本的示例,假设打卡时间和下班时间为固定列,例如A列是打卡时间,B列是下班时间:
```vba
Sub CheckAttendance()
Dim startTime As Date
Dim endTime As Date
Dim currentTime As Date
Dim isLate As Boolean '用于记录是否迟到
Dim isAbsent As Boolean '用于记录是否旷工
'设置开始时间和结束时间
startTime = Range("A2") '假设从第二行开始
endTime = Range("B2")
'遍历每一行的数据
For Each cell In Range("A2:A" & Rows.Count).Cells
currentTime = cell.Value '获取当前打卡时间
'判定迟到
If currentTime > endTime Then
isLate = True
MsgBox "该员工打卡晚于下班时间,已标记为迟到!"
Else
isLate = False
End If
'判定旷工(这里假设无打卡即视为旷工)
If IsEmpty(currentTime) Then
isAbsent = True
MsgBox "该员工未打卡,已标记为旷工!"
Else
isAbsent = False
End If
'在此行显示结果(例如C列)
cell.Offset(0, 1).Value = isLate '<-- 如果你想在D列显示迟到信息
cell.Offset(0, 2).Value = isAbsent '<-- 如果你想在E列显示旷工信息
Next cell
End Sub
```
阅读全文
相关推荐
















