2012-08-24 170 views
1

我正在嘗試爲我的個人銀行帳戶執行基於Excel的工具。我已經用3列(DateInformationAmount)在一張表Import中導入了一些銀行操作。我想將Import中的每一行與已在另一張表Data中建立的集合進行比較。VBA Excel比較行

我已經創建了一個函數來測試(根據日期然後信息,如果需要的話,如果需要的話,如果有必要的話),每行返回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片的每一行,但是我不能循環的每個日期元件上第一。該循環在CollectionRange的每個元素上運行。我試圖做For Each row In CollectionRange.Rows,但MsgBox後無效。我如何在每一行上做這個循環?

回答

0

對於循環行For Each row In CollectionRange.Rows是正確的。

原因MsgBox row.Value然後不起作用row.Value是一個數組。要查看數組中的某個值,請使用類似於以下內容的值:row.Cells(1,1).Value

也就是說,請注意,在這樣的範圍內循環可能會很慢。如果您對代碼的性能不滿意,還有其他替代方法,如AutoFilterFindVariant Arrays