Home » excel » excel – Add Iferror Function to VBA

excel – Add Iferror Function to VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

How do you inject the iferror function on this code?

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

I just want a null value cells (d, 16) if it is an error.

How to&Answers:

Add before the row with Next:

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

Answer:

Something like below would do that for you, a nested application function:

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