Error Handling Help

JHogan0721

New Member
Joined
Dec 7, 2016
Messages
12
I have what is probably a simple error handling issue for most of you but I can't seem to figure it out.

I have an excel userform with multiple text boxes that auto populate via a vlookup. This all works fine except for one text box I can't seem to get the error handling right for.

I need a simple error handling code to leave a textbox blank if a corresponding name cannot be found. I have included the code I am using currently to populate the box.

The box I am having issues with is labeled (rather boring) TextBox3
I am searching range "RT" to find a phone number that corresponds to the name that populates in a text box labeled "box4"
I would like "TextBox3" to remain empty if no corresponding name/number can be found in range "RT"
Any help would be greatly appreciated!

VBA Code:
Private Sub gobutton_click()

If WorksheetFunction.CountIf(Sheet3.Range("A:A"), Me.Box1.Value) = 0 Then
MsgBox "No Record"
Me.box2.Value = ""
Me.box3.Value = ""
Me.box4.Value = ""
Me.box5.Value = ""
Me.box6.Value = ""
Me.box7.Value = ""
Me.box8.Value = ""
Me.box9.Value = ""
Me.box10.Value = ""
Me.box11.Value = ""
Me.box12.Value = ""


Exit Sub

End If

With Me

.box2 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 2, 0)
.box3 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 3, 0)
.box4 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 4, 0)
.box5 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 5, 0)
.box6 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 6, 0)
.box7 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 7, 0)
.box8 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 8, 0)
.box9 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 9, 0)
.box10 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 10, 0)
.box11 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 11, 0)
.box12 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet3.Range("master1"), 12, 0)
.TextBox1 = Application.WorksheetFunction.VLookup(CLng(Me.Box1), Sheet1.Range("Phonelist"), 7, 0)
.TextBox2 = Application.WorksheetFunction.VLookup((Me.box5), Sheet3.Range("DT"), 2, 0)
.TextBox4 = Application.WorksheetFunction.VLookup((Me.box5), Sheet3.Range("DT"), 3, 0)
.TextBox3 = Application.WorksheetFunction.VLookup((Me.box4), Sheet3.Range("RT"), 2, 0)


 







End With
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You can error trap vlookup without the "worksheetfunction"

VBA Code:
Private Sub CommandButton1_Click()
    Dim x
    x = Application.VLookup((Me.box4), Sheet3.Range("RT"), 2, 0)
    If IsError(x) Then
        Me.TextBox3 = ""
    Else
        Me.TextBox3 = x
    End If

End Sub
 
Upvote 0
You can error trap vlookup without the "worksheetfunction"

VBA Code:
Private Sub CommandButton1_Click()
    Dim x
    x = Application.VLookup((Me.box4), Sheet3.Range("RT"), 2, 0)
    If IsError(x) Then
        Me.TextBox3 = ""
    Else
        Me.TextBox3 = x
    End If

End Sub
This is perfect - thanks!!
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,535
Members
449,316
Latest member
sravya

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