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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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