用VBA对比两个excel表中的内容,并将不同打印出来

来源:百度知道 编辑:UC知道 时间:2024/06/07 13:32:44
将sheet1和sheet2的不同在sheet3中显示出来,sheet3中可以写两个按钮,一个按钮单击完成比较,并将不同处用反色显示出来,一个按钮单击完成显示

用VBA对比两个excel表中的内容,并将不同打印出来的方法:
【1】Sub aa() '只用一个按钮
Dim r, c
For r = 1 To 10
For c = 1 To 5 '区域范围为十行五列
Worksheets("Sheet3").Cells(r, c) = Worksheets("Sheet1").Cells(r, c) '复制sheet1的内容
If Worksheets("Sheet1").Cells(r, c) <> Worksheets("Sheet2").Cells(r, c) Then
Worksheets("Sheet3").Cells(r, c).Interior.ColorIndex = 3
End If
Next
Next
End Sub
【2】
Private Sub CommandButton1_Click()
Dim arr, brr, crr(1 To 1000), b As Boolean
b = False
Dim x%: x = 1
arr = Sheet2.Range("a1:a" & Sheet2.[a65536].End(xlUp).Row)
brr = Sheet3.Range("a1:a" & Sheet3.[a65536].End(xlUp).Row)
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(brr, 1)
If arr(i, 1) = brr(j, 1) Then b = True: Exit For
Next
If b = False Then crr(x) = arr(i, 1): x = x + 1
b = False
Next