excel 通过MySQL删除具有多个条件的重复项

dauxcl2d  于 2023-10-22  发布在  Mysql
关注(0)|答案(3)|浏览(206)

我有一个报告,保存显示在Excel中。此报告列出每个员工/每天的多个时钟打卡。我需要一种方法来清除列D和E之间的任何重复,只留下唯一的值。这里有一个问题,它需要查看当天的特定员工,然后在整个报告中为每个员工执行此操作(而不是查看前一天)。因此,如果Base 1在周一和周三使用7:59的时钟,它不会认为这些是重复的。我将尝试附上一个例子的屏幕截图。它只显示一天,但实际报告包含一周的数据。此外,如果D列和E列中有两个空白单元格(并排),则需要删除整行。

nr9pn0ug

nr9pn0ug1#

扫描工作表,将开始时间与上一行结束时间进行比较。

  1. Option Explicit
  2. Sub RemoveDuplicates()
  3. Dim r As Long, lastrow1 As Long, lastrow2 as long
  4. Dim d As Long, t0 As Single: t0 = Timer
  5. Application.ScreenUpdating = False
  6. With ThisWorkbook.Sheets("Sheet1")
  7. lastrow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
  8. ' remove blank lines first
  9. For r = lastrow1 To 2 Step -1
  10. If Len(.Cells(r, "D") & Cells(r, "E")) = 0 Then
  11. .Rows(r).Delete
  12. d = d + 1
  13. End If
  14. Next
  15. lastrow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
  16. For r = lastrow2 To 3 Step -1
  17. ' compare with row above
  18. If (.Cells(r, "A") = .Cells(r - 1, "A")) And _
  19. (.Cells(r, "C") = .Cells(r - 1, "C")) And _
  20. (.Cells(r, "D") = .Cells(r - 1, "E")) Then
  21. ' update finish time line above
  22. .Cells(r - 1, "E") = .Cells(r, "E")
  23. .Rows(r).Delete
  24. '.Rows(r).Interior.Color = vbRed
  25. d = d + 1
  26. End If
  27. Next
  28. End With
  29. Application.ScreenUpdating = True
  30. MsgBox lastrow1 - 1 & " rows scanned" & vbLf & _
  31. d & " rows deleted", vbInformation, Format(Timer - t0, "0.0 secs")
  32. End Sub
展开查看全部
mqxuamgl

mqxuamgl2#

如果你不想这样做,我能想到的最简单的方法是:
1.首先,按员工和日期排序。
1.创建两个新列(我将使用F和G)
1.在第一个新列和第二行(F2)中,输入公式:

  1. =IF(OR(B2<>B1,C2<>C1,D2<>E1),D2,F1)

1.在第二列(G2)中,输入:

  1. =IF(OR(B3<>B2,C3<>C2,D3<>E2),E2,G3)

1.把公式抄下来。
1.您将看到列现在包含正确的IN时间和正确的OUT时间。
1.复制并粘贴为原始值。
1.使用数据/删除重复项功能进行清理。
一些解释:第一列使用B2<>B1测试这是否是新员工,使用C2<>C1测试这是否是新日期,以及此IN时间是否不同于先前的OUT时间(D2<>E2)。在所有这些情况下,采用来自该行的IN时间(D2)。否则,先前的IN时间(D1)仍然有效。
第二列的功能几乎相同,但它将雇员、日期和OUT时间与NEXT行进行比较。这样,我们就可以收集最后所需的OUT时间。
对于空白行,我只使用自动过滤器来选择它们并手动删除。
您可以尝试将这些步骤保存为宏,但我不确定这是否有效。

展开查看全部
u4vypkhs

u4vypkhs3#

请尝试。

  1. Option Explicit
  2. Sub DEMO()
  3. Dim rngData As Range, arrData, arrRes
  4. Dim i As Long, j As Long, RowCnt As Long, ColCnt As Long
  5. Dim resSht As Worksheet
  6. Dim sKey As String, sNewKey As String, iIndex As Long
  7. Dim StartRow As Long, EndRow As Long, LastTime2
  8. ' *** Modify as needed
  9. Const SOURCE_SHEET = "Data"
  10. Const OUPUT_SHEET = "Result"
  11. Const SEP_CHAR = "|"
  12. ' ***
  13. Const TIME2_COL = 5 ' Column E
  14. On Error Resume Next
  15. Set resSht = Sheets(OUPUT_SHEET)
  16. On Error GoTo 0
  17. ' Copy source table to output sheet
  18. If resSht Is Nothing Then
  19. Set resSht = Sheets(SOURCE_SHEET).Copy(After:=Sheets(Sheets.Count))
  20. resSht.Name = OUPUT_SHEET
  21. Else
  22. resSht.Cells.Clear
  23. With Sheets(SOURCE_SHEET)
  24. .Range(.Cells(1, 1).End(xlToRight), .Cells(.Rows.Count, 1).End(xlUp)).Copy resSht.Range("A1")
  25. End With
  26. End If
  27. ' Sort source table on output sheet
  28. With resSht
  29. Set rngData = .Range(.Cells(1, 1).End(xlToRight), .Cells(.Rows.Count, 1).End(xlUp))
  30. End With
  31. ' Modify column header as needed
  32. rngData.Sort key1:="Emp ID", order1:=xlAscending, key2:="Date", _
  33. order2:=xlAscending, key3:="Time1", _
  34. order3:=xlAscending, Header:=xlYes
  35. With rngData
  36. arrData = rngData.Resize(.Rows.Count + 1).Value
  37. End With
  38. RowCnt = UBound(arrData)
  39. ColCnt = UBound(arrData, 2)
  40. ReDim arrRes(1 To RowCnt, 1 To ColCnt)
  41. ' Table header
  42. For j = 1 To ColCnt
  43. arrRes(1, j) = arrData(1, j)
  44. Next j
  45. iIndex = 2: StartRow = 2
  46. ' Initial process
  47. sKey = arrData(2, 1) & SEP_CHAR & arrData(2, 3)
  48. LastTime2 = arrData(2, 4)
  49. For i = 2 To RowCnt
  50. If Len(arrData(i, 4) & arrData(i, 5)) > 0 Or Len(arrData(i, 1)) = 0 Then
  51. sNewKey = arrData(i, 1) & SEP_CHAR & arrData(i, 3)
  52. ' [EmpID+Date change] OR [Current Time1 <> Last Time2]
  53. If sNewKey <> sKey Or Abs(arrData(i, TIME2_COL - 1) - LastTime2) > 0.0000001 Then
  54. For j = 1 To ColCnt
  55. arrRes(iIndex, j) = arrData(StartRow, j)
  56. Next j
  57. arrRes(iIndex, TIME2_COL) = arrData(EndRow, TIME2_COL)
  58. iIndex = iIndex + 1
  59. StartRow = i
  60. sKey = arrData(i, 1) & SEP_CHAR & arrData(i, 3)
  61. End If
  62. LastTime2 = arrData(i, TIME2_COL)
  63. EndRow = i
  64. End If
  65. Next i
  66. ' Write data
  67. With resSht.Range("A1")
  68. .CurrentRegion.ClearContents
  69. .Resize(RowCnt, ColCnt).Value = arrRes
  70. End With
  71. End Sub

展开查看全部

相关问题