lezawang
Well-known Member
- Joined
- Mar 27, 2016
- Messages
- 1,805
- Office Version
- 2016
- Platform
- 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.
[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]
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]