2016-03-27 12 views
1

範囲が連続していると仮定&は単一の列です。ハイライト重複範囲が1つの範囲

上記のRANGEの重複した項目を強調したいと思います。私のVBAは次のとおりですが、意図したとおりに機能していません。あなたがでExcel WorksheetFunctionsを使用することができ

Sub CompareSingleRange() 
    Dim rangeToUse1 As Range, cell1 As Range 
    Dim wf As WorksheetFunction 
    Set wf = Application.WorksheetFunction 
    ' Assume Selection is contiguous 
    Set rangeToUse1 = Selection 

     For Each cell1 In rangeToUse1 
       If wf.CountIf(rangeToUse1, cell1) > 1 Then 
        cell1.Interior.ColorIndex = 38 
       End If 
     Next cell1 
End Sub 

enter image description here

答えて

1

:テーマはあなたが唯一の単一のループが必要

Sub CompareSingleRange() 
Dim rangeToUse1 As Range, rangeToUse2 As Range, cell1 As Range, cell2 As Range 
' Assume Selection is contiguous 
Set rangeToUse1 = Selection 
Set rangeToUse2 = Selection 
    For Each cell1 In rangeToUse1 
     For Each cell2 In rangeToUse2 
      If cell1.Value = cell2.Offset(1,0).Value Then 
      cell1.Interior.ColorIndex = 38 
      End If 
     Next cell2 
    Next cell1 
End Sub 
1

をOFFSET使用して、その下の値に最初のセルの値を言うと比較することですこのタスクを完了するにはこれが役立つ

Sub FindDuplicates() 
    Dim rangeToUse1 As Range, cell1 As Range, cell2 As Range 
    Set rangeToUse1 = Selection 
    For Each cell1 In rangeToUse1 
     For Each cell2 In rangeToUse1 
      If cell1.Value = cell2.Value And cell1.Row <> cell2.Row Then 
       cell1.Interior.ColorIndex = 38 
      End If 
     Next cell2 
    Next cell1 
End Sub 

希望:そうでない場合は、「純粋な」VBAソリューションを以下に示します(ちょうど少しあなたのSub追加条件でオリジナルを変更する必要があります)。

+0

素晴らしいです。これは、以前のソリューションの制限を回避します。範囲寸法 – nightcrawler

2

条件付き書式設定ルールを試してください。それがより簡単であればコードしてください。

With Worksheets("Sheet1") 
    With .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)) 
     .FormatConditions.Delete 
     .FormatConditions.AddUniqueValues 
     With .FormatConditions(1) 
      .Interior.Color = vbGreen 
     End With 
    End With 
End With 

cf_duplicates

+0

非常に、非常に良い。 –