+ Reply to Thread
Results 1 to 4 of 4

Vlookup in VBA

  1. #1
    sharonm
    Guest

    Vlookup in VBA

    I am trying to do a Vlookup. My lookup value is on the MainData sheet and my
    lookup range is on the DeptLookup sheet. If there is a match, the code works
    fine. However, if there is no match, I get a Runtime error '1004' -
    Application defined or object defined error on the line

    y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False

    My code is:
    Set wb = Workbooks("TestWorkbook.xls")
    Set ws = wb.Sheets("DeptLookup")
    Set WS2 = wb.Sheets("MainData")
    Set rng = ws.Range("$A$3:$C$59")
    Dim y As Variant

    y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
    False)
    If Not IsError(y) Then
    ActiveCell.Offset(0, -34).Value =
    Application.WorksheetFunction.VLookup_(ActiveCell.Offset(0, -31).Value, rng,
    1, False)

    Would anyone be able to tell me what I am doing wrong?

    Thanks in advance!

  2. #2
    Tom Ogilvy
    Guest

    Re: Vlookup in VBA

    If you qualify Vlookup with WorksheetFunction, then failure to match will
    results in a 1004 error (this is by design).

    If you qualify Vlookup with Application instead, you can use the IsError
    test.

    Set wb = Workbooks("TestWorkbook.xls")
    Set ws = wb.Sheets("DeptLookup")
    Set WS2 = wb.Sheets("MainData")
    Set rng = ws.Range("$A$3:$C$59")
    Dim y As Variant

    y = Application.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False)
    If Not IsError(y) Then
    ActiveCell.Offset(0, -34).Value = y
    End if


    --
    Regards,
    Tom Ogilvy



    "sharonm" <[email protected]> wrote in message
    news:[email protected]...
    > I am trying to do a Vlookup. My lookup value is on the MainData sheet and

    my
    > lookup range is on the DeptLookup sheet. If there is a match, the code

    works
    > fine. However, if there is no match, I get a Runtime error '1004' -
    > Application defined or object defined error on the line
    >
    > y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,

    False
    >
    > My code is:
    > Set wb = Workbooks("TestWorkbook.xls")
    > Set ws = wb.Sheets("DeptLookup")
    > Set WS2 = wb.Sheets("MainData")
    > Set rng = ws.Range("$A$3:$C$59")
    > Dim y As Variant
    >
    > y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
    > False)
    > If Not IsError(y) Then
    > ActiveCell.Offset(0, -34).Value =
    > Application.WorksheetFunction.VLookup_(ActiveCell.Offset(0, -31).Value,

    rng,
    > 1, False)
    >
    > Would anyone be able to tell me what I am doing wrong?
    >
    > Thanks in advance!




  3. #3
    sharonm
    Guest

    Re: Vlookup in VBA

    THANK YOU VERY MUCH!


    "Tom Ogilvy" wrote:

    > If you qualify Vlookup with WorksheetFunction, then failure to match will
    > results in a 1004 error (this is by design).
    >
    > If you qualify Vlookup with Application instead, you can use the IsError
    > test.
    >
    > Set wb = Workbooks("TestWorkbook.xls")
    > Set ws = wb.Sheets("DeptLookup")
    > Set WS2 = wb.Sheets("MainData")
    > Set rng = ws.Range("$A$3:$C$59")
    > Dim y As Variant
    >
    > y = Application.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False)
    > If Not IsError(y) Then
    > ActiveCell.Offset(0, -34).Value = y
    > End if
    >
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    >
    > "sharonm" <[email protected]> wrote in message
    > news:[email protected]...
    > > I am trying to do a Vlookup. My lookup value is on the MainData sheet and

    > my
    > > lookup range is on the DeptLookup sheet. If there is a match, the code

    > works
    > > fine. However, if there is no match, I get a Runtime error '1004' -
    > > Application defined or object defined error on the line
    > >
    > > y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,

    > False
    > >
    > > My code is:
    > > Set wb = Workbooks("TestWorkbook.xls")
    > > Set ws = wb.Sheets("DeptLookup")
    > > Set WS2 = wb.Sheets("MainData")
    > > Set rng = ws.Range("$A$3:$C$59")
    > > Dim y As Variant
    > >
    > > y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
    > > False)
    > > If Not IsError(y) Then
    > > ActiveCell.Offset(0, -34).Value =
    > > Application.WorksheetFunction.VLookup_(ActiveCell.Offset(0, -31).Value,

    > rng,
    > > 1, False)
    > >
    > > Would anyone be able to tell me what I am doing wrong?
    > >
    > > Thanks in advance!

    >
    >
    >


  4. #4
    Patrick Molloy
    Guest

    Re: Vlookup in VBA

    with function like VLOOKUP and MATCH one can use the on error resume next
    followed by whatever on error handle you had. For large blocks of code I
    simply find it easier to use a function of my own to "wrap" the excel
    function so that this looks after function errors and the error handling in
    the caller becomes trivial
    ---------
    result = SafeVLookup("blah", Range("data"), 7)
    If result = "" Then
    ' handle it
    Else
    ' use it
    End If
    ----

    Function SafeVLookup(what As String, _
    where As Range, _
    colChoice As Long) As String
    On Error Resume Next
    SafeVLookup = WorksheetFunction.VLookup(what, where, colChoice, False)
    On Error GoTo 0
    End Function


    "sharonm" wrote:

    > THANK YOU VERY MUCH!
    >
    >
    > "Tom Ogilvy" wrote:
    >
    > > If you qualify Vlookup with WorksheetFunction, then failure to match will
    > > results in a 1004 error (this is by design).
    > >
    > > If you qualify Vlookup with Application instead, you can use the IsError
    > > test.
    > >
    > > Set wb = Workbooks("TestWorkbook.xls")
    > > Set ws = wb.Sheets("DeptLookup")
    > > Set WS2 = wb.Sheets("MainData")
    > > Set rng = ws.Range("$A$3:$C$59")
    > > Dim y As Variant
    > >
    > > y = Application.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False)
    > > If Not IsError(y) Then
    > > ActiveCell.Offset(0, -34).Value = y
    > > End if
    > >
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > >
    > > "sharonm" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > I am trying to do a Vlookup. My lookup value is on the MainData sheet and

    > > my
    > > > lookup range is on the DeptLookup sheet. If there is a match, the code

    > > works
    > > > fine. However, if there is no match, I get a Runtime error '1004' -
    > > > Application defined or object defined error on the line
    > > >
    > > > y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,

    > > False
    > > >
    > > > My code is:
    > > > Set wb = Workbooks("TestWorkbook.xls")
    > > > Set ws = wb.Sheets("DeptLookup")
    > > > Set WS2 = wb.Sheets("MainData")
    > > > Set rng = ws.Range("$A$3:$C$59")
    > > > Dim y As Variant
    > > >
    > > > y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
    > > > False)
    > > > If Not IsError(y) Then
    > > > ActiveCell.Offset(0, -34).Value =
    > > > Application.WorksheetFunction.VLookup_(ActiveCell.Offset(0, -31).Value,

    > > rng,
    > > > 1, False)
    > > >
    > > > Would anyone be able to tell me what I am doing wrong?
    > > >
    > > > Thanks in advance!

    > >
    > >
    > >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1