最快的方法来检查两个范围是否相等在excel vba中

想象一下,你有两组数据,行和列的数量是相同的。 现在您要检查一个单元格中的单元格中的数据是否与另一个单元格中具有相同相对地址的单元格中的数据相同。 如果对于一行的所有单元格都是如此,则从两个集合中删除该行。 我可以很容易地通过比较每个单元格来编写代码,而这对于大型数据集来说并不好。 请参阅下面的代码两列,其中两组数据恰好在同一张表中并排排列,其中列之间的偏移量为300。

Dim RngOb As Range Dim c As Range Range("A1", "B1").Select set RngOb = Range(Selection, Selection.End(xlDown)) For Each c In RngOb.Rows If c.Cells(1,1).Value = c.Offset(0, 300).Cells(1,1).Value Then If c.Cells(1,2).Value = c.Offset(0, 300).Cells(1,2).Value Then c.EntireRow.Delete End If End If Next 

我的实际数据每天有超过100列和不同的列数。 我正在寻找一个聪明,快速的方式来做到这一点大数据集。 我高度appriciate答案,反馈和批评。 :d

这里是一个简单的方法来比较两个同构范围中的行………….在这个例子中,每个行的第5行:

 Sub RowCompare() Dim ary1() As Variant Dim Range1 As Range, Range2 As Range, rr1 As Range, rr2 As Range Set Range1 = Range("B9:F20") Set Range2 = Range("I16:M27") Set rr1 = Range1.Rows(5) Set rr2 = Range2.Rows(5) ary1 = Application.Transpose(Application.Transpose(rr1)) ary2 = Application.Transpose(Application.Transpose(rr2)) st1 = Join(ary1, ",") st2 = Join(ary2, ",") If st1 = st2 Then MsgBox "the same" Else MsgBox "different" End If End Sub 

如果在单元格中embedded逗号,则在JOIN中select另一个字符

如果我正确理解你的问题,下面的代码应该允许你做你想要的。 在代码中,你select你想要处理的范围; 每个数据集的第一列以及每个数据集内的列数。

它只是假定你只写了两个数据集,尽pipe这个数据集可以被扩展。 如果没有其他数据,有自动确定数据集列的方法。

 Option Explicit Option Base 0 Sub RemoveDups() Dim I As Long, J As Long Dim rRng As Range Dim vRng As Variant, vRes() As Variant Dim bRng() As Boolean Dim aColumns, lColumns As Long Dim colRowsDelete As Collection 'vRng to include from first to last column to be tested Set rRng = Range("f1", Cells(Rows.Count, "F").End(xlUp)).Resize(columnsize:=100) vRng = rRng ReDim bRng(1 To UBound(vRng)) 'columns to be tested 'Specify First column of each data set aColumns = Array(1, 13) 'num columns in each data set lColumns = 3 For I = 1 To UBound(vRng) bRng(I) = vRng(I, aColumns(0)) = vRng(I, aColumns(1)) For J = 1 To lColumns - 1 bRng(I) = bRng(I) And (vRng(I, aColumns(0) + J) = vRng(I, aColumns(1) + J)) Next J Next I 'Rows to Delete Set colRowsDelete = New Collection For I = 1 To UBound(bRng) If bRng(I) = True Then colRowsDelete.Add Item:=I Next I 'Delete the rows If colRowsDelete.Count > 0 Then Application.ScreenUpdating = False For I = colRowsDelete.Count To 1 Step -1 rRng.Rows(colRowsDelete.Item(I)).EntireRow.Delete Next I End If Application.ScreenUpdating = True End Sub