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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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