vlookup好像不配合

Vlookup doesn't seem to cooperate

我有以下银行对账代码,它涉及检查工作表 1(银行对帐单)D 列中的每个单元格,并查看它是否存在于 Sheet 2 的 M 列中。如果没有标记它通过将其保存到 arrOutput.

作为新用户,由于我无法附上电子表格,我有指向 Sheet 1 和 2 的链接。

Sheet1 Sheet2

 Sub abc_3()

 Dim i As Long, ii
 Dim arrBank As Range
 Dim arrAccounting As Range
 Dim arrOutput

 Dim temp As Variant

 ' setting bank transaction into range
 Set bank = ActiveWorkbook.Sheets("Sheet1").Range("D25:E25" & Cells(Rows.Count, "D").End(xlUp).Row)

 ' setting accounting transactions into range
 Set books = ActiveWorkbook.Sheets("Sheet2").Range("M1:N1" & Cells(Rows.Count, "M").End(xlUp).Row)


 'everytime time the program is run arrOutput must be cleared. 3000 is an arbitrary number I chose because there will likely never be a higher number of transactions than this.
 ReDim arrOutput(1 To 3000, 1 To 2)   

 ii = 0

 ' The main function of the program.. looping through every bank transaction checking if it can be found in accounting transactions,
 ' if it cannot be found, i.e error is thrown then save the cell to arrOutput because it needs to be flagged for checking.
 ' if it can be found, then ignore and check next bank transaction.
 ' Currently, the procedure is supposed to compare only Sheet1 credit transactions with Sheet2 credit transactions, therefore filter only credit transactions. 
 For Each cell In bank.Cells     'problem here is comparing both Column D and E of Sheet 1 whereas it should be comparing only column D.
     If cell <> "" Then       'this is to avoid checking non-credit transactions.
         On Error Resume Next
         temp = Application.WorksheetFunction.VLookup(cell, books, 2, False)
         If Err.Number <> 0 Then
             MsgBox "Bank Transaction " & cell & " could not be found in Books Transaction history"
             arrOutput(ii, 1) = cell
             arrOutput(ii, 2) = ""
             ii = ii + 1
         End If
     End If
 Next

'all cells checked then dump arrOutput to range "L4" for reading
 Range("l4").Resize(3000, 2) = arrOutput

 bank.ClearContents
 books.ClearContents

 End Sub

问题是在每个单元格上我都收到 MSG "Bank Transaction " & cell & “在图书交易历史记录中找不到”。因此,每个单元格都保存到 arrOutput 并保存到 Sheets("Sheet3").Range("L4") 让我想知道 Vlookup 是否不合作或者我没有设置错误处理程序正确。

期待得到一些帮助.. 被困在这个问题上太久了。提前谢谢你。

1) 您应该限定范围。 2) :E25 应该是 :D:N1 应该是 :M。 3)使用 Option Explicit 并使用您声明的变量(您声明了一些变量名称但随后使用了其他名称......)。 4) 最后,使用 Find 而不是 VLookup,因为您只想检查值是否存在,而不是相应的其他值。

Option Explicit
Sub abc_3()
     Dim bank As Range, books As Range, cell As Range
     With ActiveWorkbook.Sheets("Sheet1")
        Set bank = .Range("D26:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
     End With
     With ActiveWorkbook.Sheets("Sheet2")
         Set books = .Range("M2:M" & .Cells(.Rows.Count, "M").End(xlUp).Row)
     End With

     Dim ii As Long, x As Range, arrOutput(1 To 3000, 1 To 2)
     For Each cell In bank.Cells
         If Trim(cell.Value) <> "" Then
             Set x = books.Find(cell.Value, , xlValues, xlWhole)
             If x Is Nothing Then
                 ii = ii + 1
                 arrOutput(ii, 1) = cell.Value
                 MsgBox "Bank Transaction " & cell.Value & " could not be found in Books Transaction history"
             Else
                 x.Value = ""
             End If
         End If
     Next
     ActiveWorkbook.Sheets("Sheet3").Range("l4").Resize(3000, 2) = arrOutput
End Sub