VBA Search - Blank/no entry yields results

kapps813

New Member
Joined
Feb 5, 2022
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Have a vba userform that searches a list of serial numbers and populates details associated with that serial number. If the search textbox is left empty, a message box pops up advising to add a serial number, but then populates a result for any blank/empty cells in the serial number range (which I do not want it to do).

Essentially I want the form to yield no results at all if no serial is added in the search box.
How do I stop it from seeing blank cells as a viable result?

VBA Code:
Private Sub cmdsearch_Click()
Dim SERIAL As String
If txttxserial.Text = "" Then
            Msg = "You must enter a serial number!"
            MsgBox Msg
            Me.txttxserial.SetFocus
          End If
SERIAL = Trim(txttxserial.Text)
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
          
For i = 2 To lastrow
If Worksheets("Sheet1").Cells(i, 4).Value = SERIAL Then
txtTXmakemodel.Text = Worksheets("Sheet1").Cells(i, 2).Value & Space(1) & Worksheets("Sheet1").Cells(i, 3).Value
txtTXdate.Text = Worksheets("Sheet1").Cells(i, 10).Value
txttxwho.Text = Worksheets("Sheet1").Cells(i, 11).Value
txtadlic.Text = Worksheets("Sheet1").Cells(i, 12).Value
txtnotes.Text = Worksheets("Sheet1").Cells(i, 13).Value

End If
Next
End Sub

Very new to VBA.
Thank you for any help.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi,
add Exit Sub where shown

Rich (BB code):
Private Sub cmdsearch_Click()
    Dim SERIAL As String
    If txttxserial.Text = "" Then
        Msg = "You must enter a serial number!"
        MsgBox Msg
        Me.txttxserial.SetFocus
        
        Exit Sub
    End If
    
    'rest of code

Dave
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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