从行中查找和删除重复值
Finding and removing duplicates values from row
我在 Excel 2007 年工作。我有以下数据:
数据|-v1-|-v2-|-v2-|-v3-|-v4-|
------------------------------
1 |a | |a | |b |
2 | |c |d | | |
3 |e |e |e |e |e |
我正在尝试从每一行中删除重复值。我正在手动进行。
预期输出是这样的:
数据|-v1-|-v2-|-v2-|-v3-|-v4-|
------------------------------
1 |a | | | |b |
2 | |c |d | | |
3 |e | | | | |
如何快速完成此操作?
正如另一位用户 HERE 所述,以下代码应该可以为您解决此问题。
Sub RemoveDuplicatesInRow()
Dim lastRow As Long
Dim lastCol As Long
Dim r As Long 'row index
Dim c As Long 'column index
Dim i As Long
With ActiveSheet.UsedRange
lastRow = .Row + .Rows.Count - 1
lastCol = .Column + .Columns.Count - 1
End With
For r = 1 To lastRow
For c = 1 To lastCol
For i = c + 1 To lastCol 'change lastCol to c+2 will remove adjacent duplicates only
If Cells(r, i) <> "" And Cells(r, i) = Cells(r, c) Then
Cells(r, i) = ""
End If
Next i
Next c
Next r
End Sub
我认为可以(未经测试)使用条件格式中的 =COUNTIF($B2:B2,B2)>1
等公式规则,然后按颜色过滤(四列各一次)以清空 CF 格式值。 (或者可能保留值原样,只是应用格式将重复项混合到背景中!)
考虑:
Sub ClearDuplicatesByRow()
Dim nLastRow As Long, nLastColumn As Long
Dim nFirstRow As Long, nFirstColumn As Long
Dim i As Long, j As Long, wf As WorksheetFunction
Dim rLook As Range, rWhat As Variant
ActiveSheet.UsedRange
Set r = ActiveSheet.UsedRange
Set wf = Application.WorksheetFunction
nLastRow = r.Rows.Count + r.Row - 1
nLastColumn = r.Columns.Count + r.Column - 1
nFirstRow = r.Row
nFirstColumn = r.Column
For i = nFirstRow To nLastRow
For j = nFirstColumn + 1 To nLastColumn
Set rLook = Range(Cells(i, nFirstColumn), Cells(i, j - 1))
rWhat = Cells(i, j).Value
If wf.CountIf(rLook, rWhat) > 0 Then
Cells(i, j).ClearContents
End If
Next j
Next i
End Sub
它会清除重复项,即使它们不相邻。
我在 Excel 2007 年工作。我有以下数据:
数据|-v1-|-v2-|-v2-|-v3-|-v4-|
------------------------------
1 |a | |a | |b |
2 | |c |d | | |
3 |e |e |e |e |e |
我正在尝试从每一行中删除重复值。我正在手动进行。 预期输出是这样的:
数据|-v1-|-v2-|-v2-|-v3-|-v4-|
------------------------------
1 |a | | | |b |
2 | |c |d | | |
3 |e | | | | |
如何快速完成此操作?
正如另一位用户 HERE 所述,以下代码应该可以为您解决此问题。
Sub RemoveDuplicatesInRow()
Dim lastRow As Long
Dim lastCol As Long
Dim r As Long 'row index
Dim c As Long 'column index
Dim i As Long
With ActiveSheet.UsedRange
lastRow = .Row + .Rows.Count - 1
lastCol = .Column + .Columns.Count - 1
End With
For r = 1 To lastRow
For c = 1 To lastCol
For i = c + 1 To lastCol 'change lastCol to c+2 will remove adjacent duplicates only
If Cells(r, i) <> "" And Cells(r, i) = Cells(r, c) Then
Cells(r, i) = ""
End If
Next i
Next c
Next r
End Sub
我认为可以(未经测试)使用条件格式中的 =COUNTIF($B2:B2,B2)>1
等公式规则,然后按颜色过滤(四列各一次)以清空 CF 格式值。 (或者可能保留值原样,只是应用格式将重复项混合到背景中!)
考虑:
Sub ClearDuplicatesByRow()
Dim nLastRow As Long, nLastColumn As Long
Dim nFirstRow As Long, nFirstColumn As Long
Dim i As Long, j As Long, wf As WorksheetFunction
Dim rLook As Range, rWhat As Variant
ActiveSheet.UsedRange
Set r = ActiveSheet.UsedRange
Set wf = Application.WorksheetFunction
nLastRow = r.Rows.Count + r.Row - 1
nLastColumn = r.Columns.Count + r.Column - 1
nFirstRow = r.Row
nFirstColumn = r.Column
For i = nFirstRow To nLastRow
For j = nFirstColumn + 1 To nLastColumn
Set rLook = Range(Cells(i, nFirstColumn), Cells(i, j - 1))
rWhat = Cells(i, j).Value
If wf.CountIf(rLook, rWhat) > 0 Then
Cells(i, j).ClearContents
End If
Next j
Next i
End Sub
它会清除重复项,即使它们不相邻。