在两张Excel中比较并突出显示不匹配的值

时间:2014-03-25 07:22:51

标签: excel macos

  • 我在excel中有两张纸
    1. Test1
    2. 的Test2
      现在,我在每张纸上有42列和100行。
  • 我必须比较两张纸并找出两张纸中价值不同的所有单元格 示例:如果单元格A1 =是,则在 Test1 工作表中,如果A1 =是,则在 Test2 工作表中,然后不执行任何操作其他在两张纸中突出显示红色的单元格A1。
  • 可以使用条件格式实现吗?

1 个答案:

答案 0 :(得分:0)

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

enter image description here