0

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

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

而删除重复关键字后,应该是:
item keywords
A -> 123 456 234
B -> 23 456 567

4

1 回答 1

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
于 2013-03-25T00:18:23.507 回答