将Iferror函数添加到VBA

时间:2018-07-18 09:09:09

标签: excel vba excel-vba

如何在此代码中插入iferror函数?

For d = 2 To lastrowx2

Worksheets("Educational Service Report").Cells(d, 16).Value = Application.WorksheetFunction.VLookup( _
Worksheets("Educational Service Report").Cells(d, 15).Value, _
Worksheets("Educational Service Report").Range("X:Y"), 2, 0)

Next

如果出现错误,我只想要一个空值单元格(d,16)。

2 个答案:

答案 0 :(得分:2)

嵌套的应用程序功能可以为您完成以下工作:

For d = 2 To lastrowx2

Worksheets("Educational Service Report").Cells(d, 16).Value = Application.IfError( _
    Application.VLookup(Worksheets("Educational Service Report").Cells(d, 15).Value, _
    Worksheets("Educational Service Report").Range("X:Y"), 2, 0), "")

Next d

答案 1 :(得分:0)

在带有Next的行之前添加:

If IsError(Worksheets("Educational Service Report").Cells(d, 16)) Then
    Worksheets("Educational Service Report").Cells(d, 16) = ""
End If