vlookup, handling wrong lookup value - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to improve the code below so if a user entered a wrong name, then vba will give error message. I tried with IfError() as below but got error message. Thank you so much.

Code:
Sub mylookup()
    Dim xname As String
    Dim xnumber As Integer
    xname = InputBox("name")
    xnumber = Application.WorksheetFunction.VLookup(xname, Range("a1:b100"), 2, False)
    'xnumber = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(xname, Range("a1:b100"), 2, False), MsgBox("not good name"))
    MsgBox xnumber
End Sub

nameph#
mary11
mary22
john33

<tbody>
</tbody>
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try
Code:
Sub mylookup()
    Dim xname As String
    Dim xnumber As Variant
    xname = InputBox("name")
    xnumber = Application.vlookup(xname, Range("a1:b100"), 2, False)
    If IsError(xnumber) Then
      MsgBox "Oops"
    Else
      MsgBox xnumber
End Sub
 
Upvote 0
I would use Find in VBA rather than vlookup, I believe speed of execution is better. You can then use a "on error goto"

Code:
Sub mylookup()
    Dim Rng As Range: Set Rng = Range("A:A")
    Dim xname As String: xname = InputBox("Name")
    On Error GoTo Wrongentry
    MsgBox Rng.Find(xname).Offset(0, 1).Value
    Exit Sub
Wrongentry:
   MsgBox ("Not good name")
End Sub
 
Last edited:
Upvote 0
Thank you all for the help. But I am just curious, why I have to change

application.worksheetfunction.vlookup()

to

application.vlookup()

I does not work with worksheetfunction.vlookup()! what is the difference between these both function. Thank you once again.
 
Upvote 0
If you use WorksheetFunction then the code will fail if no match is found.
Without it an error number will be returned to the variable, that you can then check against.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top