How to add an Else Msgbox to code like this?

mageeg

Board Regular
Joined
Jun 6, 2021
Messages
81
Hi I have written a very simple piece of code, which looks up a Batch Number, and then if found it pulls through the Job Number and Date of manufacture

the issue with this is that if the batch number does not exist, it simply pulls though no data. I would like a messagebox to then pop up saying "Batch Number not found"

Any help would be appreciated. See code below:

VBA Code:
Private Sub BatchNo_AfterUpdate()
Dim MyTableArray As Range
Set MyTableArray = Sheets("SafetyValveData").Range("A:F")

On Error GoTo err_trap

'vlookup batch no

Me.JobNo.Value = WorksheetFunction.VLookup(Me.BatchNo.Value, MyTableArray, 4, 0)
Me.DateOfManufacture.Value = WorksheetFunction.VLookup(Me.BatchNo.Value, MyTableArray, 5, 0)

err_trap:
    
    Exit Sub


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.
How about
VBA Code:
Private Sub BatchNo_AfterUpdate()
Dim Res As Variant
Dim MyTableArray As Range
Set MyTableArray = Sheets("SafetyValveData").Range("A:F")


'vlookup batch no
Res = Application.VLookup(Me.BatchNo.Value, MyTableArray, 4, 0)
If IsError(Res) Then
   MsgBox "not found"
Else
   Me.JobNo.Value = Res
   Me.DateOfManufacture.Value = Application.VLookup(Me.BatchNo.Value, MyTableArray, 5, 0)
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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