这个索引和匹配的VBA代码不起作用是什么?

我坚持这段代码,我已经看了一遍又一遍,但我似乎无法find问题。 有谁知道为什么会发出错误? 以下显示错误的post。

For i = 2 To Total_rows_Pick For j = 2 To Total_rows_Dash If Application.WorksheetFunction.IfError(Application.WorksheetFunction.Index(Worksheets("Finished").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("Pick-ups").Cells(i, 4), Worksheets("Finished").Range("A2:A1048576"), 0)), 1) <> 1 Then If Worksheets("Dashboard").Cells(j, 2) = Worksheets("Pick-ups").Cells(i, 2) And Worksheets("Pick-ups").Cells(i, 4) = Worksheets("Dashboard").Cells(j, 1) Then Worksheets("Dashboard").Cells(j, 3) = Worksheets("Dashboard").Cells(j, 3) + Worksheets("Pick-ups").Cells(i, 3) End If End If Next j Next i 

在这里输入图像说明

编辑(尝试答案后):

在这里输入图像说明

修改后的代码(注意错误,如第3行):

  For i = 2 To Total_rows_Pick For j = 2 To Total_rows_Dash m = Application.Match(Worksheets("Pick-ups").Cells(i, 4), Worksheets("Finished").Range("A2:A1048576"), 0) If Not IsError(m) Then If Application.WorksheetFunction.IfError(Application.WorksheetFunction.Index(Worksheets("Finished").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("Pick-ups").Cells(i, 4), Worksheets("Finished").Range("A2:A1048576"), 0)), 1) <> 1 Then If Worksheets("Dashboard").Cells(j, 2) = Worksheets("Pick-ups").Cells(i, 2) And Worksheets("Pick-ups").Cells(i, 4) = Worksheets("Dashboard").Cells(j, 1) Then Worksheets("Dashboard").Cells(j, 3) = Worksheets("Dashboard").Cells(j, 3) + Worksheets("Pick-ups").Cells(i, 3) End If End If End If Next j Next i 

如果没有匹配,那么Application.WorksheetFunction.Match会抛出一个运行时错误,就像你看到的那样(你不能像使用IfError()那样捕获这个IfError() )。 如果您使用Application.Match那么它将返回一个错误值,您可以在Index中使用该返回值之前使用IsError()进行testing:

 Dim m For i = 2 To Total_rows_Pick For j = 2 To Total_rows_Dash m = Application.Match(Worksheets("Pick-ups").Cells(i, 4), _ Worksheets("Finished").Range("A2:A1048576"), 0) If Not IsError(m) Then 'rest of your code here End If Next j Next i