How to add "Else Msgbox" To this piece of code?

mageeg

Board Regular
Joined
Jun 6, 2021
Messages
81
Hi, i have written a piece of code which checks whether a serial number is between two certain values. If it is then it pulls data from that row.

I would like to add an else messagebox that says ("Serial number not found") However i can't seem to do this without it coming up no matter what serial number i put in.

See code below:
VBA Code:
Private Sub SerialNumber_AfterUpdate()
Dim ID As Long
Dim StartID As Long
Dim EndID As Long
Dim i As Range

If SerialNumber.Value = "" Then

Else

'code for receiver range, *if number entered is between range then lookup rest of data*


    ID = SerialNumber.Value
    
        For Each i In Sheets("ReceiverData").Range("Table2[From]")
        
        StartID = i.Value
        EndID = i.Offset(0, 1).Value
        
            If ID >= StartID And ID < EndID Then

            txtType.Value = i.Offset(0, 3).Value
            txtSize.Value = i.Offset(0, 4).Value
            txtWKPRESS.Value = i.Offset(0, 6).Value
            txtCertDate.Value = i.Offset(0, 5).Value
            
            End If
        
        Next i

End If


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
How about
VBA Code:
Private Sub SerialNumber_AfterUpdate()
Dim ID As Long
Dim StartID As Long
Dim EndID As Long
Dim i As Range
Dim Flg As Boolean

If SerialNumber.Value = "" Then

Else

'code for receiver range, *if number entered is between range then lookup rest of data*


    ID = SerialNumber.Value
    
        For Each i In Sheets("ReceiverData").Range("Table2[From]")
        
        StartID = i.Value
        EndID = i.Offset(0, 1).Value
        
            If ID >= StartID And ID < EndID Then

            txtType.Value = i.Offset(0, 3).Value
            txtSize.Value = i.Offset(0, 4).Value
            txtWKPRESS.Value = i.Offset(0, 6).Value
            txtCertDate.Value = i.Offset(0, 5).Value
            Flg = True
            End If
        
        Next i

End If
If Flg = False Then MsgBox "not found"

End Sub
 
Upvote 0
Solution
How about
VBA Code:
Private Sub SerialNumber_AfterUpdate()
Dim ID As Long
Dim StartID As Long
Dim EndID As Long
Dim i As Range
Dim Flg As Boolean

If SerialNumber.Value = "" Then

Else

'code for receiver range, *if number entered is between range then lookup rest of data*


    ID = SerialNumber.Value
   
        For Each i In Sheets("ReceiverData").Range("Table2[From]")
       
        StartID = i.Value
        EndID = i.Offset(0, 1).Value
       
            If ID >= StartID And ID < EndID Then

            txtType.Value = i.Offset(0, 3).Value
            txtSize.Value = i.Offset(0, 4).Value
            txtWKPRESS.Value = i.Offset(0, 6).Value
            txtCertDate.Value = i.Offset(0, 5).Value
            Flg = True
            End If
       
        Next i

End If
If Flg = False Then MsgBox "not found"

End Sub

That was the exact method i was trying to use but i was trying to put it in the wrong place. instead i was trying to implement it inside the already made IF. whoops!

Works perfectly, thank you!
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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