該excel表有以下結構數組
姓名\日期 | 周1 | 周2 | 周3 | 周4 | 周5 |
張三 | 7:35 18:02 |
7:35 18:02 |
7:46 | 17:56 | |
李四 | 7:35 18:02 |
7:02函數 18:00spa 18:02excel |
須要判斷天天是否遲到早退,並生成考勤表code
用以下自定義函數cal或者calsblog
Public Function cal(ByVal cs As Range) As Integer '計算單元格並返回相應值以下 '沒打卡 1 '正常上下班 0 '遲到 2 '遲到超過2小時 11 '早退 3 '早退超過2小時 12 '遲到+早退 5 '遲到+早退分別都超過2小時 '只有上班打卡,沒有下班打卡 4 '只有上班打卡,遲到,沒下班打卡 2+4=6 '只有下班打卡,沒有上班打卡 7 '只有下班打卡,早退,沒有上班打卡 3+7=10 '一天3次打卡記錄30 '一天n次以上(n>3)打卡記錄 N*10=10n '異常打卡 10:00-15:30打卡 8 Const morning_time = "08:00" Const evening_time = "17:30" '超過120分鐘算曠工 Const offset_point = 120 '單元格每行是一個數組元素lines() Dim lines() As String '單元格有多少行 Dim count As Integer '沒打卡-空值,返回1 至關於count=0 If IsEmpty(cs) Then cal = 1 Exit Function End If count = Len(cs.Text) - Len(Application.WorksheetFunction.Substitute(cs.Text, Chr(10), "")) + 1 '大於3次以上的打卡記錄返回10*n If count >= 3 Then cal = count * 10 Exit Function End If '處理只有一條記錄的,並計算是否上班 If count = 1 Then Dim offset_morning, offset_evening As Integer offset_morning = Hour(CDate(cs.Text) - CDate(morning_time)) * 60 + Minute(CDate(cs.Text) - CDate(morning_time)) offset_evening = Hour(CDate(cs.Text) - CDate(evening_time)) * 60 + Minute(CDate(cs.Text) - CDate(evening_time)) 'MsgBox offset_morning If CDate(cs.Text) <= CDate(morning_time) Then ' 只有上班打卡,沒有下班打卡 cal = 4 'MsgBox cal Exit Function End If If CDate(cs.Text) >= CDate(evening_time) Then ' 只有下班打卡,沒有上班班打卡 cal = 7 Exit Function End If If (CDate(cs.Text) > CDate(morning_time)) And (CDate(cs.Text) < CDate(evening_time)) Then If offset_morning < 120 Then '只有上班打卡,遲到,沒有下班打卡 cal = 6 ElseIf offset_evening < 120 Then '只有下班打卡,早退,沒有上班打卡 cal = 10 Else '異常打卡 cal = 8 End If Exit Function End If 'count=1 End If 'count=2 Dim line1, line2 As String Dim moring_tmp, evening_tmp As Integer morning_tmp = 0 evening_tmp = 0 '提取第一行打卡和第二行打卡時間line1是上班打卡,line2是下班打卡 line1 = Left(cs.Text, 5) line2 = Split(cs.Text, Chr(10))(1) 'MsgBox "line1:" & line1 'MsgBox "line2:" & line2 '分別給出 morning_tmp值:若是line1早於8點則返回0,晚於8點且不超過2小時爲遲到2,超過2小時取值11 offset_morning = Hour(CDate(line1) - CDate(morning_time)) * 60 + Minute(CDate(line1) - CDate(morning_time)) offset_evening = Hour(CDate(line2) - CDate(evening_time)) * 60 + Minute(CDate(line2) - CDate(evening_time)) 'MsgBox offset_morning 'MsgBox offset_evening If CDate(line1) <= CDate(morning_time) Then ' 正常上班打卡早於8點 morning_tmp = 0 ElseIf (CDate(line1) > CDate(morning_time)) And (offset_morning < 120) Then ' 上班遲到 morning_tmp = 2 Else ' 上班遲到超過2小時 morning_tmp = 11 End If '分別給出 evening_tmp值:若是line2晚於於17:30則返回0,早於17:30且不超過2小時爲早退取值3,超過2小時給值12, If CDate(line2) >= CDate(evening_time) Then ' 正常下班打卡 evening_tmp = 0 ElseIf (CDate(line2) < CDate(evening_time)) And (offset_evening < 120) Then ' 早退 evening_tmp = 3 Else ' 早退超過2小時 evening_tmp = 12 End If '最終cal= morning_tmp+evening_tmp cal = morning_tmp + evening_tmp End Function Public Function cals(ByVal cs As Range) As String '計算單元格並返回相應值以下 '沒打卡 1 '正常上下班 0 '遲到 2 '遲到超過2小時 11 '早退 3 '早退超過2小時 12 '遲到+早退 5 '遲到+早退分別都超過2小時 '只有上班打卡,沒有下班打卡 4 '只有上班打卡,遲到,沒下班打卡 2+4=6 '只有下班打卡,沒有上班打卡 7 '只有下班打卡,早退,沒有上班打卡 3+7=10 '一天3次打卡記錄30 '一天n次以上(n>3)打卡記錄 N*10=10n '異常打卡 10:00-15:30打卡 8 Const morning_time = "08:00" Const evening_time = "17:30" '超過120分鐘算曠工 Const offset_point = 120 '單元格每行是一個數組元素lines() Dim lines() As String '單元格有多少行 Dim count As Integer '沒打卡-空值,返回1 至關於count=0 If IsEmpty(cs) Then cals = "休息" Exit Function End If count = Len(cs.Text) - Len(Application.WorksheetFunction.Substitute(cs.Text, Chr(10), "")) + 1 '大於3次以上的打卡記錄返回10*n If count >= 3 Then cals = "異常打卡" & CStr(count) & "次" Exit Function End If '處理只有一條記錄的,並計算是否上班 If count = 1 Then Dim offset_morning, offset_evening As Integer offset_morning = Hour(CDate(cs.Text) - CDate(morning_time)) * 60 + Minute(CDate(cs.Text) - CDate(morning_time)) offset_evening = Hour(CDate(cs.Text) - CDate(evening_time)) * 60 + Minute(CDate(cs.Text) - CDate(evening_time)) 'MsgBox offset_morning If CDate(cs.Text) <= CDate(morning_time) Then ' 只有上班打卡,沒有下班打卡 cals = "無下班打卡" 'MsgBox cal Exit Function End If If CDate(cs.Text) >= CDate(evening_time) Then ' 只有下班打卡,沒有上班班打卡 cals = "無上班打卡" Exit Function End If If (CDate(cs.Text) > CDate(morning_time)) And (CDate(cs.Text) < CDate(evening_time)) Then If offset_morning < 120 Then '只有上班打卡,遲到,沒有下班打卡 cals = "遲到,無下班打卡" ElseIf offset_evening < 120 Then '只有下班打卡,早退,沒有上班打卡 cals = "早退,無上班打卡" Else '異常打卡 cals = "10點15點30之間異常打卡" End If Exit Function End If 'count=1 End If 'count=2 Dim line1, line2 As String Dim moring_tmp, evening_tmp As Integer morning_tmp = 0 evening_tmp = 0 '提取第一行打卡和第二行打卡時間line1是上班打卡,line2是下班打卡 line1 = Left(cs.Text, 5) line2 = Split(cs.Text, Chr(10))(1) 'MsgBox "line1:" & line1 'MsgBox "line2:" & line2 '分別給出 morning_tmp值:若是line1早於8點則返回0,晚於8點且不超過2小時爲遲到2,超過2小時取值11 offset_morning = Hour(CDate(line1) - CDate(morning_time)) * 60 + Minute(CDate(line1) - CDate(morning_time)) offset_evening = Hour(CDate(line2) - CDate(evening_time)) * 60 + Minute(CDate(line2) - CDate(evening_time)) 'MsgBox offset_morning 'MsgBox offset_evening If CDate(line1) <= CDate(morning_time) Then ' 正常上班打卡早於8點 morning_tmp = 0 ElseIf (CDate(line1) > CDate(morning_time)) And (offset_morning < 120) Then ' 上班遲到 morning_tmp = 2 Else ' 上班遲到超過2小時 morning_tmp = 11 End If '分別給出 evening_tmp值:若是line2晚於於17:30則返回0,早於17:30且不超過2小時爲早退取值3,超過2小時給值12, If CDate(line2) >= CDate(evening_time) Then ' 正常下班打卡 evening_tmp = 0 ElseIf (CDate(line2) < CDate(evening_time)) And (offset_evening < 120) Then ' 早退 evening_tmp = 3 Else ' 早退超過2小時 evening_tmp = 12 End If '最終cal= morning_tmp+evening_tmp 'MsgBox morning_tmp + evening_tmp Select Case (morning_tmp + evening_tmp) Case 0 cals = "全勤" Case 1 cals = "休息" Case 2 cals = "遲到" Case 3 cals = "早退" Case 4 cals = "無下班打卡" Case 5 cals = "遲到+早退" Case 6 cals = "上班遲到,下班沒打卡" Case 7 cals = "無上班打卡" Case 8 cals = "10:00-15:30異常打卡" Case 10 cals = "早退,無上班打卡" Case 11 cals = "遲到超2小時" Case 12 cals = "早退超2小時" Case 23 cals = "遲到早退都超2小時" Case Else calse = "異常打卡" & CStr(morning_tmp + evening_tmp) & "次" End Select End Function
Public Function cal(ByVal cs As Range) As Integer
'計算單元格並返回相應值以下'沒打卡 1'正常上下班 0'遲到 2'遲到超過2小時 11'早退 3'早退超過2小時 12'遲到+早退 5'遲到+早退分別都超過2小時'只有上班打卡,沒有下班打卡 4'只有上班打卡,遲到,沒下班打卡 2+4=6'只有下班打卡,沒有上班打卡 7'只有下班打卡,早退,沒有上班打卡 3+7=10'一天3次打卡記錄30'一天n次以上(n>3)打卡記錄 N*10=10n'異常打卡 10:00-15:30打卡 8Const morning_time = "08:00"Const evening_time = "17:30"'超過120分鐘算曠工Const offset_point = 120
'單元格每行是一個數組元素lines()Dim lines() As String'單元格有多少行Dim count As Integer
'沒打卡-空值,返回1 至關於count=0If IsEmpty(cs) Then cal = 1 Exit FunctionEnd If
count = Len(cs.Text) - Len(Application.WorksheetFunction.Substitute(cs.Text, Chr(10), "")) + 1
'大於3次以上的打卡記錄返回10*nIf count >= 3 Then cal = count * 10 Exit FunctionEnd If
'處理只有一條記錄的,並計算是否上班If count = 1 Then
Dim offset_morning, offset_evening As Integer
offset_morning = Hour(CDate(cs.Text) - CDate(morning_time)) * 60 + Minute(CDate(cs.Text) - CDate(morning_time))offset_evening = Hour(CDate(cs.Text) - CDate(evening_time)) * 60 + Minute(CDate(cs.Text) - CDate(evening_time))'MsgBox offset_morning If CDate(cs.Text) <= CDate(morning_time) Then ' 只有上班打卡,沒有下班打卡 cal = 4 'MsgBox cal Exit Function End If If CDate(cs.Text) >= CDate(evening_time) Then ' 只有下班打卡,沒有上班班打卡 cal = 7 Exit Function End If If (CDate(cs.Text) > CDate(morning_time)) And (CDate(cs.Text) < CDate(evening_time)) Then If offset_morning < 120 Then '只有上班打卡,遲到,沒有下班打卡 cal = 6 ElseIf offset_evening < 120 Then '只有下班打卡,早退,沒有上班打卡 cal = 10 Else '異常打卡 cal = 8 End If Exit Function End If 'count=1End If
'count=2Dim line1, line2 As StringDim moring_tmp, evening_tmp As Integermorning_tmp = 0evening_tmp = 0'提取第一行打卡和第二行打卡時間line1是上班打卡,line2是下班打卡line1 = Left(cs.Text, 5)line2 = Split(cs.Text, Chr(10))(1)'MsgBox "line1:" & line1'MsgBox "line2:" & line2
'分別給出 morning_tmp值:若是line1早於8點則返回0,晚於8點且不超過2小時爲遲到2,超過2小時取值11
offset_morning = Hour(CDate(line1) - CDate(morning_time)) * 60 + Minute(CDate(line1) - CDate(morning_time))offset_evening = Hour(CDate(line2) - CDate(evening_time)) * 60 + Minute(CDate(line2) - CDate(evening_time))
'MsgBox offset_morning'MsgBox offset_evening
If CDate(line1) <= CDate(morning_time) Then ' 正常上班打卡早於8點 morning_tmp = 0 ElseIf (CDate(line1) > CDate(morning_time)) And (offset_morning < 120) Then ' 上班遲到 morning_tmp = 2 Else ' 上班遲到超過2小時 morning_tmp = 11 End If
'分別給出 evening_tmp值:若是line2晚於於17:30則返回0,早於17:30且不超過2小時爲早退取值3,超過2小時給值12,
If CDate(line2) >= CDate(evening_time) Then ' 正常下班打卡 evening_tmp = 0 ElseIf (CDate(line2) < CDate(evening_time)) And (offset_evening < 120) Then ' 早退 evening_tmp = 3 Else ' 早退超過2小時 evening_tmp = 12 End If
'最終cal= morning_tmp+evening_tmpcal = morning_tmp + evening_tmp
End Function
Public Function cals(ByVal cs As Range) As String
'計算單元格並返回相應值以下'沒打卡 1'正常上下班 0'遲到 2'遲到超過2小時 11'早退 3'早退超過2小時 12'遲到+早退 5'遲到+早退分別都超過2小時'只有上班打卡,沒有下班打卡 4'只有上班打卡,遲到,沒下班打卡 2+4=6'只有下班打卡,沒有上班打卡 7'只有下班打卡,早退,沒有上班打卡 3+7=10'一天3次打卡記錄30'一天n次以上(n>3)打卡記錄 N*10=10n'異常打卡 10:00-15:30打卡 8Const morning_time = "08:00"Const evening_time = "17:30"'超過120分鐘算曠工Const offset_point = 120
'單元格每行是一個數組元素lines()Dim lines() As String'單元格有多少行Dim count As Integer
'沒打卡-空值,返回1 至關於count=0If IsEmpty(cs) Then cals = "休息" Exit FunctionEnd If
count = Len(cs.Text) - Len(Application.WorksheetFunction.Substitute(cs.Text, Chr(10), "")) + 1
'大於3次以上的打卡記錄返回10*nIf count >= 3 Then cals = "異常打卡" & CStr(count) & "次" Exit FunctionEnd If
'處理只有一條記錄的,並計算是否上班If count = 1 Then
Dim offset_morning, offset_evening As Integer
offset_morning = Hour(CDate(cs.Text) - CDate(morning_time)) * 60 + Minute(CDate(cs.Text) - CDate(morning_time))offset_evening = Hour(CDate(cs.Text) - CDate(evening_time)) * 60 + Minute(CDate(cs.Text) - CDate(evening_time))'MsgBox offset_morning If CDate(cs.Text) <= CDate(morning_time) Then ' 只有上班打卡,沒有下班打卡 cals = "無下班打卡" 'MsgBox cal Exit Function End If If CDate(cs.Text) >= CDate(evening_time) Then ' 只有下班打卡,沒有上班班打卡 cals = "無上班打卡" Exit Function End If If (CDate(cs.Text) > CDate(morning_time)) And (CDate(cs.Text) < CDate(evening_time)) Then If offset_morning < 120 Then '只有上班打卡,遲到,沒有下班打卡 cals = "遲到,無下班打卡" ElseIf offset_evening < 120 Then '只有下班打卡,早退,沒有上班打卡 cals = "早退,無上班打卡" Else '異常打卡 cals = "10點15點30之間異常打卡" End If Exit Function End If 'count=1End If
'count=2Dim line1, line2 As StringDim moring_tmp, evening_tmp As Integermorning_tmp = 0evening_tmp = 0'提取第一行打卡和第二行打卡時間line1是上班打卡,line2是下班打卡line1 = Left(cs.Text, 5)line2 = Split(cs.Text, Chr(10))(1)'MsgBox "line1:" & line1'MsgBox "line2:" & line2
'分別給出 morning_tmp值:若是line1早於8點則返回0,晚於8點且不超過2小時爲遲到2,超過2小時取值11
offset_morning = Hour(CDate(line1) - CDate(morning_time)) * 60 + Minute(CDate(line1) - CDate(morning_time))offset_evening = Hour(CDate(line2) - CDate(evening_time)) * 60 + Minute(CDate(line2) - CDate(evening_time))
'MsgBox offset_morning'MsgBox offset_evening
If CDate(line1) <= CDate(morning_time) Then ' 正常上班打卡早於8點 morning_tmp = 0 ElseIf (CDate(line1) > CDate(morning_time)) And (offset_morning < 120) Then ' 上班遲到 morning_tmp = 2 Else ' 上班遲到超過2小時 morning_tmp = 11 End If
'分別給出 evening_tmp值:若是line2晚於於17:30則返回0,早於17:30且不超過2小時爲早退取值3,超過2小時給值12,
If CDate(line2) >= CDate(evening_time) Then ' 正常下班打卡 evening_tmp = 0 ElseIf (CDate(line2) < CDate(evening_time)) And (offset_evening < 120) Then ' 早退 evening_tmp = 3 Else ' 早退超過2小時 evening_tmp = 12 End If
'最終cal= morning_tmp+evening_tmp
'MsgBox morning_tmp + evening_tmpSelect Case (morning_tmp + evening_tmp)
Case 0cals = "全勤"Case 1 cals = "休息"Case 2 cals = "遲到"Case 3 cals = "早退"Case 4 cals = "無下班打卡"Case 5 cals = "遲到+早退"Case 6 cals = "上班遲到,下班沒打卡"Case 7 cals = "無上班打卡"Case 8 cals = "10:00-15:30異常打卡"Case 10 cals = "早退,無上班打卡"Case 11 cals = "遲到超2小時"Case 12 cals = "早退超2小時"Case 23 cals = "遲到早退都超2小時"Case Else calse = "異常打卡" & CStr(morning_tmp + evening_tmp) & "次"
End Select
End Functionit