用 Excel VBA 代码查找出两列中不相同的数据,并填充到指定单元格区域

Sub FindDifference ()

Dim rng1 As Range ,rng2 As Range Dim cell1 As Range ,cell2 As Range Dim arr1 As Variant ,arr2 As Variant Dim i As Long , j As Long

Dim diff As Range

Set rng1= Range ("A1:A10") Set rng2= Range ("B1:B10")arr1=rng1

arr2=rng2

For i =1 To UBound (arr1) For j =1 To UBound (arr2) If arr1( i ,1)=arr2( j ,1) Then Exit For

End If

Next j

Ifj > UBound (arr2) Then If diff Is Nothing Then Set diff =rng1.Cells( i ,1) Else

Set diff = Union ( diff ,rng1.Cells( i ,

End If

End If

Next i

If Not diff Is Nothing Then diff . Copy Range ("℃1") End If

End Sub