Consulting

Results 1 to 13 of 13

Thread: Solved: vlookup error 2042

  1. #1
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location

    Solved: vlookup error 2042

    i use a vlookup to find a match on a different sheet with this code:

    [VBA]result = Application.VLookup(str, Sheets(3).Range("A1:B" & Cells(Rows.Count, 1).End(xlUp).Row), 2, False)
    MsgBox result
    [/VBA]
    and that works just fine while it finds something, but in 99,99% of the times it will not find anything.
    that makes the result become "error 2042" and causing it to crash.

    how can i avoid the msgbox-code when result is 2042?

    i have tried

    [VBA]if result = "error 2042"[/VBA]
    [VBA]on error goto 1:
    msgbox result
    1:[/VBA]
    [VBA]if is not err.number = 2042[/VBA]

    its probably simple, what did i do wrong?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    On Error Resume Next
    Result = Application.VLookup(MyStr, Sheets(3).Range("A1:B" & Cells(Rows.Count, 1).End(xlUp).Row), 2, False)
    If Result = "Error 2042" Then Result = "Nothing Found"
    MsgBox Result
    On Error GoTo 0
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    Try[VBA]On Error Resume Next[/VBA]

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    There are times when it is acceptable to use On error resume next.....this seems to fit that need.

    You have to be careful using it though as in the wrong usage it will ignore errors that can cause incorrect results....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    oh sorry i forgott to say i dont like on error resume next since this might make the code skip by a serious thing.

    [VBA]if result= "Error 2042"[/VBA]
    gives me an errorcode 13 incompatible types.
    i think this is because the Error 2042 is not a string, its something else.
    it looks like a string when i hower the mousepointer above it, but its not.

  6. #6
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    Quote Originally Posted by lucas
    There are times when it is acceptable to use On error resume next.....this seems to fit that need.

    You have to be careful using it though as in the wrong usage it will ignore errors that can cause incorrect results....
    yeah.. i was typing my other reply while you wrote this.

    the code i have is much more complex than this part i showed here, so a resume next is very risky in my opinion.

    edit:

    is there a way to say:

    use on error resume next from here...
    ....

    ....
    ...
    .....
    ...

    to here.

    ?

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Post 2 restores normal error operation
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    you mean on error goto 0?

    sweet! thanks that could work

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    BTW, avoid using Str as a variable. It is a VBA function. I used MyStr in my example.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I was wondering if you had read past Malcolms suggestion......
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    lucas, yes i did read his post but since it "wont work".

    if result = "Error 2042"
    that crashes since its incompatible types.

    and even if it would work it would change result to "Nothing found"
    then do msgbox result.
    that mean i would get a msgbox "all the time" with nothing found.
    not really what i was hoping on.

    but i had no clue about on error goto 0 means restore "default procedure".

    mdmacillop: thanks i had no idea that was a function. i will change that!
    i just made this code as a "test" since i wanted to make the code run faster.
    i used to have a loop doing the same thing, but as the list gets bigger the code will run slower so i figured i would take care of the issue before it becomes a problem.

    thanks alot guys!!

  12. #12
    Quote Originally Posted by Ago View Post
    i use a vlookup to find a match on a different sheet with this code:

    [VBA]result = Application.VLookup(str, Sheets(3).Range("A1:B" & Cells(Rows.Count, 1).End(xlUp).Row), 2, False)
    MsgBox result
    [/VBA]
    and that works just fine while it finds something, but in 99,99% of the times it will not find anything.
    that makes the result become "error 2042" and causing it to crash.

    how can i avoid the msgbox-code when result is 2042?

    i have tried

    [VBA]if result = "error 2042"[/VBA]
    [VBA]on error goto 1:
    msgbox result
    1:[/VBA]
    [VBA]if is not err.number = 2042[/VBA]

    its probably simple, what did i do wrong?

    Albeit an old thread, I recently had to do some vba excel work and came across this same issue. I'm not a fan of using "on error resume next" so i followed this solution:

    dim vresult as variant
    vresult = Application.VLookup(str, Sheets(3).Range("A1:B" & Cells(Rows.Count, 1).End(xlUp).Row), 2, False)
    If IsError(vresult ) Then
    ' do what you need to do
    else
    ' do something else
    End If
    Hope this helps someone in future...

  13. #13
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    6,844
    Location
    Kobudotoit,

    Welcome to VBA Express, and thanks for the alternate solution. I'm sure someone will find it handy
    Please take the time to read the Forum FAQ

Posting Permissions

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