使用ExcelmacrosVBA在excel范围内find一行的最快方法

我有一个excel电子表格(sheet2),logging数在100万左右。 我正在迭代这些logging,对于每个迭代,我将比较一列所选列与另一个大约2000个logging(位于sheet1中)的范围。

rangeA = 1 Million rows 'Sheet2 rangeB = 2000 rows 'Sheet1 With sheet1 For Each row In rangeA.Columns.Rows For Each rangeBRow In rangeB.Columns.Rows If (.Cells(rangeBRow.Row,1).Value = CName And .Cells(rangeBRow.Row,2).Value = LBL ... ) Then ' Do something cool... set some cell value in sheet2 Exit For End If Next rangeBRow Next row End With 

我上面的代码的问题是,它是永远完成执行。 有没有其他的最快捷的方法来find一行对Excel的macros,而不是迭代2000行2000万logging行?

感谢您的时间。

12秒检查5k行对200k:

 Sub Compare() Dim rngA As Range, rngB As Range Dim dict As Object, rw As Range Dim a As Application, tmp As String Set a = Application Set dict = CreateObject("scripting.dictionary") Set rngA = Sheet1.Range("A2:F200000") Set rngB = Sheet1.Range("K2:P5000") For Each rw In rngA.Rows 'Create a key from the entire row and map to row ' If your rows are not unique you'll have to do a bit more work here ' and use an array for the key value(s) dict.Add Join(a.Transpose(a.Transpose(rw.Value)), Chr(0)), rw.Row Next rw For Each rw In rngB.Rows 'does this row match one in range A? tmp = Join(a.Transpose(a.Transpose(rw.Value)), Chr(0)) If dict.exists(tmp) Then rw.Cells(1).Offset(0, -1).Value = dict(tmp) End If Next rw End Sub