2017-03-07 15 views
1

現在、モジュールがボタンに接続されたExcelドキュメントがあります。2行のスキャンと比較

Excelの文書では、次のようになります。

ROW | COLUMN C | COLUMN K 
1  808   253 
2  808   256 
3  908   355 
4  908   355 
5  908   356 
6  907   253 
7  907   253 

私は、次のモジュールがキックオフボタンをクリックすると:

Sub scan() 
    Dim dataRange As Range 
    Dim dataRange2 As Range 
    Dim oneCell As Range 
    Dim oneCell2 As Range 

    With ThisWorkbook.Sheets("Resource Info").Range("C:C") 
     Set dataRange = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) 
    End With 

    With ThisWorkbook.Sheets("Resource Info").Range("K:K") 
     Set dataRange2 = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) 
    End With 

    For Each oneCell In dataRange 
     If Application.WorksheetFunction.CountIf(dataRange, oneCell) > 1 Then 
      For Each oneCell2 In dataRange2 
       If Application.WorksheetFunction.CountIf(dataRange, oneCell) > 1 And Application.WorksheetFunction.CountIf(dataRange2, oneCell2) <> 1 Then 
        With oneCell 
         .EntireRow.Interior.ColorIndex = 6 
        End With 
       End If 
      Next oneCell2 
     End If 
    Next oneCell 
End Sub 

私は行1,2,3を持ってしようとしています、 4,5は列Cと一致するので強調表示されますが、列Kのデータは列Cのグループと一致しません。

私はすべての行が関係なく、列K.

+2

(クイックノートで、あなたもあなたの '' WITH'文で) 'レンジ(使用前に' .'を追加する必要があります) – BruceWayne

+1

あなたがグループ化の基準を詳しく説明してもらえますか?マクロの代わりに、[条件付き書式](http://www.excel-easy.com/data-analysis/conditional-formatting.html)または[sumproduct](https:// exceljet)を使用することもできます。 – Alex

+0

列Cでグループ化が行われるはずです。そのグループ(つまり一致する数値)では、列Kに2つの異なる値が含まれている場合、そのグループの行を強調表示する必要があります。それがうまく説明されることを望みます。 – Alex

答えて

2

に含まれているものを単一Appl;ication.CountIfsであなたの複数Application.CountIfの機能を交換していないハイライトを持っている現在のモジュール。

Sub scan() 
    Dim rw As Long 

    With ThisWorkbook.Sheets("Resource Info") 
     .UsedRange.offset(1, 0).EntireRow.Interior.Pattern = xlNone 
     For rw = 2 To .Cells(.Rows.Count, "C").End(xlUp).Row 
      If CBool(Application.CountIfs(.Columns("C"), .Cells(rw, "C").Value2, .Columns("K"), "<>" & .Cells(rw, "K"))) Then 
       .Rows(rw).EntireRow.Interior.ColorIndex = 6 
      End If 
     Next rw 
    End With 

End Sub 

enter image description here

+0

ありがとう@jeeped - 上記のコードを実行したとき、ヘッダーの色がすべて消えて、強調表示された行はありませんでした。 – Alex

+1

サンプルデータを使ってもう一度試してみてください。成功すると、データと提供したサンプルデータの違いは何かを判断する必要があります。色付きのヘッダー行は一度も言及していません。ハイライトをリセットするときに一番上の行だけを残すように上記のコードをオフセットしました。 – Jeeped

+0

あなたは正しいです。それは魅力のように働く。専門知識を評価してください。私は今CountIfとCountIfsの違いについて学びます。再度、感謝します! – Alex