我在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 | | | | |
如何快速完成这项工作?
答案 0 :(得分:0)
正如其他用户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
答案 1 :(得分:0)
我认为可能(未经测试)使用条件格式中的=COUNTIF($B2:B2,B2)>1
等公式规则,然后按颜色过滤(每列四列一次)以消除CF格式的值。 (或者可以保留原样的值,只需应用格式将副本混合到背景中!)
答案 2 :(得分:0)
考虑:
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
即使它们不相邻,它也会清除重复。