Pulling info from a Sheet into UserForm

Dan Swartz

Board Regular
Joined
Apr 17, 2020
Messages
53
Office Version
  1. 365
Platform
  1. 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
 

Attachments

  • Update Customer Form.jpg
    Update Customer Form.jpg
    50.7 KB · Views: 6

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try the Find method:

VBA Code:
Private Sub FindCust_Click()
  Dim f As Range
  
  If CustomerName.Value = "" Then
    MsgBox "Enter name"
    CustomerName.SetFocus
    Exit Sub
  End If
  
  Set f = Sheet2.Range("A:A").Find(CustomerName.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    Address1.Text = Sheet2.Cells(f.Row, 2).Value
    Address2.Text = Sheet2.Cells(f.Row, 3).Value
    City.Text = Sheet2.Cells(f.Row, 4).Value
    State.Text = Sheet2.Cells(f.Row, 5).Value
    Zip.Text = Sheet2.Cells(f.Row, 6).Value
    Email.Text = Sheet2.Cells(f.Row, 7).Value
  Else
    MsgBox ("Customer does not exist"), vbOKOnly
    CustomerName.SetFocus
  End If
End Sub
 
Upvote 0
Solution
Try the Find method:

VBA Code:
Private Sub FindCust_Click()
  Dim f As Range
 
  If CustomerName.Value = "" Then
    MsgBox "Enter name"
    CustomerName.SetFocus
    Exit Sub
  End If
 
  Set f = Sheet2.Range("A:A").Find(CustomerName.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    Address1.Text = Sheet2.Cells(f.Row, 2).Value
    Address2.Text = Sheet2.Cells(f.Row, 3).Value
    City.Text = Sheet2.Cells(f.Row, 4).Value
    State.Text = Sheet2.Cells(f.Row, 5).Value
    Zip.Text = Sheet2.Cells(f.Row, 6).Value
    Email.Text = Sheet2.Cells(f.Row, 7).Value
  Else
    MsgBox ("Customer does not exist"), vbOKOnly
    CustomerName.SetFocus
  End If
End Sub
This worked great! thank you for the answer
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

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