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

[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD]name[/TD]
[TD]ph#[/TD]
[/TR]
[TR]
[TD]mary1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]mary2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]john3[/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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