0

I'm just trying to do something very simple with Vlookup, but am getting the 1004 error. Would really, really appreciate your help. Thanks in advance. Here's my code:

Sub test()
    Dim user As String
    Dim drawn As String
    Set Sheet = ActiveWorkbook.Sheets("consolidated")

    For i = 2 To 2092
        user = CStr(Cells(i, 1).Value)
        Set Sheet = ActiveWorkbook.Sheets("sections")
        drawn = CStr(Application.WorksheetFunction.VLookup(user, Sheet.Range("A2:B3865"), 2))
        Set Sheet = ActiveWorkbook.Sheets("consolidated")
        Cells(i, 10).Value = drawn
    Next i
End Sub
9
  • Do you get an error message? On what line does the error occur? Commented Aug 6, 2013 at 14:39
  • yes - it occurs on the line i use vlookup. sometimes i get the object-defined error 1004, and sometimes i get the error that says "Unable to get the Vlookup property of the WorksheetFunction class" Commented Aug 6, 2013 at 14:45
  • could it just be because it doesn't find the first user of "consolidated" listed in "sections"? I assumed that vlookup doesn't give an error if something is not found. Commented Aug 6, 2013 at 14:52
  • stackoverflow.com/questions/15503199/… Commented Aug 6, 2013 at 14:53
  • 1
    I've also see Application.VLookup, omitting the WorksheetFunction part. I'm not sure, but something to try. Commented Aug 6, 2013 at 14:58

1 Answer 1

3

When you use VLOOKUP as a member of WorksheetFunction, an error will result in a runtime error. When you use VLOOKUP as a member of Application, an error will result in a return value that's an error, which may or may not result in a runtime error. I have no idea why MS set it up this way.

If you use WorksheetFunction, you should trap the error. If you use Application, you should use a Variant variable and test for IsError. Here are a couple of examples.

Sub VlookupWF()

    Dim sUser As String
    Dim sDrawn As String
    Dim shSec As Worksheet
    Dim shCon As Worksheet
    Dim i As Long

    Set shSec = ActiveWorkbook.Worksheets("sections")
    Set shCon = ActiveWorkbook.Worksheets("consolidated")

    For i = 2 To 2092
        sUser = shCon.Cells(i, 1).Value
        'initialize sDrawn
        sDrawn = vbNullString

        'trap the error when using worksheetfunction
        On Error Resume Next
            sDrawn = Application.WorksheetFunction.VLookup(sUser, shSec.Range("A2:B3865"), 2, False)
        On Error GoTo 0

        'see if sdrawn is still the initialized value
        If Len(sDrawn) = 0 Then
            sDrawn = "Not Found"
        End If

        shCon.Cells(i, 10).Value = sDrawn
    Next i

End Sub

Sub VlookupApp()

    Dim sUser As String
    Dim vDrawn As Variant 'this can be a String or an Error
    Dim shSec As Worksheet
    Dim shCon As Worksheet
    Dim i As Long

    Set shSec = ActiveWorkbook.Worksheets("sections")
    Set shCon = ActiveWorkbook.Worksheets("consolidated")

    For i = 2 To 2092
        sUser = shCon.Cells(i, 1).Value
        vDrawn = Application.VLookup(sUser, shSec.Range("A2:B3865"), 2, False)

        'see if vDrawn is an error
        If IsError(vDrawn) Then
            vDrawn = "Not Found"
        End If

        shCon.Cells(i, 10).Value = vDrawn
    Next i

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.