1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: trap error in VBA vlookup

Discussion in 'Business Applications' started by spooky1, Nov 22, 2011.

Thread Status:
Not open for further replies.
  1. spooky1

    spooky1 Thread Starter

    Joined:
    Jan 29, 2010
    Messages:
    126
    My Target: find a company name in another sheet using wildcard vlookup and use the found name.

    Below is what I have so far but if nothing is found macro craps out. If nothing found then need to go to add data to data base macro instead of coming up with an error. Any more elegant workable solutions?
    My attempt works until another search is attempted by goto goo.


    Sub a()
    frt$ = "*"
    pray$ = InputBox("enter in name", , "qwerqwerqw") & frt$ 'qwerqwerqw is for test purpose only

    goo:

    On Error Resume Next
    xt1 = Application.VLookup(pray$, Sheets("company data").Range("$C$39:$C$49863"), 1, 0)
    On Error GoTo enditall

    MsgBox xt1 <comes up with error when it tries to display (#na)? result

    Exit Sub

    enditall:
    If MsgBox("try again?", vbYesNo) = vbYes Then GoTo goo

    End Sub
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Just add an IF statement to determine if an error was received.

    Code:
    xt1 = Application.VLookup(pray$, Sheets("company data").Range("$C$39:$C$49863"), 1, 0)
    
    If Not IsError(xt1) Then
    'Your code to execute if match IS found
    Else
    'Your code to execute if match is NOT found
    End If

    Rollin
     
  3. spooky1

    spooky1 Thread Starter

    Joined:
    Jan 29, 2010
    Messages:
    126
    Methinks that all us newbies should band together and build you guys a temple on a hill somewhere!
    Thanks once again!

    If some religious deitites replied to prayers like you guys do to questions about excel, the world would not be in a similar mess to what my program would be without youse!
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1028049

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice