另外一位老师用了这个代码:
On Error Resume Next
Dim i, j As Integer
Set myDocument1 = ThisWorkbook.Worksheets("开票明细").Range("b6:c1000")
Set myDocument2 = ThisWorkbook.Worksheets("ERP匹配")
Set myDocument3 = ThisWorkbook.Worksheets("收入明细账").Range("F5:H1000")
Set myDocument4 = ThisWorkbook.Worksheets("收入明细账").Range("F5:F1000")
Set myDocument5 = ThisWorkbook.Worksheets("收入明细账").Range("E5:F1000")
For i = 1 To myDocument2.Range("a65536").End(xlUp).Row
If myDocument2.Cells(i, 2) <> "" Then
myDocument2.Cells(i, 7) = Application.WorksheetFunction.VLookup(myDocument2.Cells(i, 2), myDocument1, 2, [0])
Else
myDocument2.Cells(i, 7) = ""
End If
If myDocument2.Cells(i, 3) <> "" Then
myDocument2.Cells(i, 6) = Application.WorksheetFunction.VLookup(myDocument2.Cells(i, 3), myDocument3, 2, [0])
myDocument2.Cells(i, 4) = Application.WorksheetFunction.VLookup(myDocument2.Cells(i, 3), myDocument3, 3, [0])
myDocument2.Cells(i, 5) = Application.WorksheetFunction.Index(myDocument5, Application.WorksheetFunction.Match(myDocument2.Cells(i, 3), myDocument4, 0), 1)
Else
myDocument2.Cells(i, 6) = ""
myDocument2.Cells(i, 4) = ""
myDocument2.Cells(i, 5) = ""
End If
Next
End Sub