I am new to VBA and would appreciate some help. I am trying to develop a spreadsheet which I want staff to use, in order to track the status of several thousand customer survey forms as they are returned. Each customer has a unique Cust_id. I have designed a Userform to allow easy recording of info for each customer.
The form will display relevant info for each valid Cust_id entered, however I can't get it to show "Record Not found" if no record is found matching the Cust_id entered. I only want it to appear after they have entered a Cust_id in Textbox1 and pressed Enter. I want the form cleared after each Search and display. This is my code:
Private Sub TextBox1_Enter()
Dim cust_id As String
cust_id = Trim(TextBox1.Text)
lastrow = Worksheets("Main").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
If Worksheets("Main").Cells(i, 3).Value = cust_id Then
TextBox2.Text = Worksheets("Main").Cells(i, 5).Value
CmbSFStatus.Text = Worksheets("Main").Cells(i, 11).Value
CmbComment.Text = Worksheets("Main").Cells(i, 15).Value
TextBox3.Text = Worksheets("Main").Cells(i, 18).Value
TextBox4.Text = Worksheets("Main").Cells(i, 6).Value
TextBox5.Text = Worksheets("Main").Cells(i, 9).Value
TextBox6.Text = Worksheets("Main").Cells(i, 12).Value
End If
Next
rem Clear form
TextBox1.Text = ""
TextBox2.Text = ""
CmbSFStatus.Text = ""
CmbComment.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox1.SetFocus
End Sub
Any assistance would be greatly appreciated.
The form will display relevant info for each valid Cust_id entered, however I can't get it to show "Record Not found" if no record is found matching the Cust_id entered. I only want it to appear after they have entered a Cust_id in Textbox1 and pressed Enter. I want the form cleared after each Search and display. This is my code:
Private Sub TextBox1_Enter()
Dim cust_id As String
cust_id = Trim(TextBox1.Text)
lastrow = Worksheets("Main").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
If Worksheets("Main").Cells(i, 3).Value = cust_id Then
TextBox2.Text = Worksheets("Main").Cells(i, 5).Value
CmbSFStatus.Text = Worksheets("Main").Cells(i, 11).Value
CmbComment.Text = Worksheets("Main").Cells(i, 15).Value
TextBox3.Text = Worksheets("Main").Cells(i, 18).Value
TextBox4.Text = Worksheets("Main").Cells(i, 6).Value
TextBox5.Text = Worksheets("Main").Cells(i, 9).Value
TextBox6.Text = Worksheets("Main").Cells(i, 12).Value
End If
Next
rem Clear form
TextBox1.Text = ""
TextBox2.Text = ""
CmbSFStatus.Text = ""
CmbComment.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox1.SetFocus
End Sub
Any assistance would be greatly appreciated.