VBA Excel比较行

我正在尝试为我的个人银行帐户执行基于Excel的工具。 我已经导入一些银行业务在一张表Import 3列( DateInformationAmount )。 我想将Import的每一行与已经在其他工作表Databuild立的集合进行比较。

我已经创build了一个函数来testing(根据date和必要的信息,如果有必要的话,如果需要的话还有数量),如果操作不存在于数据集合中,则返回0,数据集合中的行号返回0。

 Function CompareRows(SingleRng As Range, CollectionRange As Range) As Integer 'SingleRange : Date / Info / amount in on line 'CollectionRange : Date / info / amount / ....(others) on many rows 'Return 0 if SingleRng is not in CollectionRange, row number of data 'collection if present. Dim row As Range For Each row In CollectionRange MsgBox row.Value ' If SingleRng(1, 1) = Rng_1(1, 1).Value Then ' CompareRows = irw ' Else ' irw = irw + 1 ' End If Next End Function 

这个函数将会在Import表中的每一行循环,但是我不能先循环每个date元素。 这个循环在CollectionRange每个元素上运行。 我试图做For Each row In CollectionRange.Rows ,但后面的MsgBox不起作用。 我怎样才能使这个循环在每一行?

循环使用一系列的行For Each row In CollectionRange.Rows是正确的。

MsgBox row.Value然后不起作用的原因是row.Value是一个数组。 要查看数组中的某个值,请使用row.Cells(1,1).Value

这就是说,要知道,循环这样的范围可能会很慢。 如果您对代码的性能不满意,可以使用AutoFilterFindVariant Arrays等替代方法