VBA to Exit Sub if VlookUp not returned.... I think!?!

Richard_mcr

New Member
Joined
Oct 19, 2023
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hi All,

I have a VlookUp to return project data based on User form txtInput_Change, in the way of a project number. It works just great with projects in the list, however I need to allow the user to use a project number not in the range at times. In this scenario, I return an error.

I've tried adapting serval codes found on the forum, but to no success. I feel like I need an IfError Exit Sub of some sort...!??

Any pointers would be much appreciated!

Working code when project number is valid:


Private Sub txtPrNu_Change()

If Len(txtPrNu.value) = 5 Then

txtPrNa = Application.WorksheetFunction.Vlookup(txtPrNu.value, Worksheets("ProjectData").Range("A:C"), 3, False)
txtClnt = Application.WorksheetFunction.Vlookup(txtPrNu.value, Worksheets("ProjectData").Range("A:C"), 2, False)

End If

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
VBA Code:
If Len(txtPrNu.Value) = 5 Then
    On Error Resume Next
    txtPrNa = "" 'if value not found leave it blank
    txtPrNa = Application.WorksheetFunction.VLookup(txtPrNu.Value, Worksheets("ProjectData").Range("A:C"), 3, False)
    
    txtClnt = "" 'if value not found leave it blank
    txtClnt = Application.WorksheetFunction.VLookup(txtPrNu.Value, Worksheets("ProjectData").Range("A:C"), 2, False)
End If
 
Upvote 1
Solution

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

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