VBA Vlookup - if part number not found then display Msgbox and Exit Sub

madi1004

New Member
Joined
Dec 11, 2013
Messages
11
Hello,

I have following macro which on Error will resume to next.
Now I want to display a message box if an error is found and after exit SUB
Message box( "Part number" & "PN" & "not found. Please define packaging details")
By PN should be displayed the part number that is not found.
VBA Code:
Sub Vlookup()

Dim Volume As Worksheet
Dim Packaging As Worksheet
Dim PN As Long
Dim Pcs As Long
Dim x As Variant
Dim dataRNG As Range

Set Volume = ThisWorkbook.Worksheets("Volume per shipment")
Set Packaging = ThisWorkbook.Worksheets("Packaging details")

PN = Volume.Range("A" & Rows.Count).End(xlUp).Row
Pcs = Packaging.Range("A" & Rows.Count).End(xlUp).Row

Set dataRNG = Packaging.Range("A2:G" & Pcs)

For x = 2 To PN

On Error Resume Next

Volume.Range("D" & x).Value = Application.WorksheetFunction.Vlookup( _
                            Volume.Range("A" & x).Value, dataRNG, 7, 0)

Next x

End Sub

Thank you!
Madi
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Do you want one message box to show all missing part numbers, once the loop has finished?
 
Upvote 0
Ok, how about
VBA Code:
Sub Vlookup()

Dim Volume As Worksheet
Dim Packaging As Worksheet
Dim PN As Long
Dim Pcs As Long
Dim x As Variant
Dim dataRNG As Range
Dim Msg As String
Dim PartNo As Variant

Set Volume = ThisWorkbook.Worksheets("Volume per shipment")
Set Packaging = ThisWorkbook.Worksheets("Packaging details")

PN = Volume.Range("A" & Rows.Count).End(xlUp).row
Pcs = Packaging.Range("A" & Rows.Count).End(xlUp).row

Set dataRNG = Packaging.Range("A2:G" & Pcs)

For x = 2 To PN


   PartNo = Application.Vlookup(Volume.Range("A" & x).Value, dataRNG, 7, 0)
   If IsError(PartNo) Then
      Msg = Msg & vbLf & Volume.Range("A" & x).Value
   Else
   Volume.Range("D" & x).Value = PartNo
Next x
If Msg <> "" Then MsgBox "Part number" & Msg & vbLf & "not found. Please define packaging details"
End Sub
 
Upvote 0
Oops, forgot the End If
Rich (BB code):
Sub Vlookup()

Dim Volume As Worksheet
Dim Packaging As Worksheet
Dim PN As Long
Dim Pcs As Long
Dim x As Variant
Dim dataRNG As Range
Dim Msg As String
Dim PartNo As Variant

Set Volume = ThisWorkbook.Worksheets("Volume per shipment")
Set Packaging = ThisWorkbook.Worksheets("Packaging details")

PN = Volume.Range("A" & Rows.Count).End(xlUp).row
Pcs = Packaging.Range("A" & Rows.Count).End(xlUp).row

Set dataRNG = Packaging.Range("A2:G" & Pcs)

For x = 2 To PN


   PartNo = Application.Vlookup(Volume.Range("A" & x).Value, dataRNG, 7, 0)
   If IsError(PartNo) Then
      Msg = Msg & vbLf & Volume.Range("A" & x).Value
   Else
      Volume.Range("D" & x).Value = PartNo
   End If
Next x
If Msg <> "" Then MsgBox "Part number" & Msg & vbLf & "not found. Please define packaging details"
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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