2013-06-04 46 views
2

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

回答

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

+0

TY家伙,我会尝试明天在工作和送还给你。 – user2453057

+0

我已经输入了所描述的信息,但是当我运行其他宏时没有任何反应。你能否为我评论这个,所以我可以继续解决这个问题。 – user2453057

+0

@ user2453057确保您已将代码放在图表代码部分,如图中所示。还要确保'Application.EnableEvents = True',以便宏可以响应事件。在列H中的工作表中的任何更改宏将触发 – Santosh