Conditionally formatting cells if their value equals any value of another column

后端 未结 6 1584
温柔的废话
温柔的废话 2020-12-29 07:50

I have data in the A and B columns. B column\'s data is mostly duplicates of A\'s data, but not always. For example:

6条回答
  •  情书的邮戳
    2020-12-29 08:02

    I was looking into this and loved the approach from peege using a for loop! (because I'm learning VBA at the moment)

    However, if we are trying to match "any" value of another column, how about using nested loops like the following?

    Sub MatchAndColor()
    
    Dim lastRow As Long
    Dim sheetName As String
    
    
    sheetName = "Sheet1"            'Insert your sheet name here
    lastRow = Sheets(sheetName).Range("A" & Rows.Count).End(xlUp).Row
    
    For lRowA = 1 To lastRow         'Loop through all rows
        For lRowB = 1 To lastRow
            If Sheets(sheetName).Cells(lRowA, "A") = Sheets(sheetName).Cells(lRowB, "B") Then
    
            Sheets(sheetName).Cells(lRowA, "A").Interior.ColorIndex = 3  'Set Color to RED
        End If
    
    Next lRowB
    Next lRowA
    
    End Sub
    

提交回复
热议问题