VBA vlookup not found msgbox

PLwolves87

New Member
Joined
Jan 6, 2023
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi, i have a vlookup and would like a msgbox to appear if not found (rather than the textbox being left blank) below is my code if you could please help

Private Sub TextBox27_Change()
'VLookup procedure
Dim costperpallet As String
Dim myrange As Worksheet

costperpallet = Me.TextBox27.Value
Set myrange = ThisWorkbook.Worksheets("JONEN full")

On Error Resume Next
TextBox28.Value = Application.VLookup(TextBox27.Value, Sheets("JONEN full").Range("a:g"), 6, False)
On Error GoTo 0


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Since you are using the Application.VLookup form you should be able to use IsError, so why not something like this:

VBA Code:
    If IsError(Application.VLookup(TextBox27.Value, Sheets("JONEN full").Range("a:g"), 6, False)) Then
        TextBox28.Value = "Not found"
     else
        TextBox28.Value = Application.VLookup(TextBox27.Value, Sheets("JONEN full").Range("a:g"), 6, False)
    End If

(not tested)

(update: I had my syntax backwards!)
 
Upvote 0
Hi,
try this update to your code & see if does what you want

VBA Code:
Private Sub TextBox27_AfterUpdate()
    'VLookup procedure
    Dim costperpallet As String
    Dim MyLookUp    As Variant
    Dim wsJonenFull As Worksheet
    
    costperpallet = Me.TextBox27.Value
    If Len(costperpallet) = 0 Then Exit Sub
    
    Set wsJonenFull = ThisWorkbook.Worksheets("JONEN full")
    
    MyLookUp = Application.VLookup(costperpallet, wsJonenFull.Range("a:g"), 6, False)
    
    TextBox28.Value = IIf(Not IsError(MyLookUp), MyLookUp, "")

    If IsError(MyLookUp) Then MsgBox costperpallet & Chr(10) & "LookUp Value Not Found", 48, "Not Found"

End Sub

Dave
 
Upvote 0
Hi,
try this update to your code & see if does what you want

VBA Code:
Private Sub TextBox27_AfterUpdate()
    'VLookup procedure
    Dim costperpallet As String
    Dim MyLookUp    As Variant
    Dim wsJonenFull As Worksheet
   
    costperpallet = Me.TextBox27.Value
    If Len(costperpallet) = 0 Then Exit Sub
   
    Set wsJonenFull = ThisWorkbook.Worksheets("JONEN full")
   
    MyLookUp = Application.VLookup(costperpallet, wsJonenFull.Range("a:g"), 6, False)
   
    TextBox28.Value = IIf(Not IsError(MyLookUp), MyLookUp, "")

    If IsError(MyLookUp) Then MsgBox costperpallet & Chr(10) & "LookUp Value Not Found", 48, "Not Found"

End Sub

Dave
Hi Dave,

no message box popped up when postcode wasnt found
 
Upvote 0
Hi,
try this update to your code & see if does what you want

VBA Code:
Private Sub TextBox27_AfterUpdate()
    'VLookup procedure
    Dim costperpallet As String
    Dim MyLookUp    As Variant
    Dim wsJonenFull As Worksheet
   
    costperpallet = Me.TextBox27.Value
    If Len(costperpallet) = 0 Then Exit Sub
   
    Set wsJonenFull = ThisWorkbook.Worksheets("JONEN full")
   
    MyLookUp = Application.VLookup(costperpallet, wsJonenFull.Range("a:g"), 6, False)
   
    TextBox28.Value = IIf(Not IsError(MyLookUp), MyLookUp, "")

    If IsError(MyLookUp) Then MsgBox costperpallet & Chr(10) & "LookUp Value Not Found", 48, "Not Found"

End Sub

Dave
Hi Dave,

i got this to work, instead of AFTERUPDATE() i selected CHANGE() and then it works.

thank you
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

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