2017-08-01 79 views
1

想要比較當天與前一天的值併爲每個項目逐行突出顯示重複值,我的數據有幾行,並且我有每天從文本文件導入數據的宏。 測試數據:突出顯示重複匹配下一列

enter image description here

這裏是我想要的代碼:

子重複()

Dim refRng As Range, cell As Range 
Application.ScreenUpdating = False 
With Worksheets("Sheet1") 
    Set refRng = .Range("B2", .Cells(.Rows.Count, "F").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) 
    For Each cell In .Range("C2", .Cells(.Rows.Count, "D").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) 
     If cell.value <> 0 Then 
      If Not refRng.Find(what:=cell.value, LookIn:=xlFormulas, lookat:=xlWhole, MatchCase:=False) Is Nothing Then cell.Interior.color = RGB(255, 255, 0) 
     End If 
    Next cell 
End With 
Application.ScreenUpdating = True 

結束子

+0

用您的編碼項目的當前狀態更新您的問題。如果你沒有告訴我們你確切的問題,我們不能給你一個確切的解決方案。有關一般問題的一般指導原則,請參閱下面的答案。 –

+0

[這裏有一個類似的問題,也許你可以在這裏找到答案! =)](https://stackoverflow.com/questions/2162684/ms-excel-how-to-create-a-macro-to-find-duplicates-and-highlight-them) – flowers1234

+0

更新的問題與代碼 – sumit

回答

0

我已經修改了你的代碼來實現的結果。以下代碼將從列B開始到列D.相應地更改您的範圍列。

Dim cell As Range 

Application.ScreenUpdating = False 

With Worksheets("Sheet1") 
    'starts with Column C and matches C with B, D with C and so on.. 
    For Each cell In .Range("C2", .Cells(.Rows.Count, "D").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) 
     'cell.Cells(1, 0) will refer previous cell 
     If cell.Value = cell.Cells(1, 0) Then 
      cell.Interior.Color = RGB(255, 255, 0) 
      cell.Cells(1, 0).Interior.Color = RGB(255, 255, 0) 
     End If 
    Next cell 
End With 
+0

This works'Sheets(「Sheet1」)。選擇 對於i = 1到10 如果Range(「B」&i).Value = Range(「C」 &I)。價值和Range( 「B」 &ⅰ)。價值<> 「」 然後 範圍( 「B」 &I)。選擇 隨着Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic 。顏色= 65535 .TintAndShade = 0 .PatternTintAndShade = 0 結束隨着 範圍( 「C」 &I)。選擇 隨着Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic 。顏色= 65535 .TintAndShade = 0 .PatternTintAndShade = 0 結束隨着 結束如果 下一個I ' – sumit

0

對於每一對count所述的出現次數每個值。如果發生多次,color它。

編輯:比較兩個相鄰值,如果它們匹配,在它們上色

+1

你是正確的,只限於相鄰的值可以進一步簡化它。看我的編輯。 –

0

您也可以使用條件格式。選擇要比較,則列:

條件格式>高亮細胞規則>重複值

+0

這不起作用,因爲它突出顯示了所選範圍內常見的所有內容,我需要比較明智的行。 – sumit

+0

您也可以選擇要與之比較的行。但是,是的,我看到你想要做什麼 – Phil