简体   繁体   中英

Compare and highlight unmatching values in two sheets of Excel

  • I have two sheets in excel
    1. Test1
    2. Test2
      Now, I have 42 columns and 100 rows in each sheet.
  • I have to compare both sheets and find all cells whose values are different in both sheets.
    Example: In Test1 sheet if cell A1=Yes and In Test2 sheet if A1=Yes then do nothing else highlight cell A1 with red color in both sheets.
  • Can this be achieved using conditional formatting?
Sub Highlight()
Dim RSource As Range, RTarget As Range, RTest As Range

    ' must properly qualify Cells()
    Set RSource = ActiveWorkbook.Worksheets("Sheet1").Range( _
                      ActiveWorkbook.Worksheets("Sheet1").Cells(1, 1), _
                      ActiveWorkbook.Worksheets("Sheet1").Cells(100, 42))

    ' not strictly needed ... could be as small as Set RTarget = ActiveWorkbook.Worksheets("Sheet2").[A1]
    Set RTarget = ActiveWorkbook.Worksheets("Sheet2").Range( _
                      ActiveWorkbook.Worksheets("Sheet2").Cells(1, 1), _
                      ActiveWorkbook.Worksheets("Sheet2").Cells(100, 42))

    For Each RTest In RSource.Cells

        If RTest <> RTarget.Cells(RTest.Row, RTest.Column) Then
            ' make them red
            RTest.Interior.Color = vbRed
            RTarget.Cells(RTest.Row, RTest.Column).Interior.Color = vbRed

        Else
            ' make them white
            RTest.Interior.Color = vbWhite
            RTarget.Cells(RTest.Row, RTest.Column).Interior.Color = vbWhite

        End If

    Next RTest

End Sub

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM