|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Option Explicit
- Private Sub CommandButton1_Click()
- Dim cn As Object, sql$, rs As Object
- Dim arr, brr(), i%, j%, k%
- Set cn = CreateObject("ADODB.CONNECTION")
- cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes;IMEX=2';Data Source=" & ThisWorkbook.FullName
- sql = "SELECT 员工姓名,日期,min(时间),max(时间),'' FROM (SELECT 员工姓名,cdate(format(打卡时间,'yyyy-m-d')) AS 日期,cdate(format(打卡时间,'H:M:S')) AS 时间 FROM [Sheet1$A:B]) GROUP BY 员工姓名,日期 ORDER BY 员工姓名,日期"
- Set rs = cn.Execute(sql)
- If Not (rs.EOF Or rs.BOF) Then
- arr = Application.Transpose(rs.getrows)
- For i = 1 To UBound(arr)
- If DateDiff("s", CDate(arr(i, 3)), CDate(arr(i, 4))) < 120 And CDate(arr(i, 3)) > 0.5 And CDate(arr(i, 2)) + 1 = CDate(arr(IIf(i = UBound(arr), UBound(arr), i + 1), 2)) And CDate(arr(IIf(i = UBound(arr), UBound(arr), i + 1), 3)) < 0.5 Then
- arr(i, 3) = CDate(arr(i, 2)) + CDate(arr(i, 3))
- arr(i, 4) = CDate(arr(IIf(i = UBound(arr), UBound(arr), i + 1), 2)) + CDate(arr(IIf(i = UBound(arr), UBound(arr), i + 1), 3))
- arr(i, 5) = "夜班"
- ElseIf DateDiff("s", CDate(arr(IIf(i = 1, 1, i - 1), 3)), CDate(arr(IIf(i = 1, 1, i - 1), 4))) < 120 And DateDiff("h", CDate(arr(i, 3)), CDate(arr(i, 4))) > 8 Then
- arr(i, 3) = CDate(arr(i, 2)) + CDate(arr(i, 3))
- arr(i, 4) = CDate(arr(i, 2)) + CDate(arr(i, 4))
- arr(i, 5) = "白班"
- ElseIf DateDiff("h", CDate(arr(i, 3)), CDate(arr(i, 4))) > 8 And arr(IIf(i = 1, 1, i - 1), 5) = "白班" And CDate(arr(IIf(i = 1, 1, i - 1), 2)) + 1 = CDate(arr(i, 2)) Then
- arr(i, 3) = CDate(arr(i, 2)) + CDate(arr(i, 3))
- arr(i, 4) = CDate(arr(i, 2)) + CDate(arr(i, 4))
- arr(i, 5) = "白班"
- ElseIf DateDiff("h", CDate(arr(i, 3)), CDate(arr(i, 4))) > 8 And arr(IIf(i = 1, 1, i - 1), 5) = "夜班" Then
- arr(i, 3) = CDate(arr(i, 2)) + CDate(arr(i, 4))
- arr(i, 4) = CDate(arr(IIf(i = UBound(arr), UBound(arr), i + 1), 2)) + CDate(arr(IIf(i = UBound(arr), UBound(arr), i + 1), 3))
- arr(i, 5) = "夜班"
- End If
- Next i
- End If
- Set rs = Nothing
- Set cn = Nothing
- ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2))
- For i = 1 To UBound(arr, 1)
- If Len(arr(i, 5)) > 0 Then
- k = k + 1
- For j = 1 To UBound(arr, 2)
- brr(k, j) = arr(i, j)
- Next j
- End If
- Next i
- '输出结果
- With ActiveSheet.[E2].Resize(UBound(brr, 1), UBound(brr, 2))
- .EntireColumn.Clear
- .Offset(-1, 0) = Array("员工姓名", "日期", "上班时间", "下班时间", "班次")
- .Cells = brr
- End With
- End Sub
复制代码
|
|