更新评论 此相匹配的输出所需
Option Explicit
Sub RemoveDuplicates()
Dim rng As Range, c As Range, rCell As Range
Dim temp As Range
' Update this to reference your sheet
With Sheet1
Set rng = .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1))
End With
For Each rCell In rng
Set c = Nothing
If rCell.Offset(0, 1) = vbNullString Then
With rCell.Offset(0, 1)
Set temp = Range(.End(xlUp), .End(xlDown).Offset(-1, 0)).Offset(0, 3)
End With
Set c = temp.Find(rCell.Offset(0, 4), lookat:=xlWhole, after:=rCell.Offset(0, 4))
If Not c Is Nothing Then
If rCell.Offset(0, 5) = c.Offset(0, 1) And c.Row <> rCell.Row Then
Range(rCell.Offset(0, 4), rCell.Offset(0, 5)).ClearContents
End If
End If
End If
Next rCell
End Sub
看一看下面。循环遍历工作表中的所有行,并且如果B列中的单元格为空,则尝试查找它是否存在于工作表中的其他位置。如果是,则清除该行的内容。
我认为你需要定义更多你认为重复的东西。至于你的问题你:
- 假ROW3(ROW6复印件)
- 删除2行(不重复,除非你忽视客户)
所以,你必须在你的逻辑休息。如果您比较客户(即离开Row3),那么只有row6应该被删除。但是,如果您没有比较客户以及重复项的部分内容,那么row3也应该从期望的结果中删除。
Option Explicit
Public Sub RemoveDuplicates()
Dim rng As Range, c As Range, rCell As Range
' Update this to reference your sheet
With Sheet1
Set rng = .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1))
End With
For Each rCell In rng
Set c = Nothing
If rCell.Offset(0, 1) = vbNullString Then
Set c = rng.Offset(0, 4).Find(rCell.Offset(0, 4), lookat:=xlWhole, after:=rCell.Offset(0, 4))
If Not c Is Nothing Then
'' If not including customer in comparison
If rCell.Offset(0, 5) = c.Offset(0, 1) And c.Row <> rCell.Row Then
'' Uncomment below and comment above if comparing customers as well
'If rCell.Offset(0, 5) = c.Offset(0, 1) And rCell.Offset(0, 1).Value = c.Offset(0, -3).Value And c.Row <> rCell.Row Then
Range(rCell.Offset(0, 4), rCell.Offset(0, 5)).ClearContents
End If
End If
End If
Next rCell
End Sub
你尝试过什么?这是一个非常简单的VBA循环。您想保留行,还是在列B空白时将其删除? – BruceWayne
重复将始终是名称行中的一个,还是会有两个相同的事物没有名称,因此Brian会有两个鼠标,或只有Iphone,因为这是Brian在同一行中的那个? –
请查看@Tom的答案。它给你你想要的东西。 “如果有用,请点击答案旁边的复选标记,不要忘记给服务员打电话。” – Masoud