使用VBA突出显示两个Excel工作表之间存在差异的列

时间:2018-10-31 15:25:11

标签: excel vba excel-vba

到目前为止,我已经编写了一个VBA脚本来比较两张纸并突出显示具有差异的特定单元格。我试图突出显示这些差异所在的单元格的标题或列。这是我到目前为止的内容:

Sub checked()
    Dim mycol As Range
    Dim mycell As Range
    Dim shtSheet1 As Worksheet
    Dim shtSheet2 As Worksheet
    Set shtSheet1 = Worksheets("Sheet1")
    Set shtSheet2 = Worksheets("Sheet2")
    For Each mycell In shtSheet2.UsedRange
      If Not mycell.Value = shtSheet1.Cells(mycell.Row, mycell.Column).Value Then
        mycell.Interior.Color = vbRed
        'mycell.EntireColumn.Interior.Color = vbYellow
      End If

    Next
End Sub

非常感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

Sub checked()
    Dim mycell As Range
    Dim shtSheet1 As Worksheet
    Set shtSheet1 = Worksheets("Sheet1")
    With Worksheets("Sheet2")
        For Each mycell In .UsedRange
            If Not mycell.Value = shtSheet1.Range(mycell.Address).Value Then .Cells(1, mycell.Column).Interior.Color = vbYellow
        Next
    End with
End Sub

答案 1 :(得分:0)

如果标题在第1行中,则可以调出mycell的列值并指定第1行: cells(1,mycell.Column).Interior.Color = vbRed