比较两个单独的工作表上的两个单独的列

时间:2013-06-04 18:51:35

标签: excel-vba vba excel

我需要比较两张不同纸张上的值,两张纸都在H列中,从2开始。一张纸标记为最终,另一张纸标记为最终。如果它是最终的而不是数据则在最后突出显示。如果在数据中找到的东西没有最终复制到底部的最终(整行)。这是全文。 H列标题为“参考”。

1 个答案:

答案 0 :(得分:0)

代码1

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False
    If Target.Column <> 8 Then Exit Sub

    Dim lastRow As Long
    Dim rng As Range, cell As Range

    lastRow = Range("H" & Rows.Count).End(xlUp).Row
    If lastRow < 2 Then lastRow = 2

    Set rng = Range("H2:H" & lastRow)

    For Each cell In rng

        With Sheets("data")
            a = Application.VLookup(cell.Value, .Range("H2:H" & .Range("H" & Rows.Count).End(xlUp).Row), 1, 0)

            If IsError(a) Then
                cell.Interior.Color = vbYellow
                Else
                cell.Interior.Color = xlNone
            End If
        End With

    Next

    Application.EnableEvents = True
End Sub

代码2

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False
    If Target.Column <> 8 Then Exit Sub

    Dim lastRow As Long
    Dim rng As Range, cell As Range

    lastRow = Range("H" & Rows.Count).End(xlUp).Row
    If lastRow < 2 Then lastRow = 2

    Set rng = Range("H2:H" & lastRow)

    For Each cell In rng

        With Sheets("final")
             a = Application.VLookup(cell.Value, .Range("H2:H" & .Range("H" & Rows.Count).End(xlUp).Row), 1, 0)

            If IsError(a) Then
               cell.Copy .Range("H" & .Range("H" & Rows.Count).End(xlUp).Row)
            End If
        End With

    Next

    Application.EnableEvents = True
End Sub

enter image description here