VBA,两个sheet根据第一列id相同比较其余列,不同的值放到sheet3中

Application.ScreenUpdating = False
  Dim rg As Range
  Dim rg2 As Range
  Dim rg3 As Range

  Dim counter As Double
  Dim i As Long
  Dim j As Long
  Dim k As Long
  Dim r1, r2, c1, c2 As Integer
 
   r1 = Sheets(1).Range("A65536").End(xlUp).Row
   r2 = Sheets(2).Range("A65536").End(xlUp).Row
   c1 = Sheets(1).[IV1].End(xlToLeft).Column
   c2 = Sheets(2).[IV1].End(xlToLeft).Column
   j = 1
   i = 1
   k = 1
   Sheets(3).Cells.ClearContents
  Sheets(1).Range("A1:L1").Copy Sheets(3).Range("A1")
  
  For Each rg In Sheets(1).Range("A2" & ":A" & r1)
    i = i + 1 'sheet1行号
    j = 1
    For Each rg2 In Sheets(2).Range("A2" & ":A" & r2)
    j = j + 1 'sheet2行号
       If rg.Value = rg2.Value Then '如果id一样
         For k = 2 To c1 '从第二列开始循环
         'For Each rg3 In Sheets(1).Range("A" & i & ":A" & c1) '遍历这个列来确定哪个值
            'k = k + 1
            rg5 = Sheets(1).Cells(i, k) 'sheet1的i行和sheet2的j行逐一遍历比较
            rg4 = Sheets(2).Cells(j, k)
            If (rg5 <> rg4) Then '如果值不相同 Interior.ColorIndex = 3
             Sheets(1).Cells(i, 1).Copy Sheets(3).Cells(i, 1)
            
             Sheets(1).Cells(i, k).Copy Sheets(3).Cells(i, k)
             Sheets(1).Cells(i, k).Interior.ColorIndex = 3
            End If
        ' Next
        Next
       End If
      
    Next
  Next
  Application.ScreenUpdating = True

posted @ 2012-03-27 11:42  lixq0203  阅读(1672)  评论(0编辑  收藏  举报