Dan Swartz
Board Regular
- Joined
- Apr 17, 2020
- Messages
- 53
- Office Version
- 365
- Platform
- Windows
I have a workbook with a customer database listing my customers. I have a user form on a different page to look for a customer in the customer database, and if it's there, it will fill out the form box with the information that is listed in the Customer database. once the info has been populated to the user form and I can change the information and have it right back to the database with the changes. (This code is working fine. I won't include it) Everything works as expected, but only for the first name in the database. It can't find anyone below the first name listed. it says "Customer does not exist" which is what it's supposed to do if it doesn't exist. However, it does exist and I'm sure it's something little that I'm missing. I'm new to vba. So be nice!
VBA Code:
Private Sub FindCust_Click()
Dim CustN As String
CustN = CustomerName.Text
lastrow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
If Sheet2.Cells(i, 1).Value = CustN Then
Address1.Text = Sheet2.Cells(i, 2).Value
Address2.Text = Sheet2.Cells(i, 3).Value
City.Text = Sheet2.Cells(i, 4).Value
State.Text = Sheet2.Cells(i, 5).Value
Zip.Text = Sheet2.Cells(i, 6).Value
Email.Text = Sheet2.Cells(i, 7).Value
Exit Sub
Else
MsgBox ("Customer does not exist"), vbOKOnly
CustomerName.SetFocus
Exit Sub
End If
Next
End Sub