如何删除Excel中多列之间的重复数据?

时间:2013-03-24 16:49:33

标签: excel

该表代表每个项目的关键字。关键字具有每个项目的重复条目,并且一个关键字放置在一个单元格中,不同的项目可以具有不同数量的关键字。我该如何删除excel中每个项目的重复项。

例如:
项目关键字
A - > 123 234 456 123 234
B - > 23 456 23 567

删除重复的关键字后,应该是:
项目关键字
A - > 123 456 234
B - > 23 456 567

1 个答案:

答案 0 :(得分:1)

假设每个列都包含一个关键字,这将覆盖您可以设置的范围内的行,它将删除在该行中出现多次的所有关键字。

Sub DeleteDuplicateKeywords()

Dim rng As Range
Dim r As Long 'row iterator
Dim rowRng As Range ' a separate range for each ROW in rng.
Dim c As Long 'column iterator
Dim sKeyword As String
Dim bReview As Boolean

bReview = MsgBox("Do you want to preview which cells will be deleted?", vbYesNo)

Set rng = Range("B13:E18") '<-- change this as necessary for your requirements.

For r = 1 To rng.Rows.Count  'iterate over each ROW in the range

    Set rowRng = rng.Rows(r)

    For c = rowRng.Columns.Count To 1 Step -1 'iterate backwards over the columns, ignoring the first column.

        sKeyword = rowRng.Cells(c).Value 'assign the cell value to a variable

        'Check to see if this keyword exists more than once in this row
        If Application.WorksheetFunction.CountIf(rowRng, sKeyword) > 1 Then
            'if it does, then delete it.
            If bReview Then
                rowRng.Cells(c).Interior.ColorIndex = 39
            Else:
                rowRng.Cells(c).Delete Shift:=xlToLeft
            End If
        End If
    Next
Next

End Sub