2

我需要比较两个单独工作表上的值,两者都在从 2 开始的 H 列中。一张表标记为最终,另一张数据。如果它在 final 而不是 data 中,则在 final 中突出显示。如果在数据中找到的某些内容不在最终版本中,则将其复制到底部的最终(整行)中。都是文字。H 列标题为“参考”。

4

1 回答 1

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

在此处输入图像描述

于 2013-06-04T19:18:50.530 回答