2016-01-21 56 views
3

我有一個數據集,其中每行屬於一個獨特的人,所以我想要做的就是在每一行中找到重複的值。Excel如何在一行中查找重複的單元格或值?

我嘗試使用條件格式,但它非常耗時,因爲我必須將其應用到每個單獨的行,否則它將在所有行中找到重複,而不僅僅是一行。

enter image description here

能否請您提出好的建議,可以幫助我也可以是公式或VBA或公式的條件格式。

我用宏記錄器來創建一個宏,結果如下。如果我可以使它通過一系列的行並應用可幫助的格式

Sub DuplicatesRow1() ' ' DuplicatesRow Macro ' 

' 
    Rows("251:251").Select 
    Selection.FormatConditions.AddUniqueValues 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    Selection.FormatConditions(1).DupeUnique = xlDuplicate 
    With Selection.FormatConditions(1).Font 
     .Color = -16383844 
     .TintAndShade = 0 
    End With 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 13551615 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
    Rows("252:252").Select 
    Selection.FormatConditions.AddUniqueValues 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    Selection.FormatConditions(1).DupeUnique = xlDuplicate 
    With Selection.FormatConditions(1).Font 
     .Color = -16383844 
     .TintAndShade = 0 
    End With 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 13551615 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
    Rows("253:253").Select 
    Selection.FormatConditions.AddUniqueValues 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    Selection.FormatConditions(1).DupeUnique = xlDuplicate 
    With Selection.FormatConditions(1).Font 
     .Color = -16383844 
     .TintAndShade = 0 
    End With 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 13551615 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
    Range("E259").Select End Sub 
+1

這似乎是一個糟糕的Excel設計缺陷。你確實在這裏放了'excel-vba'標籤。你有沒有試圖用代碼來完成這個任務?如果是這樣,請提供您所嘗試的。否則,這個問題有可能會被拒絕並被關閉。另一種選擇 - 也許輸入每行格式的時間更少 - 是從行到行復制和粘貼格式;儘管這對於大量的行數顯然是麻煩的。 –

+1

另外,如果您提供一些示例數據來詳細說明您的問題,這會增加獲得良好答案的機會。 – Michael

+0

@ScottHoltzman添加了Vba代碼 –

回答

0

這裏是一個循環,它將在每一行上設置條件格式。我根據您的樣本數據和代碼使用了工作表和範圍參考。您可以修改這些以適合您的確切數據集。

我還會注意到,如果存在大量行,我擔心會導致Excel中的性能問題,因爲格式量可能會嚴重增加文件大小並影響性能。

Sub LoopCF() 

Dim ws As Worksheet 
Set ws = Sheets("Sheet1") 

'Dim lRow As Long 
'lRow = ws.Range("A2").End(xlDown).Row 'will give row 200 as long as contiguous rows 

Dim rng As Range, cel As Range 
Set rng = ws.Range("B2:B200") 'ws.Range("B2:B" & lRow) 

For Each cel In rng 

    With cel.Resize(1, 4) 

     .FormatConditions.AddUniqueValues 
     .FormatConditions(.FormatConditions.Count).SetFirstPriority 

     With .FormatConditions(1) 

      .DupeUnique = xlDuplicate 

      With .Font 
       .Color = -16383844 
       .TintAndShade = 0 
      End With 

      With .Interior 
       .PatternColorIndex = xlAutomatic 
       .Color = 13551615 
       .TintAndShade = 0 
      End With 

      .StopIfTrue = False 

     End With 

    End With 

Next 

End Sub 
+0

謝謝。運行此代碼時出現錯誤。當我回家時我會修補它。你也可以說明lRow = ws.Range(「A251」)。End(xlDown).Row和Set rng = ws.Range(「B251:B」&lRow)是用來表示這兩個值是否讓我困惑?假設我想將此應用於第2行至200行,我該怎麼做? –

+0

@AAZ - 查看我編輯的代碼。 'lRow'只是獲取Range中的最後一行,而'rng'則將所需的範圍設置爲與對象一起工作,因此它可以在該對象上工作。此外,如果你告訴我你得到的錯誤和在哪一行,我可以幫助:) –

+0

我試過更新的代碼,這次沒有錯誤,但它什麼也沒有做:(工作表的名稱是Sheet1。 –

1

我進一步研究了這個,並設法想出了下面的代碼,它似乎對我很有用。我是VBA新手,沒有足夠的經驗,所以請讓我知道如果我的代碼可以進一步提高

Private Sub HighlightDuplicateRow(row As Integer) 

Dim report As Worksheet 
Set report = Excel.ActiveSheet 
report.Cells(row, row).EntireRow.Select 
    Selection.FormatConditions.AddUniqueValues 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    Selection.FormatConditions(1).DupeUnique = xlDuplicate 
    With Selection.FormatConditions(1).Font 
     .Color = -16383844 
     .TintAndShade = 0 
    End With 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 13551615 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
End Sub 


Sub DuplicatesInEachRow() 
Dim counter As Integer, limit As Variant 
counter = 2 
limit = InputBox("Give me last row number", "Highlight Duplicates in a Row") 
If limit = "" Then Exit Sub 
Do Until counter > limit 
Call HighlightDuplicateRow(counter) 
counter = counter + 1 
Loop 
End Sub 
相關問題