首页 > 解决方案 > 将 Iferror 函数添加到 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)。

标签: vbaexcel

解决方案


像下面这样的东西会为你做这件事,一个嵌套的应用程序函数:

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

推荐阅读