2016-03-27 21 views
1

Aralık fareyi bitişik & tek bir sütundur.Yinelenen değerleri tek bir aralıkta vurgula

Yukarıdaki RANGE için yinelenen girişleri vurgulamak istiyorum. VBA'm şu şekildedir, ancak amaçlandığı gibi çalışmıyor.

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

cevap

1

You Excel WorksheetFunctions kullanabilirsiniz: tema alt değere ilk hücre değeri

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

Yalnızca tek bir döngü ihtiyaç OFFSET kullanarak söylemek karşılaştırmaktır Bu görevi tamamlamak için sipariş vermek; Bu yardımcı olacaktır

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 

Hope: Aksi, "saf" VBA çözümü aşağıda gösterilmiştir (sadece hafifçe Sub ek koşulu ile orijinal değiştirmeniz gerekir).

+0

Mükemmel. Bu bile, önceki çözümler RANGE boyutunun – nightcrawler

2

Koşullu bir biçimlendirme kuralı deneyin. Daha kolay görünüyorsa kodlayın.

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

çok, çok güzel sınırlandırılmasını engeller. –

İlgili konular