简体   繁体   中英

VLOOKUP generates Run-Time Error '1004'

I want to setup a template that finds data based on pasted data in another worksheet.

Private Sub GoNoGo()

Dim i As Integer
Dim OffInt As Integer
Dim Neg As Integer
Neg = -30

Dim Ret As String
Dim I3 As Cell
Dim FindValue As String

Worksheets("BF59520").Activate
Range("AE3").Activate
i = 3
OffInt = 0

Do Until ActiveCell.Offset(0, Neg).Value = ""

    If ActiveCell.Offset(0, -1).Interior.Color = RGB(255, 235, 160) Then
        ActiveCell.Offset(1, 0).Activate
        i = i + 1
    Else
       ActiveCell.Value = Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -18), Worksheets("Go No Go").Range("B2:O180"), 4, False)

        ActiveCell.Offset(1, 0).Activate
        i = i + 1
    End If
        OffInt = OffInt + 1
Loop

End Sub

When the loop gets to the VLOOKUP Line the code returns an error of Run-Time error '1004':

Unable to get the VLOOKUP property of the worksheetFunction class.

Generally, when you get that error on a Worksheet Function it means the function itself has returned an error. Make sure you're passing it the right values. If you can't guarantee that you'll get a correct value from the function then you can try using On Error like so

On Error Resume Next
Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -18), Worksheets("Go No Go").Range("B2:O180"), 4, False)
On Error GoTo 0

or you can capture the error in an evaluate statement like so

ActiveCell.Value = Evaluate("=IFERROR(VLOOKUP(" & ActiveCell.Offset(0,-18) & ", 'Go No Go'!B2:O180, 4, FALSE),0)")

The first will result in a no change in the ActiveCell when the vlookup fails, the second allows you to set a default value as the second argument of the 'IFERROR' function.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM