根据单元格中的重复项删除整行,而没有具有特定单元格值的行
Remove entire row based on duplicate in cell without the one with a specific cell value
我在 A 列中的数据的快速示例:
- xxxNew
- xxx更新
- yyyNew
- yyy更新
- zzzNew
- aaaNew
- bbbNew
这是我想要的输出:
- xxx更新
- yyy更新
- zzzNew
- aaaNew
- bbbNew
我想搜索单元格值中没有 New 和 Update 的重复项,然后删除带有 New 的行以仅具有 Update 值。但是,有些值根本没有更新,我需要保留它们。 xxx"Update" 可以有多个值,但 zzz"New".
只能有一个
到目前为止,我只是设法删除了所有包含 New 的行,但这太多了。
这将起作用,但前提是没有单个 'New' 行的行数高于 'Update' 行的行数。这是因为 (1) 宏从最高行到最低行 # 和 (2) 只有第一个 'Update' 遇到的行被保存在字典中。之后,具有相同键的 'New' 或 'Update' 的所有较低行都将被删除。因此它可能不像您需要的那样健壮 - 但它是一个起点:
Option Explicit
Sub deleteDuplicateNew()
With Sheets(1)
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim val As String, str As String
Dim lastRow As Integer, r As Integer
lastRow = .Range("A" & .Rows.Count).End(xlUp).row
For r = lastRow To 1 Step -1:
val = .Cells(r, "A").Value
If InStr(1, val, "New") Then
str = Replace(val, "New", "")
If dict.Exists(str) Then
.Cells(r, "A").EntireRow.Delete
End If
ElseIf InStr(1, val, "Update") Then
str = Replace(val, "Update", "")
If dict.Exists(str) Then
.Cells(r, "A").EntireRow.Delete
Else
dict(str) = True
End If
End If
Next
End With
End Sub
我在 A 列中的数据的快速示例:
- xxxNew
- xxx更新
- yyyNew
- yyy更新
- zzzNew
- aaaNew
- bbbNew
这是我想要的输出:
- xxx更新
- yyy更新
- zzzNew
- aaaNew
- bbbNew
我想搜索单元格值中没有 New 和 Update 的重复项,然后删除带有 New 的行以仅具有 Update 值。但是,有些值根本没有更新,我需要保留它们。 xxx"Update" 可以有多个值,但 zzz"New".
只能有一个到目前为止,我只是设法删除了所有包含 New 的行,但这太多了。
这将起作用,但前提是没有单个 'New' 行的行数高于 'Update' 行的行数。这是因为 (1) 宏从最高行到最低行 # 和 (2) 只有第一个 'Update' 遇到的行被保存在字典中。之后,具有相同键的 'New' 或 'Update' 的所有较低行都将被删除。因此它可能不像您需要的那样健壮 - 但它是一个起点:
Option Explicit
Sub deleteDuplicateNew()
With Sheets(1)
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim val As String, str As String
Dim lastRow As Integer, r As Integer
lastRow = .Range("A" & .Rows.Count).End(xlUp).row
For r = lastRow To 1 Step -1:
val = .Cells(r, "A").Value
If InStr(1, val, "New") Then
str = Replace(val, "New", "")
If dict.Exists(str) Then
.Cells(r, "A").EntireRow.Delete
End If
ElseIf InStr(1, val, "Update") Then
str = Replace(val, "Update", "")
If dict.Exists(str) Then
.Cells(r, "A").EntireRow.Delete
Else
dict(str) = True
End If
End If
Next
End With
End Sub