2013-06-04 180 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