Vlookup error on User Form

TWaldron78

New Member
Joined
Aug 13, 2015
Messages
2
Hi folks,

Hoping someone can help me , I have built a user form that searchs a very large excel file and returns selective date , the starting point for this is someone entering an email address in a text box on the form , the macros then find that persons supervisor and location , uses that supervisors name to find his supervisor and so on giving the whole reporting structure in one form.
I have all the code working fine , it does exactly what I need it to do except,when someone enters an email address that isnt in the original file , either through miss typing or it just isnt in the file.
That part of the code that I need help with is below.

How can I get the Macro to not error if the first lookup returns #N/A and give the message box , everything on this works fine and is a huge help to people as long as the email address is correct.

Any and all suggestions greatly accepted.

Thanks


Private Sub CommandButton1_Click()




If TextBox1.Text <> "" Then


TextBox2.Value = Application.WorksheetFunction.VLookup(TextBox1, Worksheets("Test Sheet").Range("G:I"), 3, False)




Else: MsgBox "Please Enter a valid email address" ' This only works if TextBox1 is left completely blank
Exit Sub




End If


TextBox6.Value = Application.WorksheetFunction.VLookup(TextBox1, Worksheets("Test Sheet").Range("G:J"), 4, False)


and so on and so on , every thing from here works
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Perhaps.
Code:
Dim Res As Variant
' other code
Res = Application.VLookup(TextBox1, Worksheets("Test Sheet").Range("G:I"), 3, False)

If IsError(Res) Then 
    MsgBox "Email not found."
    Exit Sub
Else
    TextBox2.Value = Res
End If
By the way, if you are always looking in column G you should consider using Application.Match to get the row the email address is in.

You would only need to do that once.
 
Upvote 0

Forum statistics

Threads
1,221,645
Messages
6,161,044
Members
451,682
Latest member
ogoreo

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