假設每列都包含一個關鍵字,這將覆蓋您可以設置的範圍內的行,排除在該行中出現多次的任何關鍵字。
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
你說「關鍵字有每個項目重複的條目」,然後「不同的項目可以有不同數量的關鍵字」,所以我不知道哪個包含哪些。我也不能說「A」是代表多個細胞還是代表多個...細胞的單個細胞......用空格分開。它也不能幫助您標記具有通常Excel列標題的行。另外,什麼版本的Excel? – 2013-03-24 20:08:48
在你的例子中,每個關鍵字是否在一個單獨的單元格中?例如:'A1 =「A」','B1 =「123」','C1 =「234」','D1 =「456」'....或者它是'A1 =「A」','B1 =「123 234 456 123 234」' – NickSlash 2013-03-24 20:52:22
它是A1 =「A」,B1 =「123」,C1 =「234」,D1 =「456」。 – elephant 2013-03-25 04:09:49